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

onExtensionStart - Add Firefox support #20

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading