123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /**
- * 工具类
- */
- 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);
- }
- }
- }
|