如何在下载网址时强制Chrome浏览器不打开“另存为"对话框? [英] How to force Chrome to NOT open SaveAs Dialog when downloading a URL?

查看:164
本文介绍了如何在下载网址时强制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
        }
    }
});

这篇关于如何在下载网址时强制Chrome浏览器不打开“另存为"对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