简体   繁体   English

如何在下载URL时强制Chrome不打开“另存为”对话框?

[英]How to force Chrome to NOT open SaveAs Dialog when downloading a URL?

Chrome Build: the newest, 33+ Chrome版本:最新的33+

A Chrome Extension extracts certain urls from currently viewed site and then downloads a subset of them (quite often hundreds of files). Chrome扩展程序从当前查看的网站中提取某些网址,然后下载其中的一部分(通常为数百个文件)。

Expected Behavior: 预期行为:

Files are downloaded into the default Download-Folder without questioning where and under which filename they have to be saved. 文件被下载到默认的“下载文件夹”中, 而无需询问文件的保存位置和文件名。

Problem: 问题:

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. 如果用户在Chrome->设置->高级设置->下载中启用了“在下载前先询问保存每个文件的位置”选项,则在尝试同时下载(例如100个文件)时,Chrome尝试打开100个“另存为”对话框,然后崩溃。

What I tried: 我试过的

  • to use chrome.downloads.download(object options, function callback) method with an option saveAs: false 使用带有选项saveAs的chrome.downloads.download(对象选项,函数回调)方法:false
  • using the following code to trigger a download through an emulated mousevent: 使用以下代码通过模拟的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. 编辑 :我为多个文件下载添加了完整的示例代码,其中没有显示“另存为”对话框。

You can achieve this by using chrome.downloads API . 您可以使用chrome.downloads API来实现。

manifest.json 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 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 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
        }
    }
});

It is impossible when "Ask where to save each file before downloading" Enabled (as of 70.0.3538.77). 启用“在下载之前询问每个文件的保存位置”时(从70.0.3538.77开始)。 The corresponding Chromium bug is: 相应的Chromium错误是:

Bug 417112: chrome.downloads.download ignore saveAs 错误417112:chrome.downloads.download忽略saveAs

Moreover setting filename in chrome.downloads.downloads() also doesn't work. 此外,在chrome.downloads.downloads()设置文件名也不起作用。

Bug 758094: Extension can not rename downloaded file 错误758094:扩展名无法重命名下载的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Chrome中强制自动打开对话框 - Force Auto Open Dialog in Chrome 如何通过Chrome扩展程序显示“文件另存为”对话框? - How do I show a File Saveas dialog from a Chrome Extension? 如何在左键上强制打开“另存为...”对话框以从远程服务器下载文件 - How to force open “Save as…” dialog on left click for downloading file from remote server 下载文件时强制显示“另存为”对话框 - Force showing the "Save as" dialog box when downloading a file Servlet:强制浏览器(Google Chrome)立即打开“另存为”对话框 - Servlet: Force Browser (Google Chrome) to Open 'Save As' Dialog Immediately 如何强制链接在手机中打开“打开方式”对话框? - How to force link to open "open with" dialog in mobile phone? 如何检查打印对话框是否已打开(Mozilla和Chrome) - How to check if print dialog is open (Mozilla and Chrome) 如何在 chrome 扩展中打开对话框 - How can open dialog in chrome extension 如何检测铬相机访问对话框已打开 - how to detect chrome camera access dialog is open chrome.downloads.download会忽略saveAs选项并打开对话框,无论 - chrome.downloads.download ignores saveAs option and opens the dialog regardless
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM