diff --git a/package-lock.json b/package-lock.json index 956f391..e7d3032 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,8 @@ "license": "MIT", "dependencies": { "webext-content-scripts": "^2.6.0", + "webext-detect-page": "^5.0.1", + "webext-events": "^2.2.2", "webext-polyfill-kinda": "^1.0.2" }, "devDependencies": { @@ -8692,6 +8694,31 @@ "url": "https://github.com/sponsors/fregante" } }, + "node_modules/webext-detect-page": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/webext-detect-page/-/webext-detect-page-5.0.1.tgz", + "integrity": "sha512-HizogkTmviA5qA1yODwewzz4ETSc+N9bYrK6pEVIAP2kAG139Sg+3DOJixRnFYl2gFVZn4PBReDZhgmFOEVbeg==", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/fregante" + } + }, + "node_modules/webext-events": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/webext-events/-/webext-events-2.2.2.tgz", + "integrity": "sha512-ZJxCIzhkj9MhqtYeN6nPIuH3N/qi+prcRNL0cXRx0JohnSuhlp4eZWuYBHAS+JGlq/BUnbB82ojZ62QOwR/caQ==", + "dependencies": { + "webext-detect-page": "^5.0.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/fregante" + } + }, "node_modules/webext-patterns": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/webext-patterns/-/webext-patterns-1.3.0.tgz", diff --git a/package.json b/package.json index 952433c..72cef12 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,8 @@ }, "dependencies": { "webext-content-scripts": "^2.6.0", + "webext-detect-page": "^5.0.1", + "webext-events": "^2.2.2", "webext-polyfill-kinda": "^1.0.2" }, "devDependencies": { diff --git a/source/inject.ts b/source/inject.ts index 29a1a66..7f3ce56 100644 --- a/source/inject.ts +++ b/source/inject.ts @@ -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; @@ -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'); } @@ -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; console.debug('webext-inject-on-install: Single injection?', singleInjection); for (const tab of scriptableTabs) { diff --git a/source/register.ts b/source/register.ts index 51eaf06..cfe4f75 100644 --- a/source/register.ts +++ b/source/register.ts @@ -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(() => { + 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); + } + }); }