| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 | 
							- /**
 
-  * FeHelper数据统计模块
 
-  * @author fehelper
 
-  */
 
- import Awesome from './awesome.js';
 
- // 数据上报服务器地址
 
- let manifest = chrome.runtime.getManifest();
 
- let SERVER_TRACK_URL = '';
 
- if (manifest.name && manifest.name.endsWith('-Dev')) {
 
-     SERVER_TRACK_URL = 'https://chrome.fehelper.com/api/track';
 
- } else {
 
-     SERVER_TRACK_URL = 'https://localhost:3001/api/track';
 
- }
 
- // 用户ID存储键名
 
- const USER_ID_KEY = 'FH_USER_ID';
 
- // 上次使用日期存储键名
 
- const LAST_ACTIVE_DATE_KEY = 'FH_LAST_ACTIVE_DATE';
 
- // 用户日常使用数据存储键名
 
- const USER_USAGE_DATA_KEY = 'FH_USER_USAGE_DATA';
 
- // 记录background启动时间
 
- const FH_TIME_OPENED = Date.now();
 
- let Statistics = (function() {
 
-     
 
-     // 用户唯一标识
 
-     let userId = '';
 
-     
 
-     // 今天的日期字符串 YYYY-MM-DD
 
-     let todayStr = new Date().toISOString().split('T')[0];
 
-     
 
-     // 本地存储的使用数据
 
-     let usageData = {
 
-         dailyUsage: {}, // 按日期存储的使用记录
 
-         tools: {}       // 各工具的使用次数
 
-     };
 
-     
 
-     /**
 
-      * 生成唯一的用户ID
 
-      * @returns {string} 用户ID
 
-      */
 
-     const generateUserId = () => {
 
-         return 'fh_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
 
-     };
 
-     
 
-     /**
 
-      * 获取或创建用户ID
 
-      * @returns {Promise<string>} 用户ID
 
-      */
 
-     const getUserId = async () => {
 
-         if (userId) return userId;
 
-         
 
-         try {
 
-             const result = await Awesome.StorageMgr.get(USER_ID_KEY);
 
-             if (result) {
 
-                 userId = result;
 
-             } else {
 
-                 userId = generateUserId();
 
-                 await Awesome.StorageMgr.set(USER_ID_KEY, userId);
 
-             }
 
-             return userId;
 
-         } catch (error) {
 
-             console.error('获取用户ID失败:', error);
 
-             return generateUserId(); // 失败时生成临时ID
 
-         }
 
-     };
 
-     
 
-     /**
 
-      * 加载本地存储的使用数据
 
-      * @returns {Promise<void>}
 
-      */
 
-     const loadUsageData = async () => {
 
-         try {
 
-             const data = await Awesome.StorageMgr.get(USER_USAGE_DATA_KEY);
 
-             if (data) {
 
-                 usageData = JSON.parse(data);
 
-             }
 
-         } catch (error) {
 
-             console.error('加载使用数据失败:', error);
 
-         }
 
-     };
 
-     
 
-     /**
 
-      * 保存使用数据到本地存储
 
-      * @returns {Promise<void>}
 
-      */
 
-     const saveUsageData = async () => {
 
-         try {
 
-             await Awesome.StorageMgr.set(USER_USAGE_DATA_KEY, JSON.stringify(usageData));
 
-         } catch (error) {
 
-             console.error('保存使用数据失败:', error);
 
-         }
 
-     };
 
-     
 
-     /**
 
-      * 获取客户端详细信息,对标百度统计/GA/友盟
 
-      * @returns {Object}
 
-      */
 
-     const getClientInfo = () => {
 
-         const nav = navigator;
 
-         const screenInfo = window.screen;
 
-         const lang = nav.language || nav.userLanguage || '';
 
-         const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || '';
 
-         const ua = nav.userAgent;
 
-         const platform = nav.platform;
 
-         const vendor = nav.vendor;
 
-         const colorDepth = screenInfo.colorDepth;
 
-         const screenWidth = screenInfo.width;
 
-         const screenHeight = screenInfo.height;
 
-         const deviceMemory = nav.deviceMemory || '';
 
-         const hardwareConcurrency = nav.hardwareConcurrency || '';
 
-         // 网络信息
 
-         const connection = nav.connection || nav.mozConnection || nav.webkitConnection || {};
 
-         // 屏幕方向
 
-         const screenOrientation = screenInfo.orientation ? screenInfo.orientation.type : '';
 
-         // 触摸支持
 
-         const touchSupport = ('ontouchstart' in window) || (nav.maxTouchPoints > 0);
 
-         // JS堆内存(仅Chrome)
 
-         let memoryJSHeapSize = '';
 
-         if (window.performance && window.performance.memory) {
 
-             memoryJSHeapSize = window.performance.memory.jsHeapSizeLimit;
 
-         }
 
-         return {
 
-             language: lang,
 
-             timezone,
 
-             userAgent: ua,
 
-             platform,
 
-             vendor,
 
-             colorDepth,
 
-             screenWidth,
 
-             screenHeight,
 
-             deviceMemory,
 
-             hardwareConcurrency,
 
-             extensionVersion: chrome.runtime.getManifest().version,
 
-             // 新增字段
 
-             networkType: connection.effectiveType || '',
 
-             downlink: connection.downlink || '',
 
-             rtt: connection.rtt || '',
 
-             online: nav.onLine,
 
-             touchSupport,
 
-             cookieEnabled: nav.cookieEnabled,
 
-             doNotTrack: nav.doNotTrack,
 
-             appVersion: nav.appVersion,
 
-             appName: nav.appName,
 
-             product: nav.product,
 
-             vendorSub: nav.vendorSub,
 
-             screenOrientation,
 
-             memoryJSHeapSize,
 
-             timeOpened: FH_TIME_OPENED
 
-         };
 
-     };
 
-     /**
 
-      * 使用自建服务器发送事件数据
 
-      * @param {string} eventName - 事件名称
 
-      * @param {Object} params - 事件参数
 
-      */
 
-     const sendToServer = async (eventName, params = {}) => {
 
-         const uid = await getUserId();
 
-         const clientInfo = getClientInfo();
 
-         const payload = {
 
-             event: eventName,
 
-             userId: uid,
 
-             date: todayStr,
 
-             timestamp: Date.now(),
 
-             ...clientInfo,
 
-             ...params
 
-         };
 
-         try {
 
-             fetch(SERVER_TRACK_URL, {
 
-                 method: 'POST',
 
-                 body: JSON.stringify(payload),
 
-                 headers: {
 
-                     'Content-Type': 'application/json'
 
-                 },
 
-                 keepalive: true
 
-             }).catch(e => console.log('自建统计服务器发送失败:', e));
 
-         } catch (error) {
 
-             console.log('自建统计发送失败:', error);
 
-         }
 
-     };
 
-     
 
-     /**
 
-      * 记录每日活跃用户
 
-      * @returns {Promise<void>}
 
-      */
 
-     const recordDailyActiveUser = async () => {
 
-         try {
 
-             // 获取上次活跃日期
 
-             const lastActiveDate = await Awesome.StorageMgr.get(LAST_ACTIVE_DATE_KEY);
 
-             
 
-             // 如果今天还没有记录,则记录今天的活跃
 
-             if (lastActiveDate !== todayStr) {
 
-                 await Awesome.StorageMgr.set(LAST_ACTIVE_DATE_KEY, todayStr);
 
-                 
 
-                 // 确保该日期的记录存在
 
-                 if (!usageData.dailyUsage[todayStr]) {
 
-                     usageData.dailyUsage[todayStr] = {
 
-                         date: todayStr,
 
-                         tools: {}
 
-                     };
 
-                 }
 
-                 
 
-                 // 发送每日活跃记录到自建服务器
 
-                 sendToServer('daily_active_user', {
 
-                     date: todayStr
 
-                 });
 
-             }
 
-         } catch (error) {
 
-             console.error('记录日活跃用户失败:', error);
 
-         }
 
-     };
 
-     
 
-     /**
 
-      * 记录插件安装事件
 
-      */
 
-     const recordInstallation = async () => {
 
-         sendToServer('extension_installed');
 
-     };
 
-     
 
-     /**
 
-      * 记录插件更新事件
 
-      * @param {string} previousVersion - 更新前的版本
 
-      */
 
-     const recordUpdate = async (previousVersion) => {
 
-         sendToServer('extension_updated', {
 
-             previous_version: previousVersion
 
-         });
 
-     };
 
-     
 
-     /**
 
-      * 记录插件卸载事件
 
-      */
 
-     const recordUninstall = async () => {
 
-         sendToServer('extension_uninstall');
 
-     };
 
-     
 
-     /**
 
-      * 记录工具使用情况
 
-      * @param {string} toolName - 工具名称
 
-      */
 
-     const recordToolUsage = async (toolName) => {
 
-         // 确保今天的记录存在
 
-         if (!usageData.dailyUsage[todayStr]) {
 
-             usageData.dailyUsage[todayStr] = {
 
-                 date: todayStr,
 
-                 tools: {}
 
-             };
 
-         }
 
-         
 
-         // 增加工具使用计数
 
-         if (!usageData.tools[toolName]) {
 
-             usageData.tools[toolName] = 0;
 
-         }
 
-         usageData.tools[toolName]++;
 
-         
 
-         // 增加今天该工具的使用计数
 
-         if (!usageData.dailyUsage[todayStr].tools[toolName]) {
 
-             usageData.dailyUsage[todayStr].tools[toolName] = 0;
 
-         }
 
-         usageData.dailyUsage[todayStr].tools[toolName]++;
 
-         
 
-         // 保存使用数据
 
-         await saveUsageData();
 
-         
 
-         // 发送工具使用记录到自建服务器
 
-         sendToServer('tool_used', {
 
-             tool_name: toolName,
 
-             date: todayStr
 
-         });
 
-     };
 
-     
 
-     /**
 
-      * 定期发送使用摘要数据
 
-      */
 
-     const scheduleSyncStats = () => {
 
-         // 每周发送一次摘要数据
 
-         const ONE_WEEK = 7 * 24 * 60 * 60 * 1000;
 
-         setInterval(async () => {
 
-             // 发送工具使用排名
 
-             const toolRanking = Object.entries(usageData.tools)
 
-                 .sort((a, b) => b[1] - a[1])
 
-                 .slice(0, 5)
 
-                 .map(([name, count]) => ({name, count}));
 
-             
 
-             sendToServer('usage_summary', {
 
-                 top_tools: JSON.stringify(toolRanking)
 
-             });
 
-             
 
-             // 清理过旧的日期数据(保留30天数据)
 
-             const now = new Date();
 
-             const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
 
-             const thirtyDaysAgoStr = thirtyDaysAgo.toISOString().split('T')[0];
 
-             
 
-             Object.keys(usageData.dailyUsage).forEach(date => {
 
-                 if (date < thirtyDaysAgoStr) {
 
-                     delete usageData.dailyUsage[date];
 
-                 }
 
-             });
 
-             
 
-             // 保存清理后的数据
 
-             await saveUsageData();
 
-         }, ONE_WEEK);
 
-     };
 
-     
 
-     /**
 
-      * 初始化统计模块
 
-      */
 
-     const init = async () => {
 
-         await getUserId();
 
-         await loadUsageData();
 
-         await recordDailyActiveUser();
 
-         scheduleSyncStats();
 
-     };
 
-     
 
-     /**
 
-      * 获取最近使用的工具(按最近使用时间倒序,默认最近10个)
 
-      * @param {number} limit - 返回的最大数量
 
-      * @returns {Promise<string[]>} 工具名称数组
 
-      */
 
-     const getRecentUsedTools = async (limit = 10) => {
 
-         // 确保数据已加载
 
-         await loadUsageData();
 
-         // 收集所有日期,按新到旧排序
 
-         const dates = Object.keys(usageData.dailyUsage).sort((a, b) => b.localeCompare(a));
 
-         const toolSet = [];
 
-         for (const date of dates) {
 
-             const tools = Object.keys(usageData.dailyUsage[date].tools || {});
 
-             for (const tool of tools) {
 
-                 if (!toolSet.includes(tool)) {
 
-                     toolSet.push(tool);
 
-                     if (toolSet.length >= limit) {
 
-                         return toolSet;
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         return toolSet;
 
-     };
 
-     
 
-     /**
 
-      * 获取DashBoard统计数据
 
-      * @returns {Promise<Object>} 统计数据对象
 
-      */
 
-     const getDashboardData = async () => {
 
-         await loadUsageData();
 
-         // 最近10次使用的工具及时间
 
-         const recent = [];
 
-         const recentDetail = [];
 
-         const dates = Object.keys(usageData.dailyUsage).sort((a, b) => b.localeCompare(a));
 
-         for (const date of dates) {
 
-             for (const tool of Object.keys(usageData.dailyUsage[date].tools || {})) {
 
-                 if (!recent.includes(tool)) {
 
-                     recent.push(tool);
 
-                     recentDetail.push({ tool, date });
 
-                     if (recent.length >= 10) break;
 
-                 }
 
-             }
 
-             if (recent.length >= 10) break;
 
-         }
 
-         // 工具使用总次数排行
 
-         const mostUsed = Object.entries(usageData.tools)
 
-             .sort((a, b) => b[1] - a[1])
 
-             .slice(0, 10)
 
-             .map(([name, count]) => ({ name, count }));
 
-         const totalCount = Object.values(usageData.tools).reduce((a, b) => a + b, 0);
 
-         const activeDays = Object.keys(usageData.dailyUsage).length;
 
-         const allDates = Object.keys(usageData.dailyUsage).sort();
 
-         // 最近10天每日使用情况
 
-         const dailyTrend = allDates.slice(-10).map(date => ({
 
-             date,
 
-             count: Object.values(usageData.dailyUsage[date].tools || {}).reduce((a, b) => a + b, 0)
 
-         }));
 
-         // 首次和最近活跃日期
 
-         const firstDate = allDates[0] || '';
 
-         const lastDate = allDates[allDates.length - 1] || '';
 
-         // 连续活跃天数
 
-         let maxStreak = 0, curStreak = 0, prev = '';
 
-         for (let i = 0; i < allDates.length; i++) {
 
-             if (i === 0 || (new Date(allDates[i]) - new Date(prev) === 86400000)) {
 
-                 curStreak++;
 
-             } else {
 
-                 maxStreak = Math.max(maxStreak, curStreak);
 
-                 curStreak = 1;
 
-             }
 
-             prev = allDates[i];
 
-         }
 
-         maxStreak = Math.max(maxStreak, curStreak);
 
-         // 本月/本周统计
 
-         const now = new Date();
 
-         const thisMonth = now.toISOString().slice(0, 7);
 
-         const thisWeekMonday = new Date(now.setDate(now.getDate() - now.getDay() + 1)).toISOString().slice(0, 10);
 
-         let monthCount = 0, weekCount = 0;
 
-         allDates.forEach(date => {
 
-             const cnt = Object.values(usageData.dailyUsage[date].tools || {}).reduce((a, b) => a + b, 0);
 
-             if (date.startsWith(thisMonth)) monthCount += cnt;
 
-             if (date >= thisWeekMonday) weekCount += cnt;
 
-         });
 
-         // 平均每日使用次数
 
-         const avgPerDay = activeDays ? Math.round(totalCount / activeDays * 10) / 10 : 0;
 
-         // 最活跃的一天
 
-         let maxDay = { date: '', count: 0 };
 
-         allDates.forEach(date => {
 
-             const cnt = Object.values(usageData.dailyUsage[date].tools || {}).reduce((a, b) => a + b, 0);
 
-             if (cnt > maxDay.count) maxDay = { date, count: cnt };
 
-         });
 
-         // 最近未使用天数
 
-         let daysSinceLast = 0;
 
-         if (lastDate) {
 
-             const diff = Math.floor((new Date() - new Date(lastDate)) / 86400000);
 
-             daysSinceLast = diff > 0 ? diff : 0;
 
-         }
 
-         return {
 
-             recent,
 
-             recentDetail,
 
-             mostUsed,
 
-             totalCount,
 
-             activeDays,
 
-             dailyTrend,
 
-             firstDate,
 
-             lastDate,
 
-             maxStreak,
 
-             monthCount,
 
-             weekCount,
 
-             avgPerDay,
 
-             maxDay,
 
-             daysSinceLast,
 
-             allDates
 
-         };
 
-     };
 
-     
 
-     return {
 
-         init,
 
-         recordInstallation,
 
-         recordUpdate,
 
-         recordToolUsage,
 
-         getRecentUsedTools,
 
-         getDashboardData,
 
-         recordUninstall
 
-     };
 
- })();
 
- export default Statistics; 
 
 
  |