Possible glitch with Chrome itself: chrome.tabs.onUpdated.addListener Not correctly re-injecting content script as URL updates

1,651 views
Skip to first unread message

Rory

unread,
Feb 10, 2021, 8:04:07 PM2/10/21
to Chromium Extensions
Hi there,

I'm using chrome.tabs.onUpdated.addListener to re-inject a content script into the active tab every time the URL changes.

I notice that when I move between certain URLs:

Example 1 - 
Moving from: 
to

Example 2- 
Moving from:
To:

(Normal checkout flows).

It does not properly re-inject the content script each time. I have console.logs that should run each time the content script is re-injected and these do not run.

Here is an example of the code:


I can tell it runs because the tab ID does log each time the URL in the browser changes as I move through that ulta checkout or the adidas checkout, but, the logs I have set up in the content.js do not fire off every time, so, it is not actually being reinjected.

I have tried setting up a catch to log if there is an injection problem as well, but nothing comes back.

If I hit refresh - it injects properly.

I have been trying to figure this out for days and I'm completely stumped - I have noticed other plugins have the same issue including big plugins with  major dev teams such as Honey.

Does anyone know where I'm going wrong?

Simeon Vincent

unread,
Feb 10, 2021, 9:27:04 PM2/10/21
to Rory, Chromium Extensions
The only thing that jumps to mind is that the changeInfo.status == "complete", but the nested if (if (tab.url)) is failing. You might want to add some logging to your else cases in order to see if you're hitting that scenario. If you are, please open a bug report on crbug.com/new as that may be a bug in the extension platform.

That said, this isn't really what tabs.onUpdated is meant to do. You might be better off using the Web Navigation API right now or waiting for the Scripting API to register a content script at runtime (in development, crbug.com/1054624). Web Navigation is specifically designed to give meaningful events related to navigation; your current onUpdated logic can cause a script to be injected several times on one page.

Cheers,

Simeon - @dotproto
Chrome Extensions DevRel


--
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/adfc799c-d9f6-4907-9566-cae0188be8dbn%40chromium.org.

wOxxOm

unread,
Feb 11, 2021, 9:26:46 AM2/11/21
to Chromium Extensions, Simeon Vincent, Chromium Extensions, Rory
There's no bug here. The author's code in onUpdated listener is incorrect: it assumes that every update that changes the `url` will also set `status` but that's a false assumption. The status may change independently and be reported in a separate event. Don't just assume what happens but instead use devtools to look at the actual parameters sent by API to the listener while you navigate.

The simplest solution might be to change the first condition to if ((changeInfo.status || tab.status) == 'complete')

Rory

unread,
Feb 11, 2021, 11:17:50 AM2/11/21
to Chromium Extensions, wOxxOm, Simeon Vincent, Chromium Extensions, Rory
Thank you Simeon and w0xx0m for the information. Appreciate you looking at it. Fixing my code now, thanks.
Rory

Reply all
Reply to author
Forward
0 new messages