0

Popup.js:

I have a bunch of pages (for example 3), which I get from chrome history according to some logic. Next I put "download links" (one per page, so 3) from this pages to an array, and transfering them to content.js with port.postMessage();

Content.js:

I'm getting that array from popup.js, and next I need to download files. But for some reason script download only last link, and ignore previous 2

Can someone help me with this problem, and explain why it is happening?

popup.js

chrome.tabs.query({currentWindow: true,active: true}, 
function(tabs)
{    
       port = chrome.tabs.connect(tabs[0].id,{name: "download"});
       port.postMessage(info);                        
}); 

content.js

port.onMessage.addListener(function(response) 
{
      for (var i = 0; i < response.length; i++)
      {
            document.cookie = cook
            window.location = response[i]
      }
})
FasterBur
  • 11
  • 4
  • Assigning to `window.location` immediately navigates the tab to that URL meaning the current tab's contents is destroyed along with the extension's content script that also run inside the tab's process. You'll need to change the entire workflow of your extension. – wOxxOm Mar 28 '19 at 12:45
  • Not sure exactly what I have to do... Is it not possible to open several links in content.js? I've also trying something like this: var h = document.createElement('a'); h.setAttribute('href', link); h.click(); But it still executes only last link. Can I execute download part in popup for somehow? – FasterBur Mar 28 '19 at 13:11
  • I want to download files from website. Due to that I need to send cookie (name of file) and click "download link" after that. In my extension I push "download" button and it sends an array with cookies and links to content.js After that loop cycle send cookies and 'click' links... thats it... Just now I realize, that if I put an alert() after .click() it works fine and download all nedeed files. So problem with pausing, I need somehow pause the loop – FasterBur Mar 28 '19 at 13:47
  • Ah, those are non-navigatable links. That's a different story, I'll try to show an example shortly. – wOxxOm Mar 28 '19 at 13:55
  • Add [Mozilla's WebExtension polyfill](https://github.com/mozilla/webextension-polyfill) as the first content script and then do something like [the async/await example](https://stackoverflow.com/a/11488129) with await new Promise(r => setTimeout(r, 100)) – wOxxOm Mar 28 '19 at 14:01
  • I have to download zip with Mozilla's WebExtension polyfill , unpack it and copy "browser-polyfill.js" to my extension folder? After that I've got an error "Uncaught ReferenceError: module is not defined" and Chrome higlight this line " module.exports = wrapAPIs(chrome);" I've try to use full path, and copy-paste folder with Mozilla's WebExtension polyfill to my extension folder, but it also gives no results I'm sorry to bother you, but js is completely new for me – FasterBur Mar 29 '19 at 06:28
  • See [installation](https://github.com/mozilla/webextension-polyfill#installation) and use browser-polyfill.min.js from unpkg link. – wOxxOm Mar 29 '19 at 06:34
  • I've finally got it! Thank you so much, you are my saviour! – FasterBur Mar 29 '19 at 07:37

0 Answers0