w3c / webextensions

Charter and administrivia for the WebExtensions Community Group (WECG)
Other
578 stars 50 forks source link

Proposal: runtime.onSessionStart event. #509

Closed polywock closed 6 months ago

polywock commented 6 months ago

If an extension needs to initialize by moving some storage items from local storage into session storage. There's no single event that can be used to do that.

You would have to use both onInstalled and onStartup. This has a few issues.

  1. It can run multiple times. So you need to set some flag on session storage to ensure you're not initializing multiple times.
  2. It's unclear if this approach handles 100% of scenarios as the docs don't mention it.

Proposal

browser.runtime.onSessionStart event that is fired when the extension's session starts.

browser.runtime.onSessionStart.addListener(() => {
    // good place to initialize API labeled 'A' from the memo linked below. 
})

Relevant memo.

https://github.com/w3c/webextensions/blob/main/memo/persistence-of-states.md#definitions

tophf commented 6 months ago

This proposal can supersede #353 (runtime.onEnabled) and the existing runtime.onInstalled if this event receives their params (reason, previousVersion, etc.) with "enabled" added to the enum of reasons.

hanguokai commented 6 months ago

This proposal is the same as proposal #353 just with a different name. runtime.onSessionStart, runtime.onExtensionStartup or runtime.onExtensionLoaded are the same thing just different names. Chrome is supportive of both this event and onEnabled https://github.com/w3c/webextensions/issues/353#issuecomment-1593591201 , other browsers are supportive too.

polywock commented 6 months ago

Great, that's exactly it.