Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnStartup event doesn't fire on chrome extension toggle off/on #21

Closed
eyjohn opened this issue Dec 26, 2024 · 3 comments
Closed

OnStartup event doesn't fire on chrome extension toggle off/on #21

eyjohn opened this issue Dec 26, 2024 · 3 comments

Comments

@eyjohn
Copy link

eyjohn commented Dec 26, 2024

(thanks for the module, unfortunately I wrote this logic myself before I found this one, so thought I'd share my findings)

Per https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle, and after manual testing (Chrome 129 I think), I have found that onStartup doesn't fire when you expect as its bound to session startup not extension startup.

After some research I found that

  • Installing or updating the extension triggers chrome.runtime.onInstalled and ServiceWorkerRegistration.active
  • Reloading an unpacked extension (as opposed to toggle on/off), also fires the chrome.runtime.onInstalled, (I haven't been able to confirm on published extension but I anticipate the same)
  • Toggling on/off the extension results in only the ServiceWorkerRegistration.active event.

If you only intend to use this for true "installs" then you probably want to switch to onInstalled, however I think the more common requirement is actually to inject content scripts whenever the extension installs or reloads in such a way that the extension expects that the content scripts are loaded to matched pages and reachable (or can reach the background service). In which case you probably want ServiceWorkerRegistration.active to be the main triggering event.

@fregante
Copy link
Owner

What is this issue about exactly? This package uses onExtensionStart, which is a custom event

@fregante fregante closed this as not planned Won't fix, can't repro, duplicate, stale Dec 26, 2024
@eyjohn
Copy link
Author

eyjohn commented Dec 26, 2024

ahah, my bad, I should have looked deeper, I naively assumed it was using onStartup in this line: https://github.com/fregante/webext-inject-on-install/blob/main/source/register.ts#L20, but yes I've tested it and it still all works as expected.

After digging through the code in webext-events, I see you're relying on session storage, which seems to work great, though the lifetime of session storage is poorly documented to describe this behaviour.

Anyways, I'll just leave this for your consideration then, you can potentially drop the storage requirement for ManifestV3 using the ServiceWorkerRegistration.active event, i.e.

self.addEventListener("activate", (event) => {
  // effectively the same as onExtensionStart
});

@fregante
Copy link
Owner

If it already exists, that would be great. But no one mentioned it in this thread so it might not match the expected behavior for extensions either. Please report it here for verification:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants