c1890960ea
- 新增 entrypoints/__tests__/background.test.ts (12个测试用例) - 超长文本截断限制 (>10000字符) - Base64 内联图片拦截并返回错误提示 - 更新 parseContextMenuClick 返回 ParseResult 格式 - 更新测试匹配新的返回格式 - lint/typecheck/test 全部通过 (579个测试)
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import '../.wxt/types/imports.d.ts';
|
|
import { browser } from 'wxt/browser';
|
|
import { MessageAction, onMessage, sendMessage } from '@/utils/messages';
|
|
import { createAllContextMenus, parseContextMenuClick } from '@/utils/contextMenu';
|
|
|
|
export default defineBackground(() => {
|
|
browser.runtime.onInstalled.addListener(() => {
|
|
createAllContextMenus();
|
|
});
|
|
|
|
browser.contextMenus.onClicked.addListener(async (info, _tab) => {
|
|
const result = parseContextMenuClick(info.menuItemId as string, info);
|
|
|
|
if (!result.success || !result.data) {
|
|
if (result.error) {
|
|
console.warn('[Context Menu]', result.error);
|
|
}
|
|
return;
|
|
}
|
|
|
|
const { featureKey, payload } = result.data;
|
|
|
|
try {
|
|
const sidePanelState = await browser.storage.local.get('sidePanelOpen');
|
|
const isSidePanelOpen = sidePanelState.sidePanelOpen === true;
|
|
|
|
if (isSidePanelOpen) {
|
|
await sendMessage(MessageAction.CONTEXT_MENU_CLICKED, { featureKey, payload });
|
|
return;
|
|
}
|
|
} catch {
|
|
// sidepanel 未打开或无法通信,继续执行其他方案
|
|
}
|
|
|
|
const optionsUrl = chrome.runtime.getURL('/entrypoints/options/index.html');
|
|
const params = new URLSearchParams({ feature: featureKey, payload });
|
|
await browser.tabs.create({ url: `${optionsUrl}?${params.toString()}` });
|
|
});
|
|
|
|
// 监听扩展图标点击事件,打开侧边栏
|
|
browser.action.onClicked.addListener(async (tab) => {
|
|
if (tab.id) {
|
|
try {
|
|
await browser.sidePanel.open({ tabId: tab.id });
|
|
} catch (err) {
|
|
console.error('Failed to open side panel:', err);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 使用 @webext-core/messaging 处理消息
|
|
onMessage(MessageAction.RELOAD_TAB, async (message) => {
|
|
const { tabId, delay = 0 } = message.data;
|
|
|
|
const executeReload = () => {
|
|
browser.tabs.reload(tabId).catch((err) => {
|
|
console.error('Failed to reload tab:', err);
|
|
});
|
|
};
|
|
|
|
if (delay > 0) {
|
|
setTimeout(executeReload, delay);
|
|
} else {
|
|
executeReload();
|
|
}
|
|
|
|
return { success: true, message: '刷新请求已接收' };
|
|
});
|
|
});
|