Skip to content

Commit 3c0d47a

Browse files
authored
Drop webextension-polyfill (#313)
1 parent b118a6f commit 3c0d47a

File tree

10 files changed

+42
-53
lines changed

10 files changed

+42
-53
lines changed

package-lock.json

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"@sindresorhus/tsconfig": "^7.0.0",
4141
"@types/chrome": "^0.0.307",
4242
"@types/tape": "^5.8.1",
43-
"@types/webextension-polyfill": "^0.12.2",
4443
"buffer": "^6.0.3",
4544
"eslint": "^8.57.0",
4645
"eslint-config-pixiebrix": "^0.41.1",
@@ -52,8 +51,7 @@
5251
"stream-browserify": "^3.0.0",
5352
"tape": "^5.9.0",
5453
"typescript": "^5.8.2",
55-
"vitest": "^3.0.7",
56-
"webextension-polyfill": "^0.12.0"
54+
"vitest": "^3.0.7"
5755
},
5856
"targets": {
5957
"main": false,

source/logging.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* Warning: Do not use import browser-polyfill directly or indirectly */
2-
31
// .bind preserves the call location in the console
42
const debug = console.debug.bind(console, "Messenger:");
53
const warn = console.warn.bind(console, "Messenger:");

source/receiver.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import browser from "webextension-polyfill";
21
import { serializeError } from "serialize-error";
32
import { getContextName } from "webext-detect";
43

@@ -24,11 +23,11 @@ export function isMessengerMessage(message: unknown): message is Message {
2423
);
2524
}
2625

27-
// MUST NOT be `async` or Promise-returning-only
2826
function onMessageListener(
2927
message: unknown,
3028
sender: Sender,
31-
): Promise<unknown> | undefined {
29+
sendResponse: (response: unknown) => void,
30+
): true | undefined {
3231
if (!isMessengerMessage(message)) {
3332
// TODO: Add test for this eventuality: ignore unrelated messages
3433
return;
@@ -45,7 +44,17 @@ function onMessageListener(
4544
return;
4645
}
4746

48-
return handleMessage(message, sender, action);
47+
(async () => {
48+
try {
49+
sendResponse(await handleMessage(message, sender, action));
50+
} catch (error) {
51+
sendResponse({ __webextMessenger: true, error: serializeError(error) });
52+
}
53+
})();
54+
55+
// Make `sendMessage` wait for an async response. This stops other `onMessage` listeners from being called.
56+
// TODO: Just return a promise if this is ever implemented https://issues.chromium.org/issues/40753031
57+
return true;
4958
}
5059

5160
// This function can only be called when the message *will* be handled locally.
@@ -97,7 +106,6 @@ async function handleMessage(
97106
(value) => ({ value }),
98107
(error: unknown) => ({
99108
// Errors must be serialized because the stack traces are currently lost on Chrome
100-
// and https://github.com/mozilla/webextension-polyfill/issues/210
101109
error: serializeError(error),
102110
}),
103111
);
@@ -116,7 +124,7 @@ export function registerMethods(methods: Partial<MessengerMethods>): void {
116124
handlers.set(type, method as Method);
117125
}
118126

119-
browser.runtime.onMessage.addListener(onMessageListener);
127+
chrome.runtime.onMessage.addListener(onMessageListener);
120128
}
121129

122130
/** Ensure/document that the current function was called via Messenger */

source/sender.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import browser from "webextension-polyfill";
21
import pRetry from "p-retry";
32
import { isBackground } from "webext-detect";
43
import { deserializeError } from "serialize-error";
@@ -22,7 +21,6 @@ import { events } from "./events.js";
2221
const _errorNonExistingTarget =
2322
"Could not establish connection. Receiving end does not exist.";
2423

25-
// https://github.com/mozilla/webextension-polyfill/issues/384
2624
const _errorTargetClosedEarly =
2725
"A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received";
2826

@@ -166,9 +164,9 @@ async function manageMessage(
166164
throw error;
167165
}
168166

