Google chrome extension Chrome扩展插件executeScript以静默方式失败
我试图从后台页面在内容页面内执行一些代码,但它以静默方式失败。我可以通过调试看到断点碰到executeScript行 background.js:Google chrome extension Chrome扩展插件executeScript以静默方式失败,google-chrome-extension,Google Chrome Extension,我试图从后台页面在内容页面内执行一些代码,但它以静默方式失败。我可以通过调试看到断点碰到executeScript行 background.js: chrome.tabs.onUpdated.addListener(function (tabId, info, tab) { if (info.status === 'complete' ) { chrome.tabs.executeScript(tabId, { file: "alerter.js
chrome.tabs.onUpdated.addListener(function (tabId, info, tab) {
if (info.status === 'complete' ) {
chrome.tabs.executeScript(tabId, {
file: "alerter.js"
},
function( result) {
console.log(result);
});
}
});
alerter.js:
alert("is this thing on?");
manifest.json的部分:
"permissions": [
"tabs"
],
"web_accessible_resources": [
"jquery.min.js",
"jquery.min.map",
"alerter.js"
],
有什么想法吗,伙计们?我还应该注意,在结果回调中,结果未定义,但回调确实运行
更新:
好的,我发现如果我把“”放在清单的权限部分,它就会工作。这可能不是一件好事。如果我将“alerter.js”放入权限部分,则扩展无法加载:
There were warnings when trying to install this extension:
Permission 'alerter.js' is unknown or URL pattern is malformed.
这里的正确权限是什么?可能重复@RobW这似乎不是该问题的重复,因为OP正在尝试将脚本插入
选项卡。onUpdate
事件,不是浏览器操作或上下文菜单事件。@Darthg8r如果您希望能够在没有用户干预的情况下以编程方式在每个页面上执行内容脚本,那么您肯定需要
权限。@rsanchez虽然问题不是完全相同的重复问题,但根本问题是相同的:缺少主机权限。被接受的答案显示了该问题的一个特例(activeTab
permission),但它接着给出了一个更一般的答案(*://*/*/*
permission)以及针对此类问题的调试提示。在我看来,问题的相似性和现有答案的有用性证明了这个问题被标记为另一个问题的重复。@Darthg8r您应该使用需要访问尽可能少的域的模式,这样用户就不太可能被吓跑。您可以查看URI模式的规范。