Skip to content

Commit 5e859ef

Browse files
committed
refactor: Convert extension source files to TypeScript
1 parent 486f1bf commit 5e859ef

File tree

12 files changed

+177
-80
lines changed

12 files changed

+177
-80
lines changed

packages/ocap-skunkworks/.eslintrc.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ module.exports = {
55
{
66
files: ['src/extension/**/*.js'],
77
globals: { chrome: 'readonly', clients: 'readonly' },
8-
rules: {
9-
'jsdoc/require-jsdoc': 'off',
10-
},
118
},
129

1310
{

packages/ocap-skunkworks/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"scripts": {
1414
"build": "ts-bridge --project tsconfig.build.json --clean",
1515
"build:docs": "typedoc",
16-
"build:extension": "vite build -c vite.config.extension.mts",
16+
"build:extension": "yarn check:types:extension && vite build -c vite.config.extension.mts",
1717
"build:library": "ts-bridge --project tsconfig.build.json --clean",
18+
"check:types:extension": "tsc --project src/extension/tsconfig.json",
1819
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/ocap-skunkworks",
1920
"publish:preview": "yarn npm publish --tag preview",
2021
"start": "vite -c vite.config.extension.mts",
@@ -29,6 +30,7 @@
2930
"@metamask/auto-changelog": "^3.4.4",
3031
"@ts-bridge/cli": "^0.1.4",
3132
"@ts-bridge/shims": "^0.1.1",
33+
"@types/chrome": "^0.0.268",
3234
"@types/jest": "^28.1.6",
3335
"deepmerge": "^4.3.1",
3436
"jest": "^28.1.3",

packages/ocap-skunkworks/src/extension/background.js

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { ExtensionMessage } from './shared';
2+
import { makeHandledCallback } from './shared';
3+
4+
const OFFSCREEN_DOCUMENT_PATH = '/offscreen.html';
5+
6+
// Send
7+
chrome.action.onClicked.addListener(() => {
8+
sendMessage('greetings', { name: 'Kernel' }).catch(console.error);
9+
});
10+
11+
/**
12+
* Send a message to the offscreen document.
13+
* @param type - The message type.
14+
* @param data - The message data.
15+
* @param data.name - The name to include in the message.
16+
*/
17+
async function sendMessage(type: string, data: { name: string }) {
18+
await provideOffScreenDocument();
19+
20+
await chrome.runtime.sendMessage({
21+
type,
22+
target: 'offscreen',
23+
data,
24+
});
25+
}
26+
27+
/**
28+
* Create the offscreen document if it doesn't already exist.
29+
*/
30+
async function provideOffScreenDocument() {
31+
if (!(await chrome.offscreen.hasDocument())) {
32+
await chrome.offscreen.createDocument({
33+
url: OFFSCREEN_DOCUMENT_PATH,
34+
reasons: [chrome.offscreen.Reason.IFRAME_SCRIPTING],
35+
justification: `Surely you won't object to our capabilities?`,
36+
});
37+
}
38+
}
39+
40+
// Receive
41+
chrome.runtime.onMessage.addListener(makeHandledCallback(handleMessage));
42+
43+
/**
44+
* Receive a message from the offscreen document.
45+
* @param message - The message to handle.
46+
*/
47+
async function handleMessage(message: ExtensionMessage<string>) {
48+
if (message.target !== 'background') {
49+
return;
50+
}
51+
52+
switch (message.type) {
53+
case 'salutations':
54+
console.log(message.data);
55+
await closeOffscreenDocument();
56+
break;
57+
default:
58+
console.error(`Received unexpected message type: "${message.type}"`);
59+
}
60+
}
61+
62+
/**
63+
* Close the offscreen document if it exists.
64+
*/
65+
async function closeOffscreenDocument() {
66+
if (!(await chrome.offscreen.hasDocument())) {
67+
return;
68+
}
69+
await chrome.offscreen.closeDocument();
70+
}

packages/ocap-skunkworks/src/extension/lockdown.mjs

Whitespace-only changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<!DOCTYPE html>
2-
<script src="offscreen.js" type="module"></script>
2+
<script src="offscreen.ts" type="module"></script>

packages/ocap-skunkworks/src/extension/offscreen.js

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { ExtensionMessage } from './shared';
2+
import { makeHandledCallback } from './shared';
3+
4+
chrome.runtime.onMessage.addListener(makeHandledCallback(handleMessage));
5+
6+
/**
7+
* Handle a message from the background script.
8+
* @param message - The message to handle.
9+
*/
10+
async function handleMessage(message: ExtensionMessage<{ name: string }>) {
11+
if (message.target !== 'offscreen') {
12+
return;
13+
}
14+
15+
switch (message.type) {
16+
case 'greetings':
17+
await reply('salutations', `Good day to you, ${message.data.name}!`);
18+
break;
19+
default:
20+
console.error(`Received unexpected message type: "${message.type}"`);
21+
}
22+
}
23+
24+
/**
25+
* Reply to the background script.
26+
* @param type - The message type.
27+
* @param data - The message data.
28+
*/
29+
async function reply(type: string, data: string) {
30+
await chrome.runtime.sendMessage({
31+
data,
32+
target: 'background',
33+
type,
34+
});
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export type ExtensionMessage<
2+
Data extends string | unknown[] | Record<string, unknown>,
3+
> = {
4+
type: string;
5+
target: 'background' | 'offscreen';
6+
data: Data;
7+
};
8+
9+
/**
10+
* Wrap an async callback to ensure any errors are re-thrown synchronously.
11+
* @param callback - The async callback to wrap.
12+
* @returns The wrapped callback.
13+
*/
14+
export const makeHandledCallback = <Args extends unknown[]>(
15+
callback: (...args: Args) => Promise<void>,
16+
) => {
17+
return (...args: Args): void => {
18+
// eslint-disable-next-line n/no-callback-literal, n/callback-return
19+
callback(...args).catch((error: Error) => {
20+
throw error;
21+
});
22+
};
23+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"isolatedModules": true,
5+
"lib": ["DOM", "ES2020"],
6+
"noEmit": true,
7+
"types": ["chrome"]
8+
},
9+
"include": ["./**/*.ts"]
10+
}

0 commit comments

Comments
 (0)