webext-detect-page
Advanced tools
Comparing version 4.1.1 to 4.2.0
@@ -14,6 +14,12 @@ export declare function disableWebextDetectPageCache(): void; | ||
export declare const isBackgroundWorker: () => boolean; | ||
/** Indicates whether the code is being run in a persistent background page (as opposed to an Event Page or Background Worker, both of which can be unloaded by the browser) */ | ||
export declare const isPersistentBackgroundPage: () => boolean; | ||
/** Indicates whether the code is being run in an options page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ | ||
export declare const isOptionsPage: () => boolean; | ||
/** Indicates whether the code is being run in an options page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ | ||
export declare const isSidePanel: () => boolean; | ||
/** Indicates whether the code is being run in a dev tools page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` `devtools_page` field. */ | ||
export declare const isDevToolsPage: () => boolean; | ||
/** Indicates whether the code is being run in the dev tools page. Unlike `isDevToolsPage`, this works in any page that has the `chrome.devTools` API */ | ||
export declare const isDevTools: () => boolean; | ||
/** Loosely detect Firefox via user agent */ | ||
@@ -31,2 +37,4 @@ export declare const isFirefox: () => boolean; | ||
readonly options: () => boolean; | ||
readonly sidePanel: () => boolean; | ||
readonly devTools: () => boolean; | ||
readonly devToolsPage: () => boolean; | ||
@@ -33,0 +41,0 @@ readonly extension: () => boolean; |
26
index.js
@@ -49,12 +49,21 @@ let cache = true; | ||
export const isBackgroundWorker = once(() => isCurrentPathname(getManifest(3)?.background?.service_worker)); | ||
/** Indicates whether the code is being run in a persistent background page (as opposed to an Event Page or Background Worker, both of which can be unloaded by the browser) */ | ||
export const isPersistentBackgroundPage = () => isBackgroundPage() && getManifest(2)?.background?.persistent !== false; | ||
/** Indicates whether the code is being run in an options page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ | ||
export const isOptionsPage = once(() => { | ||
if (!isExtensionContext() || !chrome.runtime.getManifest) { | ||
const path = getManifest()?.options_ui?.page; | ||
if (typeof path !== 'string') { | ||
return false; | ||
} | ||
const { options_ui: optionsUi } = chrome.runtime.getManifest(); | ||
if (typeof optionsUi?.page !== 'string') { | ||
const url = new URL(path, location.origin); | ||
return url.pathname === location.pathname; | ||
}); | ||
/** Indicates whether the code is being run in an options page. This only works if the current page’s URL matches the one specified in the extension's `manifest.json` */ | ||
export const isSidePanel = once(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Not yet in @types/chrome | ||
const path = getManifest(3)?.['side_panel']?.default_path; | ||
if (typeof path !== 'string') { | ||
return false; | ||
} | ||
const url = new URL(optionsUi.page, location.origin); | ||
const url = new URL(path, location.origin); | ||
return url.pathname === location.pathname; | ||
@@ -64,6 +73,3 @@ }); | ||
export const isDevToolsPage = once(() => { | ||
if (!isExtensionContext() || !chrome.devtools) { | ||
return false; | ||
} | ||
const { devtools_page: devtoolsPage } = chrome.runtime.getManifest(); | ||
const devtoolsPage = isExtensionContext() && chrome.devtools && getManifest()?.devtools_page; | ||
if (typeof devtoolsPage !== 'string') { | ||
@@ -75,2 +81,4 @@ return false; | ||
}); | ||
/** Indicates whether the code is being run in the dev tools page. Unlike `isDevToolsPage`, this works in any page that has the `chrome.devTools` API */ | ||
export const isDevTools = () => Boolean(globalThis.chrome?.devtools); | ||
/** Loosely detect Firefox via user agent */ | ||
@@ -88,2 +96,4 @@ export const isFirefox = () => globalThis.navigator?.userAgent.includes('Firefox'); | ||
options: isOptionsPage, | ||
sidePanel: isSidePanel, | ||
devTools: isDevTools, | ||
devToolsPage: isDevToolsPage, | ||
@@ -90,0 +100,0 @@ extension: isExtensionContext, |
{ | ||
"name": "webext-detect-page", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"description": "Detects where the current browser extension code is being run. Compatible with Firefox, Chrome and derivates.", | ||
@@ -38,3 +38,3 @@ "keywords": [ | ||
"@sindresorhus/tsconfig": "^2.0.0", | ||
"@types/chrome": "^0.0.170", | ||
"@types/chrome": "^0.0.254", | ||
"typescript": "^4.5.3", | ||
@@ -41,0 +41,0 @@ "xo": "^0.55.0" |
13941
150