下载URL时如何强制Chrome不打开SaveAs对话框?

Qut*_*its 8 javascript google-chrome download google-chrome-extension

Chrome Build:最新的33+

Chrome扩展程序从当前查看的网站中提取某些网址,然后下载其中的一部分网址(通常是数百个文件).

预期行为:

文件将下载到默认的下载文件夹中,而不会询问他们必须保存的文件名的位置和位置.

问题:

如果用户在Chrome->设置 - >高级设置 - >下载中启用了"在下载前询问保存每个文件的位置"选项,那么当尝试同时下载100个文件时,Chrome会尝试打开100个SaveAs对话框,崩溃.

我尝试了什么:

  • 使用chrome.downloads.download(对象选项,函数回调)方法和选项saveAs:false
  • 使用以下代码通过模拟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);
    }
    
    Run Code Online (Sandbox Code Playgroud)

Ran*_*sam 1

编辑:我添加了用于多个文件下载的完整示例代码,但不显示“另存为”对话框。

您可以使用chrome.downloads API来实现此目的。

清单.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"
}
Run Code Online (Sandbox Code Playgroud)

内容脚本.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);
    })
}
Run Code Online (Sandbox Code Playgroud)

背景.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
        })
    }
});
Run Code Online (Sandbox Code Playgroud)