webext-detect-page
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -20,1 +20,12 @@ export declare function disableWebextDetectPageCache(): void; | ||
export declare const isSafari: () => boolean; | ||
declare const contextNames: { | ||
readonly contentScript: () => boolean; | ||
readonly backgroundPage: () => boolean; | ||
readonly options: () => boolean; | ||
readonly devToolsPage: () => boolean; | ||
readonly extension: () => boolean; | ||
readonly web: () => boolean; | ||
}; | ||
declare type ContextName = keyof typeof contextNames; | ||
export declare function getContextName(): ContextName | 'unknown'; | ||
export {}; |
28
index.js
@@ -28,7 +28,7 @@ let cache = true; | ||
} | ||
const { options_ui } = chrome.runtime.getManifest(); | ||
if (typeof options_ui?.page !== 'string') { | ||
const { options_ui: optionsUi } = chrome.runtime.getManifest(); | ||
if (typeof optionsUi?.page !== 'string') { | ||
return false; | ||
} | ||
const url = new URL(options_ui.page, location.origin); | ||
const url = new URL(optionsUi.page, location.origin); | ||
return url.pathname === location.pathname; | ||
@@ -41,7 +41,7 @@ }); | ||
} | ||
const { devtools_page } = chrome.runtime.getManifest(); | ||
if (typeof devtools_page !== 'string') { | ||
const { devtools_page: devtoolsPage } = chrome.runtime.getManifest(); | ||
if (typeof devtoolsPage !== 'string') { | ||
return false; | ||
} | ||
const url = new URL(devtools_page, location.origin); | ||
const url = new URL(devtoolsPage, location.origin); | ||
return url.pathname === location.pathname; | ||
@@ -55,1 +55,17 @@ }); | ||
export const isSafari = () => !isChrome() && globalThis.navigator?.userAgent.includes('Safari'); | ||
const contextNames = { | ||
contentScript: isContentScript, | ||
backgroundPage: isBackgroundPage, | ||
options: isOptionsPage, | ||
devToolsPage: isDevToolsPage, | ||
extension: isExtensionContext, | ||
web: isWebPage, | ||
}; | ||
export function getContextName() { | ||
for (const [name, test] of Object.entries(contextNames)) { | ||
if (test()) { | ||
return name; | ||
} | ||
} | ||
return 'unknown'; | ||
} |
{ | ||
"name": "webext-detect-page", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "Detects where the current browser extension code is being run. Compatible with Firefox, Chrome and derivates.", | ||
@@ -40,13 +40,10 @@ "keywords": [ | ||
"webextensions" | ||
], | ||
"rules": { | ||
"@typescript-eslint/camelcase": 0 | ||
} | ||
] | ||
}, | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^1.0.2", | ||
"@types/chrome": "0.0.148", | ||
"typescript": "^4.3.5", | ||
"xo": "^0.42.0" | ||
"@sindresorhus/tsconfig": "^2.0.0", | ||
"@types/chrome": "0.0.161", | ||
"typescript": "^4.4.4", | ||
"xo": "^0.45.0" | ||
} | ||
} |
@@ -40,3 +40,3 @@ # webext-detect-page [![](https://img.shields.io/npm/v/webext-detect-page.svg)](https://www.npmjs.com/package/webext-detect-page) | ||
The functions are only ever evaluated once. This protects from future "invalidated context" errors.Read the note about [testing](#testing) if you're running this code in a tester. | ||
The functions are only ever evaluated once. This protects from future "invalidated context" errors. Read the note about [testing](#testing) if you're running this code in a tester. | ||
@@ -71,4 +71,16 @@ #### isWebPage() | ||
Return a `boolean` if it matches the current browser. They are loose detections based on the user agent that are useful when developing Web Extensions. | ||
Returns a `boolean` if it matches the current browser. They are loose detections based on the user agent that are useful when developing Web Extensions. | ||
#### getContextName() | ||
Returns the first matching context among those defined in `index.ts`, depending on the current context: | ||
- 'contentScript' | ||
- 'backgroundPage' | ||
- 'options' | ||
- 'devToolsPage' | ||
- 'extension' | ||
- 'web' | ||
- 'unknown' | ||
## Testing | ||
@@ -75,0 +87,0 @@ |
10746
98
105