webext-detect-page
Advanced tools
Comparing version 3.1.0 to 4.0.0
@@ -8,4 +8,8 @@ export declare function disableWebextDetectPageCache(): void; | ||
export declare const isContentScript: () => boolean; | ||
/** Indicates whether the code is being run in a background context */ | ||
export declare const isBackground: () => boolean; | ||
/** Indicates whether the code is being run in a background page */ | ||
export declare const isBackgroundPage: () => boolean; | ||
/** Indicates whether the code is being run in a background worker */ | ||
export declare const isBackgroundWorker: () => 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` */ | ||
@@ -23,3 +27,3 @@ export declare const isOptionsPage: () => boolean; | ||
readonly contentScript: () => boolean; | ||
readonly backgroundPage: () => boolean; | ||
readonly background: () => boolean; | ||
readonly options: () => boolean; | ||
@@ -26,0 +30,0 @@ readonly devToolsPage: () => boolean; |
32
index.js
@@ -5,2 +5,17 @@ let cache = true; | ||
} | ||
function isCurrentPathname(path) { | ||
if (!path) { | ||
return false; | ||
} | ||
try { | ||
const { pathname } = new URL(path, location.origin); | ||
return pathname === location.pathname; | ||
} | ||
catch { | ||
return false; | ||
} | ||
} | ||
function getManifest(_version) { | ||
return globalThis.chrome?.runtime?.getManifest?.(); | ||
} | ||
function once(function_) { | ||
@@ -21,5 +36,16 @@ let result; | ||
export const isContentScript = once(() => isExtensionContext() && isWebPage()); | ||
/** Indicates whether the code is being run in a background context */ | ||
export const isBackground = () => isBackgroundPage() || isBackgroundWorker(); | ||
/** Indicates whether the code is being run in a background page */ | ||
export const isBackgroundPage = once(() => isExtensionContext() && (location.pathname === '/_generated_background_page.html' | ||
|| chrome.extension?.getBackgroundPage?.() === globalThis.window)); | ||
export const isBackgroundPage = once(() => { | ||
const manifest = getManifest(2); | ||
if (manifest | ||
&& isCurrentPathname(manifest.background_page || manifest.background?.page)) { | ||
return true; | ||
} | ||
return Boolean(manifest?.background?.scripts | ||
&& isCurrentPathname('/_generated_background_page.html')); | ||
}); | ||
/** Indicates whether the code is being run in a background worker */ | ||
export const isBackgroundWorker = once(() => isCurrentPathname(getManifest(3)?.background?.service_worker)); | ||
/** 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` */ | ||
@@ -57,3 +83,3 @@ export const isOptionsPage = once(() => { | ||
contentScript: isContentScript, | ||
backgroundPage: isBackgroundPage, | ||
background: isBackground, | ||
options: isOptionsPage, | ||
@@ -60,0 +86,0 @@ devToolsPage: isDevToolsPage, |
{ | ||
"name": "webext-detect-page", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Detects where the current browser extension code is being run. Compatible with Firefox, Chrome and derivates.", | ||
@@ -36,14 +36,8 @@ "keywords": [ | ||
}, | ||
"xo": { | ||
"envs": [ | ||
"browser", | ||
"webextensions" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^2.0.0", | ||
"@types/chrome": "0.0.161", | ||
"typescript": "^4.4.4", | ||
"xo": "^0.45.0" | ||
"@types/chrome": "^0.0.170", | ||
"typescript": "^4.5.3", | ||
"xo": "^0.47.0" | ||
} | ||
} |
@@ -20,3 +20,3 @@ # webext-detect-page [![](https://img.shields.io/npm/v/webext-detect-page.svg)](https://www.npmjs.com/package/webext-detect-page) | ||
isContentScript, | ||
isOptionsPage | ||
isOptionsPage, | ||
} from 'webext-detect-page'; | ||
@@ -51,6 +51,14 @@ ``` | ||
#### isBackground() | ||
Returns a `boolean` that indicates whether the code is being run in a background page or background worker. | ||
#### isBackgroundPage() | ||
Returns a `boolean` that indicates whether the code is being run in a background page. | ||
Returns a `boolean` that indicates whether the code is being run in a background page (manifest v2). | ||
#### isBackgroundWorker() | ||
Returns a `boolean` that indicates whether the code is being run in a background worker (manifest v3). | ||
#### isContentScript() | ||
@@ -79,3 +87,3 @@ | ||
- 'contentScript' | ||
- 'backgroundPage' | ||
- 'background' | ||
- 'options' | ||
@@ -82,0 +90,0 @@ - 'devToolsPage' |
11993
128
113