You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/guide/essentials/extension-apis.md
+12-35
Original file line number
Diff line number
Diff line change
@@ -4,52 +4,29 @@
4
4
5
5
Different browsers provide different global variables for accessing the extension APIs (chrome provides `chrome`, firefox provides `browser`, etc).
6
6
7
-
WXT simplifies this - always use `browser`:
7
+
WXT merges these two into a unified API accessed through the `browser` variable.
8
8
9
9
```ts
10
+
import { browser } from'wxt/browser';
11
+
10
12
browser.action.onClicked.addListener(() => {
11
13
// ...
12
14
});
13
15
```
14
16
15
-
Other than that, refer to Chrome and Mozilla's documentation for how to use specific APIs. Everything a normal extension can do, WXT can do as well, just via `browser` instead of `chrome`.
16
-
17
-
## Webextension Polyfill
18
-
19
-
> Since `v0.1.0`
20
-
21
-
By default, WXT uses the [`webextension-polyfill` by Mozilla](https://www.npmjs.com/package/webextension-polyfill) to make the extension API consistent between browsers.
22
-
23
-
To access types, you should import the relevant namespace from `wxt/browser`:
24
-
25
-
```ts
26
-
import { Runtime } from'wxt/browser';
27
-
28
-
function handleMessage(message:any, sender:Runtime.Sender) {
29
-
// ...
30
-
}
31
-
```
32
-
33
-
### Disabling the polyfill
34
-
35
-
> Since `v0.19.0`
36
-
37
-
After the release of MV3 and Chrome's official deprecation of MV2 in June 2024, the polyfill isn't really doing anything useful anymore.
17
+
:::tip
18
+
With auto-imports enabled, you don't even need to import this variable from `wxt/browser`!
19
+
:::
38
20
39
-
You can disable it with a single line:
21
+
The `browser` variable WXT provides is a simple export of the `browser` or `chrome` globals provided by the browser at runtime:
40
22
41
-
```ts
42
-
// wxt.config.ts
43
-
exportdefaultdefineConfig({
44
-
extensionApi: 'chrome',
45
-
});
46
-
```
23
+
<<< @/../packages/wxt/src/browser.ts#snippet
47
24
48
-
This will change `wxt/browser` to simply export the `browser` or `chrome` globals based on browser at runtime:
25
+
This means you can use the promise-style API for both MV2 and MV3, and it will work across all browsers (Chromium, Firefox, Safari, etc).
Accessing types is a little different with the polyfill disabled. They do not need to be imported; they're available on the `browser` object itself:
29
+
All types can be accessed via WXT's `browser` object:
53
30
54
31
```ts
55
32
function handleMessage(message:any, sender:browser.runtime.Sender) {
@@ -59,7 +36,7 @@ function handleMessage(message: any, sender: browser.runtime.Sender) {
59
36
60
37
## Feature Detection
61
38
62
-
Depending on the manifest versionand browser, some APIs are not available at runtime. If an API is not available, it will be `undefined`.
39
+
Depending on the manifest version, browser, and permissions, some APIs are not available at runtime. If an API is not available, it will be `undefined`.
63
40
64
41
:::warning
65
42
Types will not help you here. The types WXT provides for `browser` assume all APIs exist. You are responsible for knowing whether an API is available or not.
0 commit comments