【问题标题】:chrome.tabs.executeScript not working inside chrome.tabs.onUpdated callbackchrome.tabs.executeScript 在 chrome.tabs.onUpdated 回调中不起作用
【发布时间】:2015-07-17 13:22:26
【问题描述】:

见下面代码中的 cmets:

chrome.browserAction.onClicked.addListener(function(tab) {

  // ################# This works without any problems
  chrome.tabs.executeScript(tabId, {
    code: '!!window.LoadedFlag'
    }, function (hasContentJs) { ... });


  chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {

    //  ################# This throws error:
    // Unchecked runtime.lastError while running tabs.executeScript:
    // Cannot access contents of url "http://localhost:3000/". Extension manifest must request permission to access this host.
    chrome.tabs.executeScript(tabId, {
      code: '!!window.LoadedFlag'
    }, function (hasContentJs) { ... });

我不允许在专门针对 chrome.tabs.onUpdated.addListener 的回调中执行 chrome.tabs.executeScript 吗?

我的用例是我需要在每次页面刷新时运行启动脚本,但前提是我的扩展程序已启用。

manifest.json:

"permissions" : [
    "activeTab"
    "tabs",
    "http://*/*",
    "https://*/*"
],

【问题讨论】:

  • 您在设置新权限后是否重新加载了您的扩展程序?并且使用 onUpdated 并不是选择性地注入内容脚本的最佳方式。如需更好的选择,请参阅stackoverflow.com/questions/26667112/…

标签: google-chrome-extension


【解决方案1】:

抱歉,只是重新加载扩展的一个简单错误。更改 .js 文件时,只需点击 cmd+r 即可重新加载它。看起来在更改manifest.json 时我需要转到chrome://extentions 并进行更困难的重新加载。感谢 Rob W 在评论中提供提示并链接到更好的替代方案。

【讨论】:

    猜你喜欢
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多