Skip to content

Commit

Permalink
update browser plugin code for new webextension-polyfill types
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranMn committed Aug 26, 2024
1 parent 2213036 commit 067cef9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
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";

0 comments on commit 067cef9

Please sign in to comment.