问题
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.
回答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>"
],
来源:https://stackoverflow.com/questions/42212014/error-no-window-matching-matcheshost-for-tabs-insertcss