Error: No window matching {"matchesHost":[]} for tabs.insertCSS()
Asked Answered
H

1

0

I'm writing a WebExtension where a background script communicates between a popup window (UI script) and web pages (content script).

Using port = browser.runtime.connect({name: "ProjName"});, the content script connects to the background script as soon as it loads.

The background script registers that:

function connected(p) {
    ...
        console.log(p.sender.tab.id); // <-- works fine, gives me an integer Tab ID
        browser.tabs.insertCSS(p.sender.tab.id, {file : "/css/stylesheetName.css"});
    ...
}

browser.runtime.onConnect.addListener(connected);

and tries to insertCSS(), using the sending tab's ID.

I always get the Error: No window matching {"matchesHost":[]} message. I'm not sure what's happening here, I'm not sure what part of the script is now matching against a host...?!?

I can use browser.tabs.sendMessage(p.sender.tab.id, msg); just fine in exactly the same place. Hell, if I knew how to reliably read the file from disk, I would send its contents over via sendMessage at this point.

Where might this be coming from?

Edit: I've stripped it down to only the necessary code (and no page_Action, no popup, etc) and uploaded a .zip: [removed] Which you can Load as Temporary Add-On on the firefox about:debugging page.

Haematothermal answered 13/2, 2017 at 19:28 Comment(1)
Please edit the question to be on-topic: include a complete minimal reproducible example that duplicates the problem. Including a manifest.json, some of the background/content/popup scripts/HTML. Questions seeking debugging help ("why isn't this code working?") must include: ►the desired behavior, ►a specific problem or error and ►the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: "How to create a minimal reproducible example", What topics can I ask about here?, and How to Ask.Unobtrusive
H
1

It took me adjusting it for Chrome and looking at the background page there... Although I have logging for extensions enabled in Firefox, it didn't give me what I needed, but Chrome did: It was another missing permission.

Unchecked runtime.lastError while running tabs.insertCSS: Cannot access contents of url "[url]". Extension manifest must request permission to access this host.

adding "<all_urls>" to the permissions in the manifest.json did the trick:

"permissions": [
    "activeTab",
    "tabs",
    "storage",
    "<all_urls>"
],
Haematothermal answered 13/2, 2017 at 20:21 Comment(7)
Please add your original manifest.json to the question. Currently, someone would have to be psychic to be able to know this is the answer to your problem.Unobtrusive
@Makyen, it looks more like Firefox neglected the developer friendliness in that case.Inobservance
@Ivancho, I disagree. First, the requirement originates in Chrome, so the permissions philosophy, which is what dictates this requirement, started there. However, the philosophy is reasonable. The permissions in the manifest.json allow the browser to display to the user what the extension is requesting to be able to do. This allows the user to make the choice to install the extension based on these permissions. For Chrome, this is critical. For Firefox, less so, because, unlike Chrome, all Firefox extensions are reviewed by a human for malicious/duplicitous code prior to being listed on AMO.Unobtrusive
@Makyen, what you say is nice, but it does not explain why we get this cryptic message Error: No window matching {"matchesHost":[]} instead of a more developer friendly message. I admit I overlooked the host permissions in the manifest file, but Firefox did not say anything related to it. And downvoting the question and the answer is not nice. My +1 neutralized the -1 of someone else.Inobservance
@Ivancho, From your last message it's clear that I didn't understand what you were trying to say. It wasn't clear to me from your initial comment that what you were talking about is the actual error message rather than the requirement to include permissions. As to the down-voting: Yes, I down-voted both the question (Q) an the answer. I did so because, the Q does not contain a manifest.json. The information in that file is directly relevant to this problem. In fact, without the contents of that file we can not be certain the answer is correct. (continued)Unobtrusive
We can guess that it very likely solves the problem, but it's still a guess. Thus, the Q is incomplete. It is, in fact, off-topic and should be closed due to not containing a minimal reproducible example. I down-voted the answer because this self-answer takes advantage of information not included in the Q, which means other people are at an unfair disadvantage wrt. answering. If the manifest.json was included in the Q, I might have up-voted both, but I certainly would not have down-voted. In comments on both the Q and Answer, I have already explicitly requested that the OP include a manifest.json in the Q.Unobtrusive
I had the app, including manifest.json linked in the Q shortly after posting, but I removed it because it was unnecessary, as the problem was a permission issue (and any further complication, like "cryptic messages" lay with Firefox) - I don't have it anymore, sorry to inconvenience any of you virtuous scavengers looking to solve the behind-the-scenesHaematothermal

© 2022 - 2024 — McMap. All rights reserved.