如何在下载网址时强制Chrome浏览器不打开“另存为"对话框?
Chrome Build:最新的33 +
Chrome Build: the newest, 33+
Chrome扩展程序会从当前查看的网站中提取某些网址,然后下载其中的一部分(通常是数百个文件).
A Chrome Extension extracts certain urls from currently viewed site and then downloads a subset of them (quite often hundreds of files).
预期行为:
将文件下载到默认的下载文件夹中,而无需询问必须将文件保存在何处以及以何种文件名保存.
Files are downloaded into the default Download-Folder without questioning where and under which filename they have to be saved.
问题:
如果用户在Chrome->设置->高级设置->下载中启用了在下载前先询问保存每个文件的位置"选项,则在尝试同时下载(例如100个文件)时,Chrome尝试打开100个文件另存为对话框并崩溃.
If a user has enabled the option "Ask where to save each file before downloading" in Chrome->Settings->Advanced Settings->Downloads then when trying to download, for example, 100 files simultaniously, Chrome tries to open 100 SaveAs Dialogs and crashes.
我尝试过的事情:
- 使用带有选项 saveAs:false 的chrome.downloads.download(对象选项,函数回调)方法
-
使用以下代码通过模拟的mousevent触发下载:
- to use chrome.downloads.download(object options, function callback) method with an option saveAs: false
using the following code to trigger a download through an emulated mousevent:
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);
}
编辑:我为多个文件下载添加了完整的示例代码,其中没有显示另存为"对话框.
Edit : I've added complete sample code for multiple file downloads which doesn't show SaveAs Dialog.
您可以使用 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
}
}
});