How do I get Chrome to NOT open the SaveAs dialog when loading a URL?

Chrome Build: Newest, 33+

The Chrome extension retrieves specific URLs from the site you are currently viewing and then downloads a subset of them (often hundreds of files).

Expected Behavior:

Files are downloaded to the Download-Folder folder without, by default, where and under what file name should they be saved.

Problem:

If the user has turned on the “Ask where to save each file before downloading” option in Chrome-> Settings-> Advanced Settings-> Downloads, when trying to download, for example, 100 files at a time, Chrome tries to open 100 SaveAs Dialogs and crashes.

What I tried:

  • use the chrome.downloads.download method (object method, callback function) with the saveAs option : false
  • using the following code to start the download through mousevent emulation:

    function saveAs(Url,filename){ var blob=new Blob([''], {type:'application/octet-stream'}); var url = webkitURL.createObjectURL(blob); var a = document.createElementNS('http://www.w3.org/1999/xhtml','a'); a.href = Url; a.download = filename; var e = document.createEvent('MouseEvents'); e.initMouseEvent('click', false, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); a.dispatchEvent(e); webkitURL.revokeObjectURL(url); } 
+7
javascript google-chrome google-chrome-extension download
source share
1 answer

Change I have added a complete code sample for several file downloads that the SaveAs dialog does not show.

This can be done using the chrome.downloads API .

manifest.json

 { "description": "Multiple file downloads without showing SaveAs Dialog", "background": { "scripts": [ "background.js" ], "persistent" : true }, "content_scripts": [{ "js": [ "content_script.js"], "matches": [ "<all_urls>" ], "run_at": "document_start" }], "manifest_version": 2, "name": "MultipleFileDownloads", "permissions": [ "downloads" ], "short_name": "MFD", "version": "0.0.0.1" } 

content_script.js

 var DOWNLOAD_LIMIT = 100; function downloadURL(url, filename, callback){ chrome.runtime.sendMessage({ download_url : url, filename : filename },function(){ if(typeof callback == 'function'){ callback(); } }) } function simulateFileDownload(i){ if(i > DOWNLOAD_LIMIT){ document.getElementById('download_btn').disabled = false; return false; } var blob = new Blob(['This is sample file '+i], {type:'text/plain'}); var url = URL.createObjectURL(blob); downloadURL(url,'Sample-'+i+'.txt',function(){ URL.revokeObjectURL(url); i++; simulateFileDownload(i); }) } window.onload = function(){ var btn = document.createElement('button'); btn.id = 'download_btn'; btn.style.cssText = 'position:fixed;top:10px;left:10px;width:140px;height:30px;z-index:1000000;'; btn.textContent = 'Download Files'; document.body.appendChild(btn); btn.addEventListener('click',function(){ this.disabled = true; simulateFileDownload(0); }) } 

background.js

 chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ if(message.download_url){ chrome.downloads.download({ url : message.download_url, filename : message.filename, saveAs : false } } }); 
0
source share

All Articles