Trying to pass messages from an injected script back to my extension in Manifest v3.

190 views
Skip to first unread message

Bogdan Nazaruk

unread,
Oct 30, 2023, 11:12:29 AM10/30/23
to Chromium Extensions
Hi folks! 

I want to make a simple extension helping with debugging analytics. For no specific sites, it should work everywhere. I want the popup to display some analysis of the active tab's window variables. 

One would think it's trivial enough to not be challenging and yet, It's quite tough to just set up an infrastructure for it. I found this article: https://www.freecodecamp.org/news/chrome-extension-message-passing-essentials/ And it seems to be doing exactly what I'm thinking to do, but it's implemented via Manifest v2. I reimplemented it in v3 and it seems like the background script is now having troubles adding listeners to chrome.runtime.onMessage. 

I'm now contemplating to drop manifest v3 and just go with v2. Please help me make it v3 compliant. 

If you know of an extension that does messaging back and forth between an injected DOM script and popup in v3 (really mostly from the script to the popup), that would be super helpful. 

wOxxOm

unread,
Oct 31, 2023, 5:39:46 AM10/31/23
to Chromium Extensions, Bogdan Nazaruk
You can use executeScript instead of messaging, it's much simpler, see https://stackoverflow.com/a/46870005.

To use messaging, declare externally_connectable and use chrome.runtime.sendMessage as explained in the documenation - note that you can specify "<all_urls>" in externally_connectable since Chrome 107.

Bogdan Nazaruk

unread,
Oct 31, 2023, 10:56:50 AM10/31/23
to Chromium Extensions, wOxxOm, Bogdan Nazaruk
The Message from the documentation is not usable in my case because of this: " The URL pattern must contain at least a second-level domain;  ". I don't want that limitation. I need it to work everywhere as long as the extension is enabled. I mostly develop it for myself and colleagues to help out with debugging, so any extra effort in using it is unwanted. Must be as simple as possible. Essentially, one-click debugging.

I really like the SO solution. It is quite limited comparing to what I've had. I did all the parsing and the logic in the injected script, now I'll shift it to the popup, but it shouldn't be a biggie as long as I have my vars. 
The only nuance is that I'm deploying a setInterval on the page to check if my vars have arrived. I have it running about ten times with a 2 second interval. The vars I'm looking for are dependent on a third party lib, which can get quite heavy, and take quite some time to load and execute, so there's no real expectation to get the vars once the window is loaded. I effectively give the lib 20 more seconds to initialize after injecting my script.

But I guess I could just keep doing that and reading the timer variable from my popup. If the vars are not there and the timer is below 10 then display a spinner. If they're still not there after the timer is at 10, then just report no library present. It will look awkward though. I will have to do another setInterval in my popup and have it polling the injected script's setInterval. Very awkward. But well, I guess that's how the extensions API developers want it.

wOxxOm

unread,
Oct 31, 2023, 11:01:29 AM10/31/23
to Chromium Extensions, Bogdan Nazaruk, wOxxOm
As I wrote in my message you can specify "<all_urls>" in externally_connectable since Chrome 107.

Bogdan Nazaruk

unread,
Oct 31, 2023, 11:04:10 AM10/31/23
to Chromium Extensions, wOxxOm, Bogdan Nazaruk
Yes, but given how it's not in the documentation, and in fact, goes against it, can we expect it to work forever? I'm not a big fan of the extension breaking in a few versions when they decide to remove an undocumented feature.

wOxxOm

unread,
Oct 31, 2023, 11:06:56 AM10/31/23
to Chromium Extensions, Bogdan Nazaruk, wOxxOm
The change is intentional. The documentation is outdated and no one has time to fix it.

Bogdan Nazaruk

unread,
Oct 31, 2023, 11:08:04 AM10/31/23
to Chromium Extensions, wOxxOm, Bogdan Nazaruk
Oh, nice! I'll give it a shot too then. It should make my life much easier. Thanks a lot!

Patrick Kettner

unread,
Nov 3, 2023, 8:01:55 AM11/3/23
to Bogdan Nazaruk, Chromium Extensions, wOxxOm
> The change is intentional. The documentation is outdated and no one has time to fix it.

Thanks for calling it out, @wOxxOm! I pushed an update to the docs to include that.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/e052041f-cc9a-4785-af69-9df109d284d0n%40chromium.org.
Reply all
Reply to author
Forward
0 new messages