(function() { 'use strict'; // === 核心功能 === function initEnhancements() { try { const hostname = window.location.hostname; console.log(`[Enhancement] 检测到网站: ${hostname}`); if (hostname === 'matrix.tencent.com') { const key = 'fp'; const value = generateRandomHex(32); setStorageItem(key, value, 'Zhuque'); } else if (hostname === 'www.badstudent.ai') { const key = 'deviceId'; const value = generateUUID(); setStorageItem(key, value, 'BadStudentAI'); } else { console.log('[Enhancement] 当前网站不在支持列表中'); } } catch (error) { console.error('[Enhancement] 初始化增强功能时出错:', error); } } // 随机十六进制生成 function generateRandomHex(length) { try { // 优先使用crypto API if (window.crypto && crypto.getRandomValues) { const array = new Uint8Array(length); crypto.getRandomValues(array); return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join(''); } // 回退到Math.random() const hex = '0123456789abcdef'; let result = ''; for (let i = 0; i < length; i++) { result += hex[Math.floor(Math.random() * 16)]; } return result; } catch (error) { console.error('[Enhancement] 生成随机十六进制时出错:', error); return '00000000000000000000000000000000'; // 默认值 } } // UUID生成 function generateUUID() { try { // 优先使用crypto API if (window.crypto && crypto.randomUUID) { return crypto.randomUUID(); } // 回退实现 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { const r = Math.random() * 16 | 0; const v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } catch (error) { console.error('[Enhancement] 生成UUID时出错:', error); return '00000000-0000-0000-0000-000000000000'; // 默认值 } } // 存储设置 function setStorageItem(key, value, site) { try { localStorage.setItem(key, value); console.log(`[${site} Enhancement] 已启用增强功能,${key}: ${value}`); // 通知background if (chrome && chrome.runtime && chrome.runtime.sendMessage) { chrome.runtime.sendMessage({ action: 'resetComplete', site: site, key: key, value: value }).catch(error => { console.error('[Enhancement] 发送消息到background失败:', error); }); } } catch(error) { console.error('[Enhancement] 设置存储项失败:', error); } } // 页面加载完成后初始化增强功能 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initEnhancements); } else { initEnhancements(); } })();