New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

webext-detect-page

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webext-detect-page - npm Package Compare versions

Comparing version 3.1.0 to 4.0.0

6

index.d.ts

@@ -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;

@@ -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,

14

package.json
{
"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'

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc