wOxxOm
unread,Mar 29, 2023, 8:25:22 PM3/29/23Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Chromium Extensions, Patrick Kettner, Chromium Extensions, Darin Dimitrov
> This is working as intended. Scripts that are injected via chrome.scripting do not have access to onMessage. You can access chrome.runtime.onMessage via content-script, however.
While this is intended, but not for the reason stated in the quote, which is actually wrong because chrome.scripting by itself doesn't preclude the use of onMessage.
The actual reason for the observed behavior is that the code is explicitly injected by the author in MAIN world, i.e. the normal JS context of the page where all the web page scripts run. The `chrome` API is exposed for contents scripts only in the default ISOLATED world which is used by default when you don't specify `world` parameter.
The MAIN world (the web page world) sees `chrome` only when there is at least one extension that has this site listed in
externally_connectable in its manifest.json.
Note that code you inject in the MAIN world can be hijacked and the data may be intercepted by any other web page script (or by another extension's script) that already ran in the page and spoofed global objects like Object.prototype, Array.prototype, Function.prototype. Never inject in the MAIN world your entire content script. Only inject a small portion that needs to access some JS object in the MAIN world, and never trust this data fully i.e. sanitize/verify it.
Chrome team has made a mistake by naming such MAIN-world scripts "content scripts" so this kind of confusion is already in the process of becoming a widespread problem.