169-
if (browser.tabs && typeof target.tabId === "number") {
167+
if (chrome.tabs && typeof target.tabId === "number") {
170168
try {
171-
const tabInfo = await browser.tabs.get(target.tabId);
169+
const tabInfo = await chrome.tabs.get(target.tabId);
172170
if (tabInfo.discarded) {
173171
throw new Error(errorTabWasDiscarded);
174172
}
@@ -270,7 +268,7 @@ function messenger<
270268
"↗️ sending message to runtime",
271269
attemptLog(attemptCount),
272270
);
273-
return browser.runtime.sendMessage(
271+
return chrome.runtime.sendMessage(
274272
makeMessage(type, args, target, options),
275273
);
276274
};
@@ -279,7 +277,7 @@ function messenger<
279277
}
280278

281279
// Contexts without direct Tab access must go through background
282-
if (!browser.tabs) {
280+
if (!chrome.tabs) {
283281
return manageConnection(
284282
type,
285283
options,
@@ -291,7 +289,7 @@ function messenger<
291289
"↗️ sending message to runtime",
292290
attemptLog(attemptCount),
293291
);
294-
return browser.runtime.sendMessage(
292+
return chrome.runtime.sendMessage(
295293
makeMessage(type, args, target, options),
296294
);
297295
},
@@ -316,7 +314,7 @@ function messenger<
316314
frameId,
317315
attemptLog(attemptCount),
318316
);
319-
return browser.tabs.sendMessage(
317+
return chrome.tabs.sendMessage(
320318
tabId,
321319
makeMessage(type, args, target, options),
322320
frameId === "allFrames"

source/targetLogic.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assert, describe, test, vi } from "vitest";
22
import { getActionForMessage } from "./targetLogic.js";
3-
import { type Tabs } from "webextension-polyfill";
43
import { isContentScript, isBackground } from "webext-detect";
54

65
vi.mock("webext-detect");
@@ -14,7 +13,12 @@ const tab = {
1413
pinned: false,
1514
highlighted: true,
1615
incognito: false,
17-
} satisfies Tabs.Tab;
16+
discarded: false,
17+
frozen: false,
18+
selected: true,
19+
autoDiscardable: false,
20+
groupId: -1,
21+
} satisfies chrome.tabs.Tab;
1822

1923
const senders = {
2024
background: { page: "background" },

source/test/background/testingApi.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import browser from "webextension-polyfill";
21
import { once } from "webext-messenger/shared.js";
32

43
export async function ensureScripts(tabId: number): Promise<void> {
5-
await browser.scripting.executeScript({
4+
await chrome.scripting.executeScript({
65
target: { tabId },
76
files: ["contentscript/registration.js"],
87
});
@@ -34,7 +33,7 @@ export async function createTargets(): Promise<Targets> {
3433
let frames;
3534
while (limit--) {
3635
// eslint-disable-next-line no-await-in-loop -- It's a retry loop
37-
frames = (await browser.webNavigation.getAllFrames({
36+
frames = (await chrome.webNavigation.getAllFrames({
3837
tabId,
3938
}))!;
4039

@@ -60,15 +59,15 @@ export async function createTargets(): Promise<Targets> {
6059
}
6160

6261
const getHiddenWindow = once(async (): Promise<number> => {
63-
const { id } = await browser.windows.create({
62+
const { id } = await chrome.windows.create({
6463
focused: false,
6564
state: "minimized",
6665
});
6766
return id!;
6867
});
6968

7069
export async function openTab(url: string): Promise<number> {
71-
const tab = await browser.tabs.create({
70+
const tab = await chrome.tabs.create({
7271
windowId: await getHiddenWindow(),
7372
active: false,
7473
url,
@@ -77,9 +76,9 @@ export async function openTab(url: string): Promise<number> {
7776
}
7877

7978
export async function closeHiddenWindow(): Promise<void> {
80-
return browser.windows.remove(await getHiddenWindow());
79+
return chrome.windows.remove(await getHiddenWindow());
8180
}
8281

8382
export async function closeTab(tabId: number): Promise<void> {
84-
await browser.tabs.remove(tabId);
83+
await chrome.tabs.remove(tabId);
8584
}

source/test/contentscript/api.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import browser from "webextension-polyfill";
21
import test from "tape";
32
import { isBackground, isContentScript, isWebPage } from "webext-detect";
43
import { type PageTarget, type Target } from "webext-messenger";
@@ -184,7 +183,7 @@ async function testEveryTarget() {
184183
await closeSelf({ tabId, frameId: parentFrame });
185184
try {
186185
// Since the tab was closed, this is expected to throw
187-
t.notOk(await browser.tabs.get(tabId), "The tab should not be open");
186+
t.notOk(await chrome.tabs.get(tabId), "The tab should not be open");
188187
} catch {
189188
t.pass("The tab was closed");
190189
}

source/test/contentscript/fixtures/unrelatedMessageListener.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import browser from "webextension-polyfill";
2-
3-
browser.runtime.onMessage.addListener(
4-
(message: unknown): Promise<string> | undefined => {
1+
chrome.runtime.onMessage.addListener(
2+
(message: unknown, _sender, sendResponse): boolean | undefined => {
53
// eslint-disable-next-line @typescript-eslint/no-explicit-any
64
if ((message as any)?.type === "sleep") {
75
console.log(
86
"I’m an unrelated message listener, but I'm replying anyway to",
97
{ message },
108
);
11-
return Promise.resolve("/r/nosleep");
9+
10+
void Promise.resolve("Buonanotte").then(sendResponse);
11+
12+
return true;
1213
}
1314

1415
console.log("I’m an unrelated message listener. I’ve seen", { message });

source/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { type Runtime } from "webextension-polyfill";
21
import { type Asyncify, type ValueOf } from "type-fest";
32
import { type ErrorObject } from "serialize-error";
43

@@ -83,7 +82,7 @@ export type Message<LocalArguments extends Arguments = Arguments> = {
8382
options?: Options;
8483
};
8584

86-
export type Sender = Runtime.MessageSender & { origin?: string }; // Chrome includes the origin
85+
export type Sender = chrome.runtime.MessageSender;
8786

8887
export type MessengerMessage = Message & {
8988
/** Guarantees that a message is meant to be handled by this library */

0 commit comments

Comments
 (0)