Injecting into navigation error page gets: Error: No window matching {"matchesHost":["<all_urls>"]}

Injecting into navigation error page gets: Error: No window matching {"matchesHost":["<all_urls>"]}

我正在尝试执行在指定选项卡(按 ID)上显示绿色边框的脚本。当请求的 URL 的响应出错时,脚本应该执行。问题是,当我从 about:debugging 加载扩展时,出现以下错误(在 FF 53 的浏览器控制台中):

Error: No window matching {“matchesHost”:[“<all_urls>”]}

我搜索了好几个小时,并查看了几篇类似问题的帖子,但其中 none 对我有所帮助。例如,这个 suggests adding "<all_urls>" permission and it did not help in my case. Another 表示无法在某些类型的主机(例如 about:[anything] 页面和 mozilla 页面)中执行脚本。我没有看到我的 URL 属于他们中的任何一个。

这是我的例子:

manifest.json

{
  "manifest_version": 2,
  "name": "test",
  "version": "1.0",
  "background": {
    "scripts": ["test.js"]
  },
 "permissions": [
    "<all_urls>",
    "activeTab",
    "tabs",
    "storage",
    "webRequest"
  ]
}

后台脚本为test.js:

   console.log("-- inside js file --");
    var target = "<all_urls>"; 
    function logError(responseDetails) {    
          errorTab=responseDetails.tabId;
          console.log("response tab: "+errorTab);

    var makeItGreen = 'document.body.style.border = "5px solid green"';
    var executing = browser.tabs.executeScript(errorTab,{
    code: makeItGreen
    });
}//end function

browser.webRequest.onErrorOccurred.addListener(
  logError,
  {urls: [target],
  types: ["main_frame"]}
); 

您看到的错误:

Error: No window matching {"matchesHost":["<all_urls>"]}

当您尝试使用 tabs.executeScript() (or CSS with tabs.insertCSS()) 在当前显示您无权注入的 URL 的选项卡中注入脚本时生成。在这种情况下,您在 manifest.json 中指定了主机权限 "<all_urls>"。显示 "matchesHost":["<all_urls>"] 的事实表明 Firefox 知道您的 "<all_urls>" 权限。你仍然得到错误意味着你试图注入一个不匹配 <all_urls>.

的 URL

如您所述,Firefox 不允许注入 about:* 页面。此外,不允许在域 addons.mozilla.org 中注入页面。 None 个页面将匹配 <all_urls>。如果您尝试注入显示它们的选项卡,所有这些 URL 都会产生上述错误。

但是,我正在注入一些有错误的正常 URL

相反,所有容易获得的信息 — 包括 tabs.Tab data obtained from tabs.get() 中提供的 URL —,您试图注入的页面实际上是一个 about:* 页面,不是 URL 页面(不存在),您在其中收到错误。虽然在您收到错误的选项卡的 tabs.tab 结构中报告的 URL 将显示发生错误的 URL,但页面的实际 URL显示是这样的:

about:neterror?e=dnsNotFound&u=[URL you were attempting to get to, but encoded as a query string]

我知道这是因为我测试尝试加载 URL 时的最后一个 webNavigation.onDOMContentLoaded 事件:http://www.exampleahdsafhd.com/ 是:

webNavigation.onDOMContentLoaded - > arg[0] = Object {
    url: "about:neterror?e=dnsNotFound&u=http%3A//www.exampleahdsafhd.com/&c=UTF-8&f=regular&d=Firefox%20can%E2%80%99t%20find%20the%20server%20at%20www.exampleahdsafhd.com.",
    timeStamp: 1497389662844,
    frameId: 0,
    parentFrameId: -1,
    tabId: 2,
    windowId: 3
}

错误页面是 about:* 页面这一事实意味着您将无法向其中注入脚本或 CSS。这意味着您将需要找到一些其他方法来完成您想要的 and/or 使您想要做的事情适应可能的事情。一种可能是导航到您的扩展中描述错误的页面。