0

I'm trying to cancel every download just as they are started. Here's my code so far:

var downloadCreated = function (a) {
    // cancel the download
    chrome.downloads.cancel(a.id);
    chrome.downloads.erase({id: a.id}, function (d) {
    });
};

chrome.downloads.onCreated.addListener(downloadCreated);

I've set Chrome to display Save As dialog when downloads are about to start. I run my extension and click on a link several times. Sometimes the download is simply cancelled (which is the exact behavior that I want), and sometimes the Save As dialog is displayed. Clicking on Save button in the dialog has no effect and nothing will be downloaded. Whether the dialog is shown or not is random.

I'm just trying to avoid displaying the Save As dialog by any means. Do you guys know a solution for this?

Javid
  • 2,755
  • 2
  • 33
  • 60
  • To cancel the downloads in chrome, you need to use the [`chrome.downloads.cancel`](https://developer.chrome.com/extensions/downloads) for it. Now, for the save as dialog part that you don't want to pop up, try to check these SO questions [Supress the Save as dialog in Chrome](http://stackoverflow.com/questions/14286215) and [Save a file without the save dialog box in Chrome Apps](http://stackoverflow.com/questions/20836604/) if it can help you to suppress the save as dialog. – KENdi Mar 14 '17 at 14:49
  • @KENdi Thanks I'l try it – Javid Mar 14 '17 at 19:23

1 Answers1

0

I found the solution some time ago but forgot to provide the answer. Here it is:

To stop a Chrome download attempt, chrome.downloads.cancel should be called in the proper event, that is chrome.downloads.onDeterminingFilename. That's it! No useless save dialogs will appear.

Javid
  • 2,755
  • 2
  • 33
  • 60