@wxt-dev/is-background 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.mjs +38 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Aaron
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# `@wxt-dev/is-background`
|
|
2
|
+
|
|
3
|
+
Exports a getter to determine if the current JS context is the background or not.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @wxt-dev/is-background
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { isBackground } from '@wxt-dev/is-background';
|
|
15
|
+
|
|
16
|
+
isBackground(); // true | false
|
|
17
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Getter that returns if the current context is apart of an extension's
|
|
4
|
+
* background or not.
|
|
5
|
+
*
|
|
6
|
+
* > This function caches the result when called for the first time so it
|
|
7
|
+
* > doesn't have to recalculate.
|
|
8
|
+
*
|
|
9
|
+
* @returns true when in a background page or service worker.
|
|
10
|
+
*/
|
|
11
|
+
declare function isBackground(): boolean;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { isBackground };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { browser } from "@wxt-dev/browser";
|
|
2
|
+
|
|
3
|
+
//#region src/getter.ts
|
|
4
|
+
function getIsBackground() {
|
|
5
|
+
if (!browser?.runtime?.id) return false;
|
|
6
|
+
if (typeof ServiceWorkerGlobalScope !== "undefined" && self instanceof ServiceWorkerGlobalScope) return true;
|
|
7
|
+
return typeof window !== "undefined" && typeof browser.extension?.getBackgroundPage === "function" && browser.extension.getBackgroundPage() === window;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/index.ts
|
|
12
|
+
/**
|
|
13
|
+
* This module uses a lazy getter function so the logic isn't ran until it's needed.
|
|
14
|
+
*
|
|
15
|
+
* This has a few benefits:
|
|
16
|
+
* 1. Easier to mock in tests
|
|
17
|
+
* 2. Safe to import in NodeJS environments (but it should be safe to run just in-case)
|
|
18
|
+
* 3. Keeps startup fast by waiting to run the slow functions (`instanceof` or `browser.extension.getBackgroundPage`) until needed
|
|
19
|
+
*
|
|
20
|
+
* @module @wxt-dev/is-background
|
|
21
|
+
*/
|
|
22
|
+
let cached;
|
|
23
|
+
/**
|
|
24
|
+
* Getter that returns if the current context is apart of an extension's
|
|
25
|
+
* background or not.
|
|
26
|
+
*
|
|
27
|
+
* > This function caches the result when called for the first time so it
|
|
28
|
+
* > doesn't have to recalculate.
|
|
29
|
+
*
|
|
30
|
+
* @returns true when in a background page or service worker.
|
|
31
|
+
*/
|
|
32
|
+
function isBackground() {
|
|
33
|
+
if (cached == null) cached = getIsBackground();
|
|
34
|
+
return cached;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { isBackground };
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wxt-dev/is-background",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Check if the current context is the background or not.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@wxt-dev/browser": "^0.1.37"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"oxlint": "^1.43.0",
|
|
12
|
+
"publint": "^0.3.17",
|
|
13
|
+
"typescript": "^5.9.3",
|
|
14
|
+
"vitest": "^4.0.18"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/wxt-dev/wxt.git",
|
|
19
|
+
"directory": "packages/is-background"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"wxt",
|
|
23
|
+
"chrome",
|
|
24
|
+
"web",
|
|
25
|
+
"extension",
|
|
26
|
+
"is",
|
|
27
|
+
"background",
|
|
28
|
+
"script",
|
|
29
|
+
"page",
|
|
30
|
+
"service",
|
|
31
|
+
"worker"
|
|
32
|
+
],
|
|
33
|
+
"author": {
|
|
34
|
+
"name": "Aaron Klinker",
|
|
35
|
+
"email": "aaronklinker1+wxt@gmail.com"
|
|
36
|
+
},
|
|
37
|
+
"funding": "https://github.com/sponsors/wxt-dev",
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"types": "./dist/index.d.mts",
|
|
42
|
+
"module": "./dist/index.mjs",
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"types": "./dist/index.d.mts",
|
|
46
|
+
"default": "./dist/index.mjs"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "buildc -- tsdown",
|
|
51
|
+
"check": "pnpm build && check",
|
|
52
|
+
"test": "buildc --deps-only -- vitest",
|
|
53
|
+
"test:coverage": "pnpm test run --coverage"
|
|
54
|
+
}
|
|
55
|
+
}
|