tip for the manifest v2 -> v3 conversion if listening for tabs to open

66 views
Skip to first unread message

joette cordsen

unread,
May 23, 2022, 9:56:20 PM5/23/22
to Chromium Extensions
I am not a professional developer, although my private extensions are used by over 1k people at my company, so I'm slower to catch on to things. But, I thought I'd pass on this tip in case it will help someone else. When starting the conversion from v2 to v3, I kept my background script and just made the changes needed to to make it work. After working on this conversion part time for about 6 months, I finally figured something important out.  In the background, I was waiting for a tab to open (a tab listener) and then took actions (sent messages to the tab content script) based on which tab was opened. I had random problems with the messages being lost (not acted upon by the tab) and sometimes the background appeared to not get any events when new tabs were opened. It was so random, and hard to nail down. Last week I changed the code so the background no longer initiates the action. Its a simple thing, instead of the background waiting to be notified that a new tab has opened, the tab sends a message to the background when it is loaded. The message is received, background loads its context, and takes an action depending on which content script has sent a message. Now that I had made this change, its clear to me - this is how its supposed to work. This is what happens when you try and retro fit old design into a new design, imho. Most of you know this already, but I thought I'd share it at the risk of looking really stupid? Is that the right word, I don't know - maybe just short sighted. 

George Henderson

unread,
May 23, 2022, 10:19:37 PM5/23/22
to joette cordsen, Chromium Extensions
This is a great call out. With the background as a SW it is patterned to received and respond to events. Sending messages from content scripts instead of trying to manage all of the tabs in the background simplifies a lot things. As a long time extension developer this is a great pattern to use even with v2, thanks for sharing. 

On May 23, 2022, at 8:56 PM, joette cordsen <jill...@gmail.com> wrote:

I am not a professional developer, although my private extensions are used by over 1k people at my company, so I'm slower to catch on to things. But, I thought I'd pass on this tip in case it will help someone else. When starting the conversion from v2 to v3, I kept my background script and just made the changes needed to to make it work. After working on this conversion part time for about 6 months, I finally figured something important out.  In the background, I was waiting for a tab to open (a tab listener) and then took actions (sent messages to the tab content script) based on which tab was opened. I had random problems with the messages being lost (not acted upon by the tab) and sometimes the background appeared to not get any events when new tabs were opened. It was so random, and hard to nail down. Last week I changed the code so the background no longer initiates the action. Its a simple thing, instead of the background waiting to be notified that a new tab has opened, the tab sends a message to the background when it is loaded. The message is received, background loads its context, and takes an action depending on which content script has sent a message. Now that I had made this change, its clear to me - this is how its supposed to work. This is what happens when you try and retro fit old design into a new design, imho. Most of you know this already, but I thought I'd share it at the risk of looking really stupid? Is that the right word, I don't know - maybe just short sighted. 

--
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/058f2b96-4589-4c88-b215-c5dcfe77962en%40chromium.org.

Cuyler Stuwe

unread,
May 23, 2022, 10:33:58 PM5/23/22
to George Henderson, Chromium Extensions, joette cordsen
It’s hard to say without seeing your code, but it sounds like at least some of your problem may have been the consequence of simple “race conditions”; There’s an inherent gap between “the time a tab exists” and “the time a tab has your content script fully injected and run and ready to go”. It seems that the pattern you’ve discovered is essentially something along the lines of “the thing which is less predictable should notify the thing which is more predictable when it is ready”. I would go so far as to say that this pattern is actually useful to notice in real life outside of development, too.

Reply all
Reply to author
Forward
0 new messages