1
This commit is contained in:
33
src/background/index.ts
Normal file
33
src/background/index.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { handleBackgroundCommand, handleInstalled, handleStartup, handleWindowRemoved } from './service';
|
||||
import type { BackgroundCommand } from './types';
|
||||
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
void handleInstalled();
|
||||
});
|
||||
|
||||
chrome.runtime.onStartup.addListener(() => {
|
||||
void handleStartup();
|
||||
});
|
||||
|
||||
chrome.runtime.onMessage.addListener((message: BackgroundCommand, _sender, sendResponse) => {
|
||||
void handleBackgroundMessage(message, sendResponse);
|
||||
return true;
|
||||
});
|
||||
|
||||
chrome.windows.onRemoved.addListener((windowId) => {
|
||||
void handleWindowRemoved(windowId);
|
||||
});
|
||||
|
||||
/** 统一包装后台消息处理,确保异步错误能回给调用方。 */
|
||||
async function handleBackgroundMessage(
|
||||
message: BackgroundCommand,
|
||||
sendResponse: (response?: unknown) => void,
|
||||
) {
|
||||
try {
|
||||
const result = await handleBackgroundCommand(message);
|
||||
sendResponse(result);
|
||||
} catch (error: unknown) {
|
||||
const messageText = error instanceof Error ? error.message : 'Unknown error';
|
||||
sendResponse({ ok: false, error: messageText });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user