117 lines
3.3 KiB
JavaScript
117 lines
3.3 KiB
JavaScript
// Chrome扩展后台脚本 - 移除验证功能版
|
|
// 版本: 1.2.0
|
|
|
|
// 图标状态管理
|
|
let currentIconState = 'default';
|
|
let iconTimeout = null;
|
|
|
|
// 扩展安装时的初始化
|
|
chrome.runtime.onInstalled.addListener((details) => {
|
|
console.log('AI助手增强工具扩展已安装');
|
|
|
|
if (details.reason === 'install') {
|
|
console.log('首次安装完成');
|
|
} else if (details.reason === 'update') {
|
|
console.log('扩展已更新到版本', chrome.runtime.getManifest().version);
|
|
}
|
|
|
|
// 初始化默认状态
|
|
updateExtensionIcon('default');
|
|
});
|
|
|
|
// 监听来自content script的消息
|
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|
if (request.action === 'resetComplete') {
|
|
// 记录增强功能启用
|
|
console.log(`增强功能已启用: ${request.site}`, request);
|
|
|
|
// 更新扩展图标
|
|
updateExtensionIcon('success');
|
|
|
|
// 3秒后恢复默认图标
|
|
if (iconTimeout) clearTimeout(iconTimeout);
|
|
iconTimeout = setTimeout(() => {
|
|
updateExtensionIcon('default');
|
|
}, 3000);
|
|
|
|
sendResponse({ received: true });
|
|
return true;
|
|
}
|
|
});
|
|
|
|
// 监听标签页更新
|
|
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
|
// 只在页面加载完成时处理
|
|
if (changeInfo.status === 'complete' && tab.url) {
|
|
handleTabUrlChange(tab.url);
|
|
}
|
|
});
|
|
|
|
// 监听标签页切换
|
|
chrome.tabs.onActivated.addListener(async (activeInfo) => {
|
|
try {
|
|
const tab = await chrome.tabs.get(activeInfo.tabId);
|
|
if (tab.url) {
|
|
handleTabUrlChange(tab.url);
|
|
}
|
|
} catch (error) {
|
|
console.log('获取标签页信息失败:', error);
|
|
// 出错时重置为默认状态
|
|
updateExtensionIcon('default');
|
|
}
|
|
});
|
|
|
|
/**
|
|
* 处理标签页URL变化
|
|
* @param {string} url - 标签页URL
|
|
*/
|
|
function handleTabUrlChange(url) {
|
|
try {
|
|
const urlObj = new URL(url);
|
|
const supportedSites = ['matrix.tencent.com', 'www.badstudent.ai'];
|
|
|
|
if (supportedSites.includes(urlObj.hostname)) {
|
|
console.log(`检测到支持的网站: ${urlObj.hostname}`);
|
|
updateExtensionIcon('active');
|
|
} else {
|
|
// 只有当前状态不是默认状态时才更新
|
|
if (currentIconState !== 'default') {
|
|
updateExtensionIcon('default');
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.log('解析URL失败:', error);
|
|
// URL无效时重置为默认状态
|
|
if (currentIconState !== 'default') {
|
|
updateExtensionIcon('default');
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新扩展标题
|
|
* @param {string} state - 状态: 'default', 'active', 'success'
|
|
*/
|
|
function updateExtensionIcon(state) {
|
|
// 避免不必要的更新
|
|
if (currentIconState === state) {
|
|
return;
|
|
}
|
|
|
|
currentIconState = state;
|
|
let title;
|
|
|
|
switch (state) {
|
|
case 'active':
|
|
title = "AI增强助手 - 可用";
|
|
break;
|
|
case 'success':
|
|
title = "AI增强助手 - 功能已启用";
|
|
break;
|
|
default:
|
|
title = "AI增强助手";
|
|
}
|
|
|
|
chrome.action.setTitle({ title: title })
|
|
.catch(error => console.error('设置扩展标题失败:', error));
|
|
} |