From 03f0a7ffd72ee8c49f9beb120235696b026f7cf5 Mon Sep 17 00:00:00 2001 From: Reed Hambrook Date: Sat, 23 Apr 2022 18:54:19 -0700 Subject: [PATCH] example webext-bridge --- package.json | 2 +- src/background/background.ts | 9 ++++++++- src/scripts/youtube.ts | 4 ++++ src/shim.d.ts | 7 +++++++ vite.config.ts | 1 + 5 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/shim.d.ts diff --git a/package.json b/package.json index 9be47f8..bd6468d 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "start": "vite", "dev": "vite", - "build": "vite build && web-ext build --overwrite-dest -s ./build/dist -a ./build/ && cp ./build/*.zip /mnt/e/extensions/", + "build": "vite build && web-ext build --overwrite-dest -s ./build/dist -a ./build/ && cp -f ./build/*.zip /mnt/e/extensions/", "sign": "web-ext sign --api-key $(grep WEB_EXT_API_KEY .env | cut -d '=' -f2) --api-secret $(grep WEB_EXT_API_SECRET .env | cut -d '=' -f2) -s ./build/dist -a ./build/", "serve": "vite preview" }, diff --git a/src/background/background.ts b/src/background/background.ts index de1d59b..d6f6558 100644 --- a/src/background/background.ts +++ b/src/background/background.ts @@ -1,3 +1,10 @@ /** * The background script for the extension is responsible for sending requests to the servers using fetch. - */ \ No newline at end of file + */ +// Inside background script +import { onMessage } from 'webext-bridge' + +onMessage('test', ({ data: { a, b } }) => { + console.log(a, b); + return { result: true }; +}); \ No newline at end of file diff --git a/src/scripts/youtube.ts b/src/scripts/youtube.ts index c9a0e8e..5cfff7e 100644 --- a/src/scripts/youtube.ts +++ b/src/scripts/youtube.ts @@ -1,3 +1,7 @@ /** * Responsible for parsing youtube for specific data, e.g. urls, tags, comments, etc. */ + +import { sendMessage } from 'webext-bridge'; + +sendMessage("test", { a: "speed", b: 2 }).then(console.log); diff --git a/src/shim.d.ts b/src/shim.d.ts new file mode 100644 index 0000000..515e513 --- /dev/null +++ b/src/shim.d.ts @@ -0,0 +1,7 @@ +import { ProtocolWithReturn } from 'webext-bridge' + +declare module 'webext-bridge' { + export interface ProtocolMap { + test: ProtocolWithReturn<{ a: string, b: number }, { result: boolean }> + } +} \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index b8eca8d..b092368 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -17,6 +17,7 @@ export default defineConfig({ }), ], build: { + minify: 'terser', outDir: path.resolve(__dirname, "build/dist"), target: 'esnext', polyfillDynamicImport: false,