Exchange of messages through the new Offscreen Document API.

1,046 views
Skip to first unread message

Robbi

unread,
Feb 23, 2023, 2:11:28 PM2/23/23
to Chromium Extensions
Hi developers,
I have an extension I'm trying to convert (with no little effort) to MV3.
This extension modifies the home page of the browser with the directive:
"chrome_url_overrides": { "newtab": "newpage.html" },

I noticed a weird thing using the new Offscreen Document API.

From the SW, on several occasions I need to parse the DOM of an html page that I receive as a response from a fetch command.
When I exchange a message between sender (service worker) and recipient (the offscreen document), that message is delivered to the offscreen document and I get the desired response,
but I noticed that that message
also reaches the runtime.onMessage event handler of the extension page (the new tab) and sometime also reaches the  runtime.onMessage event handler of the SW.

The question I ask you at this point is simple. Is this normal behavior?

Using  runtime.sendMessage shouldn't I get the response only in its callback function or as a return value if I use the promise syntax?

A Minimal, Reproducible Example can be found here:
gitHub Rep

wOxxOm

unread,
Feb 23, 2023, 5:26:17 PM2/23/23
to Chromium Extensions, Robbi
Yes, it's the correct behavior and it always worked like this: chrome.runtime.sendMessage is broadcasting to the entire origin of chrome-extension://id so it reaches every page, every popup, every web_accessible_resources iframe in addition to the background script.

Until a better API is created you can use the standard SW messaging via navigator.serviceWorker in the client (newtab script, offscreen document) + self.onmessage listener in SW. There's also the reversed direction: the SW can find the client via clients.matchAll() then use postMessage on the found client, while the client would use navigator.serviceWorker.onmessage to listen to the messages.

Robbi

unread,
Feb 23, 2023, 5:42:38 PM2/23/23
to Chromium Extensions, wOxxOm, Robbi
Thanks you wOxxOm,
to send a message to an extension page I've always used tabs.sendMessage,
however I was convinced that runtime.sendMessage could only communicate with background scripts, SW and action popups.
You gave me ideas to think about...

Robbi

unread,
Feb 23, 2023, 6:05:50 PM2/23/23
to Chromium Extensions, Robbi, wOxxOm
I was wondering if it hadn't been better think the OSD as a hidden tab with a given tabId (as Firefox seems to me to do) so that messages could only reach the OSD.
Of course, that's just my thought.

Jordan Cohen

unread,
Jul 9, 2023, 4:10:07 PM7/9/23
to Chromium Extensions, Robbi, wOxxOm
I am pretty sure that this is what is says in the docs too... I didn't think it was even possible for 2 content scripts to talk to each other without a background script brokering the message...

wOxxOm

unread,
Jul 9, 2023, 4:17:20 PM7/9/23
to Chromium Extensions, Jordan Cohen, Robbi, wOxxOm
This topic is not about content scripts though.
Reply all
Reply to author
Forward
0 new messages