Files
fuwari/public/js/umami-share.js
二叉树树 4112bb23f1 update: 更新Umami API路径和响应数据处理逻辑
更新Umami API基础URL至自定义域名,并调整API路径以适配v3版本
修改统计数据获取逻辑以匹配新版API响应格式
更新文档说明以反映API变更
2025-10-11 19:42:12 +08:00

47 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function (global) {
const cacheKey = 'umami-share-cache';
const cacheTTL = 3600_000; // 1h
async function fetchShareData(baseUrl, shareId) {
const cached = localStorage.getItem(cacheKey);
if (cached) {
try {
const parsed = JSON.parse(cached);
if (Date.now() - parsed.timestamp < cacheTTL) {
return parsed.value;
}
} catch {
localStorage.removeItem(cacheKey);
}
}
const res = await fetch(`${baseUrl}/analytics/us/api/share/${shareId}`);
if (!res.ok) {
throw new Error('获取 Umami 分享信息失败');
}
const data = await res.json();
localStorage.setItem(cacheKey, JSON.stringify({ timestamp: Date.now(), value: data }));
return data;
}
/**
* 获取 Umami 分享数据websiteId、token
* 在缓存 TTL 内复用;并用全局 Promise 避免并发请求
* @param {string} baseUrl
* @param {string} shareId
* @returns {Promise<{websiteId: string, token: string}>}
*/
global.getUmamiShareData = function (baseUrl, shareId) {
if (!global.__umamiSharePromise) {
global.__umamiSharePromise = fetchShareData(baseUrl, shareId).catch((err) => {
delete global.__umamiSharePromise;
throw err;
});
}
return global.__umamiSharePromise;
};
global.clearUmamiShareCache = function () {
localStorage.removeItem(cacheKey);
delete global.__umamiSharePromise;
};
})(window);