Skip to content

Commit

Permalink
onExtensionStart - Run even when uncertain
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Feb 1, 2024
1 parent f5db331 commit 264630a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@
},
"webExt": {
"sourceDir": ".built/demo"
},
"dependencies": {
"webext-detect-page": "^5.0.0"
}
}
19 changes: 19 additions & 0 deletions source/on-extension-start.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {isChrome, isPersistentBackgroundPage} from 'webext-detect-page';

const storageKey = '__webext-events__startup';
const event = new EventTarget();
let hasRun = false;
Expand All @@ -10,6 +12,23 @@ async function runner() {
return;
}

if (isPersistentBackgroundPage()) {
// It's certainly the first and only time
event.dispatchEvent(new Event('extension-start'));
return;
}

if (!chrome.storage?.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 {
console.warn('onExtensionStart is unable to determine whether it’s being run for the first time without the `storage` permission. It will run the listeners anyway');
}

event.dispatchEvent(new Event('extension-start'));
return;
}

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

0 comments on commit 264630a

Please sign in to comment.