Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update npm package webextension-polyfill to v0.12.0 #4911

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ blocks/**/.env
**/dist
**/build
**/target
**/pkg

# vscode config
**/.vscode/settings.json
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ blocks/**/.env
**/dist
**/build
**/target
**/pkg

# vscode config
**/.vscode/settings.json
Expand Down
1 change: 1 addition & 0 deletions .markdownlintignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ blocks/**/.env
**/dist
**/build
**/target
**/pkg

# vscode config
**/.vscode/settings.json
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ blocks/**/.env
**/dist
**/build
**/target
**/pkg

# vscode config
**/.vscode/settings.json
Expand Down
4 changes: 2 additions & 2 deletions apps/plugin-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"uuid": "9.0.1",
"webextension-polyfill": "0.10.0",
"webextension-polyfill": "0.12.0",
"ws": "8.17.1"
},
"devDependencies": {
Expand All @@ -65,7 +65,7 @@
"@types/lodash.debounce": "4.0.9",
"@types/react": "18.2.68",
"@types/react-dom": "18.2.25",
"@types/webextension-polyfill": "0.10.4",
"@types/webextension-polyfill": "0.12.0",
"@types/ws": "8.5.12",
"babel-loader": "9.1.2",
"babel-preset-react-app": "10.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export const InferEntitiesAction = ({
};

try {
const sourceWebPage = await (browser.tabs.sendMessage(
activeTab.id,
message,
) as Promise<GetTabContentReturn>);
const sourceWebPage = await browser.tabs.sendMessage<
GetTabContentRequest,
GetTabContentReturn
>(activeTab.id, message);

void sendMessageToBackground({
createAs,
Expand Down
15 changes: 11 additions & 4 deletions apps/plugin-browser/src/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getUser } from "../shared/get-user";
import type {
GetTabContentRequest,
GetTabContentReturn,
Message,
} from "../shared/messages";
import { isWellFormattedMessage } from "../shared/messages";
import {
clearLocalStorage,
getFromLocalStorage,
Expand Down Expand Up @@ -36,7 +36,11 @@ browser.runtime.onInstalled.addListener(({ reason }) => {
}
});

browser.runtime.onMessage.addListener(async (message: Message, sender) => {
browser.runtime.onMessage.addListener(async (message, sender) => {
if (!isWellFormattedMessage(message)) {
return `Unrecognised message format ${String(message)}`;
}

if (sender.tab) {
// We are not expecting any messages from the content script
return;
Expand All @@ -63,9 +67,12 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo) => {
setTimeout(resolve, 2_000);
});

const webPage = await (browser.tabs.sendMessage(tabId, {
const webPage = await browser.tabs.sendMessage<
GetTabContentRequest,
GetTabContentReturn
>(tabId, {
type: "get-tab-content",
} satisfies GetTabContentRequest) as Promise<GetTabContentReturn>);
});

const applicableRules = automaticInferenceConfig.rules.filter(
({ restrictToDomains }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ export const getWebsiteContent = async (urls: string[]) => {
browser.tabs.onUpdated.addListener(tabChangeListener);
});

const webPage = await (browser.tabs.sendMessage(tab.id, {
const webPage = await browser.tabs.sendMessage<
GetTabContentRequest,
GetTabContentReturn
>(tab.id, {
type: "get-tab-content",
} satisfies GetTabContentRequest) as Promise<GetTabContentReturn>);
});

webPages.push(webPage);

Expand Down
16 changes: 13 additions & 3 deletions apps/plugin-browser/src/scripts/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import browser from "webextension-polyfill";

import {
type GetTabContentReturn,
isWellFormattedMessage,
} from "../shared/messages";

/**
* @file
* Content scripts operate in the context of the webpage itself, for reading and manipulating context.
*
* They have access to a limited set of browser APIs
Expand All @@ -10,11 +16,15 @@ import browser from "webextension-polyfill";
*
* You must update the extension if you amend this file, from the extensions manager page in the browser.
*/
import type { GetTabContentReturn, Message } from "../shared/messages";

// Promise based API (see: https://github.com/mozilla/webextension-polyfill/?tab=readme-ov-file#using-the-promise-based-apis)
// Promise based API (see:
// https://github.com/mozilla/webextension-polyfill/?tab=readme-ov-file#using-the-promise-based-apis)
// eslint-disable-next-line @typescript-eslint/require-await
browser.runtime.onMessage.addListener(async (message: Message, _sender) => {
browser.runtime.onMessage.addListener(async (message) => {
if (!isWellFormattedMessage(message)) {
return `Unrecognised message format ${String(message)}`;
}

if (message.type === "get-tab-content") {
const docContent =
document.querySelector("main") ?? document.querySelector("body");
Expand Down
5 changes: 5 additions & 0 deletions apps/plugin-browser/src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ export type Message =
| InferEntitiesRequest
| CancelInferEntitiesRequest
| GetTabContentRequest;

export const isWellFormattedMessage = (message: unknown): message is Message =>
typeof message === "object" &&
message !== null &&
typeof (message as { type: unknown }).type === "string";
22 changes: 0 additions & 22 deletions patches/@types+webextension-polyfill+0.10.4.patch

This file was deleted.

16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11220,10 +11220,10 @@
dependencies:
"@types/node" "*"

"@types/webextension-polyfill@0.10.4":
version "0.10.4"
resolved "https://registry.yarnpkg.com/@types/webextension-polyfill/-/webextension-polyfill-0.10.4.tgz#09feeb2d8f04ac0a28818ade8aeeb4ab9fbafebb"
integrity sha512-pvEIqAZEbJRzaqTaWq3xlUoMWa3+euZHHz+VZHCzHWW+jOf8qLOq9wXy38U+WiPG3108SJC/wNc1X6vPC5TcjQ==
"@types/webextension-polyfill@0.12.0":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@types/webextension-polyfill/-/webextension-polyfill-0.12.0.tgz#eac57362d021d08d8d30fe0b7bae447896346aa5"
integrity sha512-0d1V0jw5wswj11ijrPoUUP+kV2pHCNKDDgQSzNdD1PK5i3vQ0eHiA1xzT5mFwaqdwatrXdgkxgC4BzCQ6C9eUw==

"@types/webidl-conversions@*":
version "7.0.3"
Expand Down Expand Up @@ -31398,10 +31398,10 @@ webcrypto-core@^1.7.4:
pvtsutils "^1.3.2"
tslib "^2.4.0"

webextension-polyfill@0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz#ccb28101c910ba8cf955f7e6a263e662d744dbb8"
integrity sha512-c5s35LgVa5tFaHhrZDnr3FpQpjj1BB+RXhLTYUxGqBVN460HkbM8TBtEqdXWbpTKfzwCcjAZVF7zXCYSKtcp9g==
webextension-polyfill@0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.12.0.tgz#f62c57d2cd42524e9fbdcee494c034cae34a3d69"
integrity sha512-97TBmpoWJEE+3nFBQ4VocyCdLKfw54rFaJ6EVQYLBCXqCIpLSZkwGgASpv4oPt9gdKCJ80RJlcmNzNn008Ag6Q==

webidl-conversions@^3.0.0:
version "3.0.1"
Expand Down
Loading