Google chrome extension 在chrome.downloads.download之后运行文件

Google chrome extension 在chrome.downloads.download之后运行文件,google-chrome-extension,download,Google Chrome Extension,Download,我的Chrome扩展中有一个后台脚本,它从扩展目录下载一个名为install.bat的文件。那很好用。但是当我想调用chrome.downloads.open(id)引发以下错误: Unchecked runtime.lastError while running downloads.open: User gesture required 我已在manifest.json文件中请求了此过程所需的两个权限([“downloads”,“downloads.open”]) 这个问题有没有解决办法,甚

我的Chrome扩展中有一个后台脚本,它从扩展目录下载一个名为
install.bat
的文件。那很好用。但是当我想调用
chrome.downloads.open(id)引发以下错误:

Unchecked runtime.lastError while running downloads.open: User gesture required
我已在
manifest.json
文件中请求了此过程所需的两个权限(
[“downloads”,“downloads.open”]


这个问题有没有解决办法,甚至是简单的解决办法?

因此,在我阅读了他评论中提到的讨论之后,我找到了解决问题的办法。通知现在再次算作
用户手势
。 但是,如果在清单中请求权限
downloads.open
时无法自动打开下载,则此权限将毫无用处

因此,这是我的解决方案(对于wich,我并不十分满意,因为下载不会自动打开),但它对我有效:

var downloadID = 123;

var nIcon = chrome.extension.getURL("icons/icon_48.png");
var nTitle = "My Extension - Client Installer";
var nMessage = "Please click the button below to run the installer.";
var nButtons = [{ title: "Run the installer..." }];

var nOptions = { type: "basic", iconUrl: nIcon, priority: 2, title: nTitle, message: nMessage, buttons: nButtons };
chrome.notifications.create("hello_world", nOptions, function (nIDa) {
    chrome.notifications.onButtonClicked.addListener(function (nIDb, nButtonIndex) {
        if (nIDb === nIDa) {
            chrome.downloads.open(downloadID);
        }
    });
});

看一看。答案上的注释似乎解释了您遇到的问题。@dan75我看到了这个问题和注释,但它们没有提供解决方案。上面说你可以“展示一个自定义按钮”。但是怎么做呢?因为JavaScript confirm()对话框和chrome.downloads.acceptDanger不起作用。我想这是一个已知的问题。“将用户手势处理添加到通知api。这修复了用户与通知交互后未考虑用户手势的错误。”