I'm working on solving a tricky problem that results in duplicate content script injections and curious if anyone in this group has insight or suggestions.
We inject our content script programmatically using `exectureScript` from the background page in response to navigation events. The intended flow goes something like this:
1. user navigates to supported url
2. webNavigation event fires in background script
3. background script injects content script
The problem arises when users are quickly redirected through multiple supported urls (e.g. saml flow). We have code in place to try and prevent duplicate injections but in the course of rapid redirects, the navigation listener fires twice. Here's the central problem – the content script is sizable and it takes a long time for the browser to parse all the JS. So the content script that was meant to be injected into "foobar/login?redirect" actually gets injected into "foobar/home." Then, the second webNavigation event also triggers in "foobar/home" – now that the navigation sequence is over we end up with duplicate scripts.
Have others run into this issue? Do you have any strategies to recommend?
Another approach that I've considered is sending a message from background to content after the navigation event to check whether there is already a content script there. The problem with this solution is that when there is NOT a content script in the host page, the callback for `sendMessage` never fires because there is no response. So we need wait an arbitrary length of time to see if the content script will respond before injecting. By the time the arbitrary time has elapsed, the host page may have navigated again or the service worker may fall asleep...
Anyway, curious to hear if others have encountered similar issues.