-
-
Notifications
You must be signed in to change notification settings - Fork 368
fix: Improve background script detection logic for analytics package #1807 #1808
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,37 @@ import { browser } from '@wxt-dev/browser'; | |
|
||
const ANALYTICS_PORT = '@wxt-dev/analytics'; | ||
|
||
// Based on https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/background#browser_support | ||
function isBackgroundScript(): boolean { | ||
if (!browser?.runtime?.getManifest || !location.protocol.endsWith('extension:')) { | ||
return false; | ||
} | ||
|
||
const { background, background_page: backgroundPageWXT, manifest_version } = browser.runtime.getManifest(); | ||
// Misconfiguration or Non-WXT Context Check | ||
// Force check page attribute for possible configurations | ||
const backgroundPageManifest = (background as any)?.page; | ||
|
||
const backgroundPageOrScriptsCheck = | ||
location.pathname === '/_generated_background_page.html' || | ||
(backgroundPageManifest && location.pathname.endsWith(backgroundPageManifest)) || | ||
(backgroundPageWXT && location.pathname.endsWith(backgroundPageWXT)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need help over here. I couldn't find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure where you found this property in the first place... Isn't it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right. I earlier thought that having |
||
|
||
if (manifest_version === 3) { | ||
try { | ||
return self instanceof ServiceWorkerGlobalScope && | ||
// Check if called inside manifest defined service worker file | ||
background?.service_worker !== undefined && location.pathname.endsWith(background.service_worker); | ||
// oxlint-disable-next-line no-unused-vars | ||
} catch (e) { | ||
// Return page or scripts check for non-service worker contexts | ||
return backgroundPageOrScriptsCheck; | ||
} | ||
} | ||
|
||
return backgroundPageOrScriptsCheck; | ||
} | ||
|
||
export function createAnalytics(config?: AnalyticsConfig): Analytics { | ||
if (!browser?.runtime?.id) | ||
throw Error( | ||
|
@@ -24,10 +55,7 @@ export function createAnalytics(config?: AnalyticsConfig): Analytics { | |
); | ||
} | ||
|
||
// TODO: This only works for standard WXT extensions, add a more generic | ||
// background script detector that works with non-WXT projects. | ||
if (location.pathname === '/background.js') | ||
return createBackgroundAnalytics(config); | ||
if (isBackgroundScript()) return createBackgroundAnalytics(config); | ||
|
||
return createFrontendAnalytics(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this string coming from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found it in source code of browsers