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

Avoid duplicate injections in MV3 workers #9

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
27 changes: 27 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
},
"dependencies": {
"webext-content-scripts": "^2.6.0",
"webext-detect-page": "^5.0.1",
"webext-events": "^2.2.1",
"webext-polyfill-kinda": "^1.0.2"
},
"devDependencies": {
Expand Down
11 changes: 5 additions & 6 deletions source/inject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {injectContentScript, isScriptableUrl} from 'webext-content-scripts';
import {isPersistentBackgroundPage} from 'webext-detect-page';
import chromeP from 'webext-polyfill-kinda';

const acceptableInjectionsCount = 10;
Expand Down Expand Up @@ -42,11 +43,8 @@ function onActivated({tabId}: {tabId: number}) {
}
}

const background = globalThis.chrome?.runtime.getManifest().background;
const permissions = globalThis.chrome?.runtime.getManifest().permissions;
const isPersistentBackgroundPage = background && !('service_worker' in background) && background.persistent !== false;

export default async function progressivelyInjectScript(contentScript: ContentScript) {
const permissions = globalThis.chrome?.runtime.getManifest().permissions;
if (!permissions?.includes('tabs')) {
throw new Error('webext-inject-on-install: The "tabs" permission is required');
}
Expand All @@ -60,8 +58,9 @@ export default async function progressivelyInjectScript(contentScript: ContentSc
return;
}

// TODO: Non-persistent pages support via chrome.storage.session https://github.com/fregante/webext-dynamic-content-scripts/issues/1
const singleInjection = !isPersistentBackgroundPage || scriptableTabs.length <= acceptableInjectionsCount;
// TODO: Non-persistent pages support via chrome.storage.session
// https://github.com/fregante/webext-inject-on-install/issues/4
const singleInjection = !isPersistentBackgroundPage() || scriptableTabs.length <= acceptableInjectionsCount;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by unrelated improvements

console.debug('webext-inject-on-install: Single injection?', singleInjection);

for (const tab of scriptableTabs) {
Expand Down
19 changes: 11 additions & 8 deletions source/register.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {onExtensionStart} from 'webext-events';
import progressivelyInjectScript from './inject.js';

if (globalThis.chrome && !navigator.userAgent.includes('Firefox')) {
const {content_scripts: scripts} = chrome.runtime.getManifest();
onExtensionStart.addListener(() => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core change

const {content_scripts: scripts} = chrome.runtime.getManifest();

if (!scripts?.length) {
throw new Error('webext-inject-on-install tried to inject content scripts, but no content scripts were found in the manifest.');
}
if (!scripts?.length) {
throw new Error('webext-inject-on-install tried to inject content scripts, but no content scripts were found in the manifest.');
}

console.debug('webext-inject-on-install: Found', scripts.length, 'content script(s) in the manifest.');
console.debug('webext-inject-on-install: Found', scripts.length, 'content script(s) in the manifest.');

for (const contentScript of scripts) {
void progressivelyInjectScript(contentScript);
}
for (const contentScript of scripts) {
void progressivelyInjectScript(contentScript);
}
});
}
Loading