镜像自地址
https://github.com/lhly/cloud-drive-renamer.git
已同步 2026-10-21 06:01:51 +08:00
132 行
4.1 KiB
TypeScript
132 行
4.1 KiB
TypeScript
import { logger } from '../utils/logger';
|
|
import { diagnosticService } from './diagnostic-service';
|
|
import { RUNTIME_MESSAGE_TYPES } from '../types/runtime-message';
|
|
|
|
/**
|
|
* Service Worker (Background Script)
|
|
* Manifest V3的后台脚本
|
|
*/
|
|
|
|
// 扩展安装事件
|
|
chrome.runtime.onInstalled.addListener(details => {
|
|
logger.info('Extension installed', details);
|
|
|
|
if (details.reason === 'install') {
|
|
// 首次安装
|
|
logger.info('First time installation');
|
|
// 可以在这里初始化默认配置
|
|
chrome.storage.local.set({
|
|
config: {
|
|
defaultPlatform: 'quark',
|
|
requestInterval: 800,
|
|
maxRetries: 3,
|
|
},
|
|
});
|
|
} else if (details.reason === 'update') {
|
|
// 扩展更新
|
|
logger.info('Extension updated', details.previousVersion);
|
|
}
|
|
});
|
|
|
|
// 监听消息
|
|
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|
|
|
switch (message.type) {
|
|
case 'GET_CONFIG':
|
|
// 获取配置
|
|
chrome.storage.local.get('config', result => {
|
|
sendResponse({ config: result.config });
|
|
});
|
|
return true; // 保持消息通道打开
|
|
|
|
case 'SAVE_CONFIG':
|
|
// 保存配置
|
|
chrome.storage.local.set({ config: message.config }, () => {
|
|
sendResponse({ success: true });
|
|
});
|
|
return true;
|
|
|
|
case 'LOG':
|
|
// 记录日志
|
|
logger.info('Log from content script:', message.data);
|
|
sendResponse({ success: true });
|
|
break;
|
|
|
|
case 'LANGUAGE_CHANGED':
|
|
// 广播语言变更到所有支持的云盘标签页
|
|
chrome.tabs.query({}, (tabs) => {
|
|
tabs.forEach((tab) => {
|
|
if (tab.id && tab.url && (
|
|
tab.url.includes('pan.quark.cn') ||
|
|
tab.url.includes('www.aliyundrive.com') ||
|
|
tab.url.includes('pan.baidu.com')
|
|
)) {
|
|
chrome.tabs.sendMessage(tab.id, message).catch((error) => {
|
|
// 忽略未注入 Content Script 的标签页的连接错误
|
|
if (!error.message?.includes('Could not establish connection')) {
|
|
logger.warn(`Failed to send language change to tab ${tab.id}:`, error);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
sendResponse({ success: true });
|
|
break;
|
|
|
|
case RUNTIME_MESSAGE_TYPES.APPEND_DIAGNOSTIC_LOG:
|
|
void diagnosticService
|
|
.appendLog(message.entry)
|
|
.then(() => sendResponse({ success: true }))
|
|
.catch((error) => {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
logger.warn('Failed to append diagnostic log:', errorMessage);
|
|
sendResponse({ success: false, error: errorMessage });
|
|
});
|
|
return true;
|
|
|
|
case RUNTIME_MESSAGE_TYPES.GET_DIAGNOSTIC_EXPORT:
|
|
void diagnosticService
|
|
.getExportPayload()
|
|
.then((payload) => sendResponse({ payload }))
|
|
.catch((error) => {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
logger.warn('Failed to build diagnostic export:', errorMessage);
|
|
sendResponse({ success: false, error: errorMessage });
|
|
});
|
|
return true;
|
|
|
|
case RUNTIME_MESSAGE_TYPES.OPEN_EXTERNAL_URL:
|
|
void diagnosticService
|
|
.openExternalUrl(message.url)
|
|
.then(() => sendResponse({ success: true }))
|
|
.catch((error) => {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
logger.warn('Failed to open external url:', errorMessage);
|
|
sendResponse({ success: false, error: errorMessage });
|
|
});
|
|
return true;
|
|
|
|
default:
|
|
logger.warn('Unknown message type:', message.type);
|
|
}
|
|
|
|
return false;
|
|
});
|
|
|
|
// 监听标签页更新
|
|
chrome.tabs.onUpdated.addListener((_tabId, changeInfo, tab) => {
|
|
if (changeInfo.status === 'complete' && tab.url) {
|
|
|
|
// 检测是否是支持的网盘平台
|
|
if (
|
|
tab.url.includes('pan.quark.cn') ||
|
|
tab.url.includes('www.aliyundrive.com') ||
|
|
tab.url.includes('pan.baidu.com')
|
|
) {
|
|
logger.info('Supported cloud drive detected:', tab.url);
|
|
}
|
|
}
|
|
});
|
|
|
|
logger.info('CloudDrive Renamer Service Worker initialized');
|