B
B
BartGTO2018-06-16 11:21:34
JavaScript
BartGTO, 2018-06-16 11:21:34

Timer in chrome extension?

Hello.
There is an extension (addition, application) for the Google Chrome browser. When selecting several links, files are downloaded, but since files are downloaded one by one and before the previous file is loaded, a new file is loaded into the queue (300kb photos - 150 pieces), and, accordingly, some of the files go into cancellation (download failure). As a result, you have to view the download history and look for files that have not downloaded.
I see the following solution to the issue: set a timer, after calling the link, to download each file for 2 seconds (the computer and the Internet are weak). Those. selected link, download started, 2 second delay, selected link, download started, 2 second delay, etc.
I unloaded the extension from the browser, but in programming I am 0.
Please help me complete the program.
files:
background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  var startLink = "https://"+cleanUp(tab.url);
  chrome.tabs.sendMessage(tab.id, {greeting: 'test'}, function(response){
    var length = response.links.length;
    for ( var i = 0; i < length; i++) {
      chrome.downloads.download({
        url: startLink + response.links[i],
        //saveAs: true
        conflictAction: "uniquify"
      }); //Здесь нужно вставить паузу
    }
  });
});

function cleanUp(url) {
  if(url.search(/^https?\:\/\//) != -1)
    url = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i, "");
  else
    url = url.match(/^([^\/?#]+)(?:[\/?#]|$)/i, "");
  return url[1];
}

content.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
  var links = [];


  $('.main-grid-table tbody tr.main-grid-row-checked').each(function(){
    console.log($(this));
    $(this).find('a').each(function(){
      var link = /download=y/i.exec( $(this).attr('href') );
      if ( link ) {
        links.push( $(this).attr('href') );
      }
    });
  });

  if ( links.length < 1 ) {
    alert('Вы не выбрали ни одной строки для массового скачивания!');
  } else {
    sendResponse({links: links});
  }
});

5b24c87863b1f604849762.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question