mv3, seeing "cannot access contents of the page"

200 views
Skip to first unread message

Nathan Peterson

unread,
Apr 22, 2024, 5:58:00 PMApr 22
to Chromium Extensions
Hello,

I'm getting ready to migrate my extension to MV3 and I wanted to check about an odd behavior. In my manifest, I have requested all_urls

`{... host_permissions: ["<all_urls>"] } `

In the background script, I inject a very small content script that checks for a property in the head of the document and returns a response:

const results = await chrome.scripting.executeScript({
target: { tabId: <tabId> },
func: () => !!document.head.querySelector(<selector>),
});

This code executes as expected but prints a red console error: "Error: Cannot access contents of the page. Extension manifest must request permission to access the respective host."

Is this expected? Can I safely swallow the error since the code gets executed? Any chance this has to do with injecting using the `func` option rather than the `files` option? (I ask this last question because I am also injecting files but they seem not to throw the error)

Thanks

wOxxOm

unread,
Apr 22, 2024, 10:41:32 PMApr 22
to Chromium Extensions, Nathan Peterson
> Can I safely swallow the error since the code gets executed?

Yes and no. The code was executed in a different tab. The "bad" tab isn't injectable, so nothing was injected, e.g. its URL is chrome:// or chrome-extension:// or it's a start (newtab ) page. It can also happen on URLs forbidden via policies and when the user manually changed the sites on which the extension is allowed. The easiest way to handle it is try{...} catch(err) {...} where you can ignore the error if its message matches the one you see.

Note that chrome://extensions UI shows old errors from past runs so you must remove them manually by clicking the corresponding button.

wOxxOm

unread,
Apr 23, 2024, 10:08:47 AMApr 23
to Chromium Extensions, wOxxOm, Nathan Peterson
The error can also happen if you inject into a tab that was just created via chrome.tabs.create or navigated via chrome.tabs.update or inside chrome.tabs.onUpdated listener when tab only starts loading. In these cases you should wait for the tab to get a url first, see https://stackoverflow.com/a/67244340
Reply all
Reply to author
Forward
0 new messages