diff --git a/README.md b/README.md index ef72fd5..c18f232 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,3 @@ -# Vue 3 + TypeScript + Vite - -This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 ` diff --git a/src/message.ts b/src/message.ts new file mode 100644 index 0000000..ef7235e --- /dev/null +++ b/src/message.ts @@ -0,0 +1,24 @@ +export const { sendTriggered, watchTriggered } = getMessenger("triggered"); + +type MessageType = + | "wrongPage" // current page doesn't match https://book.douban.com/subject/27594044/ + | "ready" // content script has collected book information + | "triggered" // user triggers the save book button + | "failed" // background script fails to send the book to notion + | "success" // background script sends the book to notion successfully + | "duplicate"; // background scripts detects a duplicate book in notion + +function getMessenger(type: MessageType) { + return { + ["send" + type.toUpperCase()]: (data: T) => { + chrome.runtime.sendMessage({ type, data }); + }, + ["watch" + type.toUpperCase()]: (action: (data: T) => void) => { + chrome.runtime.onMessage.addListener((message) => { + if (message.type === type) { + action(message.data); + } + }); + }, + }; +} diff --git a/src/types.ts b/src/types.ts index d069053..67f09ae 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,3 +12,8 @@ export interface Book { ratingCount: number; // 评价人数 cover: string; } + +export interface BackgroundRes { + success: boolean; + error?: Error; +} diff --git a/tsconfig.json b/tsconfig.json index 5ff65d3..3ccd35b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,12 +14,6 @@ "skipLibCheck": true, "noEmit": true }, - "include": [ - "src/**/*.ts", - "src/**/*.d.ts", - "src/**/*.tsx", - "src/**/*.vue", - "src/**.ts" - ], + "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"], "references": [{ "path": "./tsconfig.vite.json" }] }