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

1

0

I am trying to execute a script that shows a green border on the specified tab (by ID). The script should execute when the response for the requested URL is an error. The problem is that, when I load the extension from about:debugging, I get the following error (in the browser console in FF 53):

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

I searched for hours and hours and looked at several posts for similar problems but none of them have helped me. For example, this post suggests adding "<all_urls>" permission and it did not help in my case. Another post says that it is not possible to execute script in certain type of hosts such as about:[anything] pages and mozilla pages. I do not see my URL belongs to any of them.

Here is my example:

The manifest.json

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

The background script is 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"]}
); 
Yeta answered 13/6, 2017 at 19:7 Comment(0)
P
0

The error you are seeing:

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

is generated when you attempt to inject a script using tabs.executeScript() (or CSS with tabs.insertCSS()) in a tab that is currently displaying a URL which you do not have permission to inject into. In this case, you have specified in your manifest.json the host permission "<all_urls>". The fact that "matchesHost":["<all_urls>"] is displayed indicates that Firefox is aware of your "<all_urls>" permission. That you have still gotten the error means that you have attempted to inject into a URL which does not match <all_urls>.

As you have mentioned, Firefox does not permit injecting into about:* pages. In addition, injecting into pages at the domain addons.mozilla.org is not permitted. None of those pages will match <all_urls>. All such URLs will generate the above error if you attempt to inject into tabs showing them.

But, I'm injecting into some normal URL that had an error

All easily obtainable information to the contrary — including the URL provided in the tabs.Tab data obtained from tabs.get() —, the page you are attempting to inject into is, in fact, an about:* page, not the page (that doesn't exist) at the URL where you got the error. While the URL reported in the tabs.tab structure for the tab in which you received the error will show the URL on which the error occurred, the actual URL for the page being displayed is something like:

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

I know this because the last webNavigation.onDOMContentLoaded event when I tested attempting to load the URL: http://www.exampleahdsafhd.com/ was:

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
}

The fact that the error page is an about:* page, means that you will not be able to inject scripts, or CSS, into it. This means that you will need to find some other way to accomplish what you desire and/or adapt what you desire to do to what is possible. One possibility would be to navigate to a page within your extension which describes the error.

Publicist answered 13/6, 2017 at 22:51 Comment(6)
Thanks. One possibility would be to navigate to a page within your extension which describes the error, you mean redirect the page?Yeta
Effectively, yes. However, you can't directly redirect the request from the webRequest.onErrorOccurred handler (i.e. there's no special way to do so, as is available for onBeforeRequest and onHeadersReceived handlers). In such case, "redirecting" would be a separate navigation, but the navigation should be able to occur fast enough such that the user would not notice they were briefly on the error page, or at least such that it's minimally noticeable to the user.Publicist
If I want to redirect to a page within my extension but it contains javascript. Would it be allowed? In link, among the restricted URLs: javascript: URLs and data: URLs which I am not sure what both of these mean?Yeta
I mean, the page is within the extension files but it contain content script. Would this be possible?Yeta
@user6875880, Any HTML page within your extension which you load must have all JavaScript in file(s) external to the HTML (just like a popup or options page). They are not content scripts (you can't inject an actual content script), but such scripts have access to both the DOM, based on HTML from within your extension, and all the privileged extension APIs.Publicist
I need to return the error URL and I used the code in this example: link but it does not return anything when I encounter error page. It does work in correct URLs but nt error pages. I will appreciate if you can have a look and try it.Yeta

© 2022 - 2024 — McMap. All rights reserved.