Skip to content

Commit

Permalink
onExtensionStart: Add Firefox support (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Feb 1, 2024
1 parent 0869229 commit d24c236
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@
"sourceDir": ".built/demo"
},
"dependencies": {
"webext-detect-page": "^5.0.0"
"webext-detect-page": "^5.0.1"
}
}
6 changes: 3 additions & 3 deletions source/on-extension-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ onExtensionStart.addListener(listener);

## Compatibility

- Chrome: 112+ (MV3 only)
- Chrome: 112+ (no MV2 Event Pages support)
- Safari: 16.4
- Firefox: no
- Firefox: 115

## Permissions

Expand All @@ -34,4 +34,4 @@ onExtensionStart.addListener(listener);

- background worker
- background page
- event page
- event page (not in Chrome)
9 changes: 6 additions & 3 deletions source/on-extension-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const event = new EventTarget();
let hasRun = false;
let hasListeners = false;

// @ts-expect-error No need to load `browser` types yet
const browserStorage = globalThis.browser?.storage as typeof chrome.storage ?? chrome.storage;

async function runner() {
hasRun = true;

Expand All @@ -18,7 +21,7 @@ async function runner() {
return;
}

if (!chrome.storage?.session) {
if (!browserStorage?.session) {
if (isChrome() && chrome.runtime.getManifest().manifest_version === 2) {
console.warn('onExtensionStart is unable to determine whether it’s being run for the first time on MV2 Event Pages in Chrome. It will run the listeners anyway.');
} else {
Expand All @@ -29,12 +32,12 @@ async function runner() {
return;
}

const storage = await chrome.storage.session.get(storageKey);
const storage = await browserStorage.session.get(storageKey);
if (storageKey in storage) {
return;
}

await chrome.storage.session.set({[storageKey]: true});
await browserStorage.session.set({[storageKey]: true});
event.dispatchEvent(new Event('extension-start'));
}

Expand Down

0 comments on commit d24c236

Please sign in to comment.