【发布时间】:2017-09-11 16:28:58
【问题描述】:
我正在使用 chrome 消息传递功能发送文件名和链接到后台页面
chrome.runtime.sendMessage({link: thelink, name:filename}, function(response) {});
背景.js
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
console.log(message.name);
chrome.downloads.download({
url: message.link,
filename: message.name,
conflictAction: 'prompt'
});
});
我的 manifest.json
{
"manifest_version": 2,
"name": "4ch",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",
"icons": {
"16": "4ch-icon-16.png", "48": "4ch-icon-48.png", "128": "4ch-icon-48.png"
}
,
"permissions": ["downloads",
"<all_urls>",
"contextMenus"],
"content_scripts": [{
"js": ["script/jquery.js", "script/reddit.js"],
"matches": ["*://*.reddit.com/*"]
}
],
"background": {
"scripts": ["script/jquery.js","script/eventpage.js"], "persistent": false
}
}
下载仅在几秒钟后开始,并且不会更改文件名
【问题讨论】:
-
刚刚试了一下,对我来说效果很好。需要更多上下文。如果有延迟,它可能在您调用
sendMessage或其他内容之前出现在您的内容脚本中。如果您检查后台页面,您是否在控制台中看到任何错误? -
它对你有用吗?我只是在后台页面控制台中尝试使用链接和文件名。而且文件没有我给的名字
-
是的。在我的扩展程序的背景页面中运行它就可以了:
chrome.downloads.download({ url: "http://thecatapi.com/?id=cco", filename: "g.png", conflictAction: 'prompt' });我还使用runtime做了一个完整的测试,就像你的问题一样,这也很好。因此,我们需要有关您的上下文的更多信息。 -
谢谢。我想到了。我的扩展程序与“chrome download Master”扩展程序冲突当我禁用它时它可以工作
-
请edit 成为主题的问题:包括一个minimal reproducible example 重复问题。对于 Chrome 扩展程序或 Firefox WebExtensions,您几乎总是需要包含您的 manifest.json 和一些背景、内容和/或弹出脚本/HTML,通常是网页 HTML/脚本。寻求调试帮助的问题(“为什么我的代码没有按我想要的方式工作?”)必须包括:(1)期望的行为,(2)特定的问题或错误以及(3)重现它所需的最短代码在问题本身中。另请参阅:What topics can I ask about here? 和 How to Ask。
标签: google-chrome google-chrome-extension