-1

I want to create a Chrome extension that allows the user to download text that has been captured via one or more regexes. To learn how to create such an extension, I have downloaded and installed one of Chrome's Sample Extensions: the one titled Download Selected Links.

The extension icon shows up in my browser, and the regex appears to be working properly (the pop-up window shows a long list of links that seem to clearly have been scraped from the tab I'm on), but when I click the 'Download All' button, the pop-up window closes and none of the links I have selected is downloaded.

enter image description here

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
  • 1
    You'll need to use [debugging](https://developer.chrome.com/extensions/tut_debugging) to give yourself a clue as to what's wrong. – Xan Sep 22 '17 at 11:58

1 Answers1

1

I was able to get it working by removing the line of code from popup.js that calls window.close():

// Download all visible checked links.
function downloadCheckedLinks() {
  for (var i = 0; i < visibleLinks.length; ++i) {
    if (document.getElementById('check' + i).checked) {
      chrome.downloads.download({url: visibleLinks[i]},
                                             function(id) {
      });
    }
  }
  window.close(); // <-- Delete this line.
}

The pop-up window will now remain open after you click the 'Download All' button, and the download should begin after a fraction of a second. My best guess is that closing the pop-up with window.close() may have disabled some permission that is necessary for the download to commence.

I ran an experiment where I had all of the URLs selected (a long list, maybe 50 or more), and I clicked 'Download All', and then clicked away from the pop-up so that it would close, and the first five or so pages downloaded, but then no more.

I also came across this seemingly-useful SO question: How to download a file via a Chrome Content Script? ...but I wasn't able to get the message-passing method working (sending a message to a background.html instructing it to download the specified URLs).

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
  • 2
    You can simply call window.close in the callback of the last processed item. – wOxxOm Sep 22 '17 at 12:41
  • 2
    Closing the popup unloads the page; therefore, aborting all code that's queued for execution. If you have trouble getting Messaging to work, do ask a new question. – Xan Sep 22 '17 at 13:13