/** * 工具类 */ class Utils { /** * 防抖函数 * @param {Function} func 要执行的函数 * @param {number} wait 等待时间 * @returns {Function} */ static debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } /** * 安全地获取存储数据 * @param {string} key * @returns {Promise} */ static async getStorageData(key) { try { const result = await new Promise((resolve) => { chrome.storage.local.get(key, (data) => { if (chrome.runtime.lastError) { console.warn("Storage access error:", chrome.runtime.lastError); resolve(null); return; } resolve(data[key]); }); }); return result; } catch (error) { console.warn("Storage access error:", error); return null; } } /** * 安全地设置存储数据 * @param {string} key * @param {any} value * @returns {Promise} */ static async setStorageData(key, value) { try { await new Promise((resolve) => { chrome.storage.local.set({ [key]: value }, () => { if (chrome.runtime.lastError) { console.warn("Storage access error:", chrome.runtime.lastError); resolve(); return; } resolve(); }); }); } catch (error) { console.warn("Storage access error:", error); } } }