Skip to content
Open
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
36 changes: 32 additions & 4 deletions packages/analytics/modules/analytics/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' ||
Copy link
Member

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?

Copy link
Author

(backgroundPageManifest && location.pathname.endsWith(backgroundPageManifest)) ||
(backgroundPageWXT && location.pathname.endsWith(backgroundPageWXT));
Copy link
Author

Choose a reason for hiding this comment

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

I need help over here. I couldn't find background_page property on the manifest anywhere, so I'm not sure if this internal and needs to be checked

Copy link
Member

Choose a reason for hiding this comment

The 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 background.page?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, you are right. I earlier thought that having background_page set in the manifest would generate _generated_background_page.html but it's actually when you only specify '.js' files in the scripts


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(
Expand All @@ -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();
}
Expand Down