Files
testing-tool/src/utils/contextMenu.ts
T
雨霖铃 97098c3abe refactor: 移除不必要的 export 关键字
中优先级任务:将仅内部使用的函数和类型移除 export 关键字

移除 export 的函数:
- uiPopover: showPopover
- storageCleaner: injectClearLocalStorage, injectClearSessionStorage,
  injectClearIndexedDB, injectClearCacheStorage, injectUnregisterServiceWorkers

移除 export 的类型和常量:
- base64Converter: SUPPORTED_IMAGE_TYPES, SUPPORTED_IMAGE_EXTENSIONS,
  TextToBase64Result, Base64ToBlobResult
- contextMenu: ContextMenuItemConfig, ContextMenuClickedInfo, ParseResult
- jsonFormatter: JsonMinifyResult
- jsonToToml: JsonToTomlResult
- jsonToYaml: JsonToYamlResult
- jwt: JwtHeader, JwtPayload
- qrCodeParser: QrCodeParseResult

所有 473 个测试通过,类型检查通过
2026-06-06 00:55:05 +08:00

139 lines
3.3 KiB
TypeScript

import type { PageType } from '@/types/storage';
interface ContextMenuItemConfig {
id: string;
title: string;
contexts: [`${chrome.contextMenus.ContextType}`, ...`${chrome.contextMenus.ContextType}`[]];
parentId?: string;
}
interface ContextMenuClickedInfo {
featureKey: PageType;
payload: string;
}
interface ParseResult {
success: boolean;
data?: ContextMenuClickedInfo;
error?: string;
}
const PARENT_MENU_ID = 'testing-tools-parent';
export const MAX_PAYLOAD_LENGTH = 10000;
/** 菜单项 ID 到 PageType 的映射(仅处理非常规映射) */
const MENU_ID_TO_PAGE_TYPE: Record<string, PageType> = {
'qrCode-page': 'qrCode',
'qrCode-image': 'qrCode',
};
/**
* 将菜单项 ID 转换为 PageType
* 如果存在显式映射则使用映射,否则直接使用 menuItemId
*/
function getMenuPageType(menuItemId: string): PageType {
return MENU_ID_TO_PAGE_TYPE[menuItemId] ?? (menuItemId as PageType);
}
export const CONTEXT_MENU_CONFIGS: ContextMenuItemConfig[] = [
{
id: PARENT_MENU_ID,
title: 'Testing Tools',
contexts: [chrome.contextMenus.ContextType.ALL],
},
{
id: 'jwt',
title: '🔑 解析 JWT',
contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID,
},
{
id: 'base64Converter',
title: '🔄 Base64 解码',
contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID,
},
{
id: 'textStatistics',
title: '📊 统计选中文本',
contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID,
},
{
id: 'timestamp',
title: '⏰ 转换时间戳',
contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID,
},
{
id: 'storageCleaner',
title: '🧹 清理当前网站存储',
contexts: [chrome.contextMenus.ContextType.PAGE],
parentId: PARENT_MENU_ID,
},
{
id: 'qrCode-page',
title: '🔗 网页链接转二维码',
contexts: [chrome.contextMenus.ContextType.PAGE],
parentId: PARENT_MENU_ID,
},
{
id: 'qrCode-image',
title: '🖼️ 解析图片二维码',
contexts: [chrome.contextMenus.ContextType.IMAGE],
parentId: PARENT_MENU_ID,
},
];
export function createAllContextMenus(): void {
for (const config of CONTEXT_MENU_CONFIGS) {
chrome.contextMenus.create({
id: config.id,
title: config.title,
contexts: config.contexts,
parentId: config.parentId,
});
}
}
export function parseContextMenuClick(
menuItemId: string,
info: chrome.contextMenus.OnClickData,
): ParseResult {
const featureKey = getMenuPageType(menuItemId);
// 处理图片 URL(仅 qrCode-image 菜单项,右键点击图片时)
if (menuItemId === 'qrCode-image' && info.srcUrl) {
return {
success: true,
data: { featureKey, payload: info.srcUrl },
};
}
if (info.selectionText) {
const text = info.selectionText;
if (text.length > MAX_PAYLOAD_LENGTH) {
return {
success: true,
data: { featureKey, payload: text.substring(0, MAX_PAYLOAD_LENGTH) },
};
}
return {
success: true,
data: { featureKey, payload: text },
};
}
if (info.pageUrl) {
return {
success: true,
data: { featureKey, payload: info.pageUrl },
};
}
return { success: false, error: '无法获取有效数据' };
}