style: biome

优化访客统计动画逻辑
This commit is contained in:
二叉树树
2026-01-01 21:39:51 +08:00
parent 82bbc7c598
commit bb4aacb07b
164 changed files with 3945 additions and 3078 deletions

View File

@@ -2,133 +2,142 @@
* Static Random Pic API
* Generated by build script
*/
(function() {
var counts = {"h":979,"v":3596};
var domain = 'https://pic.acofork.com';
// State management for session consistency
var sessionRandomH = null;
var sessionRandomV = null;
(() => {
var counts = { h: 979, v: 3596 };
var domain = "https://pic.acofork.com";
// Helper: Get random URL for a type (h or v), persistent per session
function getRandomUrl(type) {
if (!counts[type] || counts[type] === 0) return '';
// Return existing session URL if available
if (type === 'h' && sessionRandomH) return sessionRandomH;
if (type === 'v' && sessionRandomV) return sessionRandomV;
// State management for session consistency
var sessionRandomH = null;
var sessionRandomV = null;
// Generate new if not exists
var num = Math.floor(Math.random() * counts[type]) + 1;
var url = domain + '/ri/' + type + '/' + num + '.webp';
// Helper: Get random URL for a type (h or v), persistent per session
function getRandomUrl(type) {
if (!counts[type] || counts[type] === 0) return "";
// Save to session state
if (type === 'h') sessionRandomH = url;
if (type === 'v') sessionRandomV = url;
// Return existing session URL if available
if (type === "h" && sessionRandomH) return sessionRandomH;
if (type === "v" && sessionRandomV) return sessionRandomV;
return url;
}
// Generate new if not exists
var num = Math.floor(Math.random() * counts[type]) + 1;
var url = domain + "/ri/" + type + "/" + num + ".webp";
// Expose global functions
window.getRandomPicH = function() { return getRandomUrl('h'); };
window.getRandomPicV = function() { return getRandomUrl('v'); };
// Save to session state
if (type === "h") sessionRandomH = url;
if (type === "v") sessionRandomV = url;
// 1. Logic for Background (Customized based on user request)
function setRandomBackground() {
// Get random URL using the helper (Dynamic count & domain)
const bgUrl = getRandomUrl('h');
// Find the background box element
const bgBox = document.getElementById('bg-box');
if (bgBox) {
// Preload image
const img = new Image();
img.onload = function() {
bgBox.style.backgroundImage = `url('${bgUrl}')`;
bgBox.classList.add('loaded');
console.log('Random background loaded:', bgUrl);
// Set CSS variables for transparency effects
document.documentElement.style.setProperty('--card-bg', 'var(--card-bg-transparent)');
document.documentElement.style.setProperty('--float-panel-bg', 'var(--float-panel-bg-transparent)');
};
img.onerror = function() {
console.error('Failed to load background image:', bgUrl);
};
img.src = bgUrl;
} else {
// Fallback: If no #bg-box, check for data-random-bg for backward compatibility/other elements
// This keeps the generic functionality available if needed, but prioritizes the user's specific logic above.
initGenericBackgrounds();
}
}
return url;
}
// 2. Logic for Image Tags (Generic)
function initImgTags() {
var imgTags = document.getElementsByTagName('img');
for (var i = 0; i < imgTags.length; i++) {
var img = imgTags[i];
var alt = img.getAttribute('alt');
var src = img.getAttribute('src');
// Expose global functions
window.getRandomPicH = () => getRandomUrl("h");
window.getRandomPicV = () => getRandomUrl("v");
if (alt === 'random:h' || (src && src.indexOf('/random/h') !== -1)) {
img.src = getRandomUrl('h');
} else if (alt === 'random:v' || (src && src.indexOf('/random/v') !== -1)) {
img.src = getRandomUrl('v');
}
}
}
// 1. Logic for Background (Customized based on user request)
function setRandomBackground() {
// Get random URL using the helper (Dynamic count & domain)
const bgUrl = getRandomUrl("h");
// Helper for generic data-random-bg (as a backup or secondary feature)
function initGenericBackgrounds() {
var bgElements = document.querySelectorAll('[data-random-bg]');
bgElements.forEach(function(el) {
// Skip if it is the bg-box we already handled (though setRandomBackground handles #bg-box specifically)
if (el.id === 'bg-box') return;
// Find the background box element
const bgBox = document.getElementById("bg-box");
var type = el.getAttribute('data-random-bg');
if (type === 'h' || type === 'v') {
var url = getRandomUrl(type);
if (url) {
var img = new Image();
img.onload = function() {
el.style.backgroundImage = 'url("' + url + '")';
el.classList.add('loaded');
};
img.src = url;
}
}
});
}
if (bgBox) {
// Preload image
const img = new Image();
img.onload = () => {
bgBox.style.backgroundImage = `url('${bgUrl}')`;
bgBox.classList.add("loaded");
console.log("Random background loaded:", bgUrl);
function init() {
setRandomBackground();
initImgTags();
}
// Run on initial load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
// Swup integration
function setupSwup() {
if (window.swup && window.swup.hooks) {
// Register hook for content replacement
window.swup.hooks.on('content:replace', init);
console.log('Random Pic API: Registered with Swup hooks.');
}
}
// Set CSS variables for transparency effects
document.documentElement.style.setProperty(
"--card-bg",
"var(--card-bg-transparent)",
);
document.documentElement.style.setProperty(
"--float-panel-bg",
"var(--float-panel-bg-transparent)",
);
};
img.onerror = () => {
console.error("Failed to load background image:", bgUrl);
};
img.src = bgUrl;
} else {
// Fallback: If no #bg-box, check for data-random-bg for backward compatibility/other elements
// This keeps the generic functionality available if needed, but prioritizes the user's specific logic above.
initGenericBackgrounds();
}
}
if (window.swup) {
setupSwup();
} else {
document.addEventListener('swup:enable', setupSwup);
}
// 2. Logic for Image Tags (Generic)
function initImgTags() {
var imgTags = document.getElementsByTagName("img");
for (var i = 0; i < imgTags.length; i++) {
var img = imgTags[i];
var alt = img.getAttribute("alt");
var src = img.getAttribute("src");
// Legacy Swup support
document.addEventListener('swup:contentReplaced', init);
})();
if (alt === "random:h" || (src && src.indexOf("/random/h") !== -1)) {
img.src = getRandomUrl("h");
} else if (
alt === "random:v" ||
(src && src.indexOf("/random/v") !== -1)
) {
img.src = getRandomUrl("v");
}
}
}
// Helper for generic data-random-bg (as a backup or secondary feature)
function initGenericBackgrounds() {
var bgElements = document.querySelectorAll("[data-random-bg]");
bgElements.forEach((el) => {
// Skip if it is the bg-box we already handled (though setRandomBackground handles #bg-box specifically)
if (el.id === "bg-box") return;
var type = el.getAttribute("data-random-bg");
if (type === "h" || type === "v") {
var url = getRandomUrl(type);
if (url) {
var img = new Image();
img.onload = () => {
el.style.backgroundImage = 'url("' + url + '")';
el.classList.add("loaded");
};
img.src = url;
}
}
});
}
function init() {
setRandomBackground();
initImgTags();
}
// Run on initial load
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
// Swup integration
function setupSwup() {
if (window.swup && window.swup.hooks) {
// Register hook for content replacement
window.swup.hooks.on("content:replace", init);
console.log("Random Pic API: Registered with Swup hooks.");
}
}
if (window.swup) {
setupSwup();
} else {
document.addEventListener("swup:enable", setupSwup);
}
// Legacy Swup support
document.addEventListener("swup:contentReplaced", init);
})();

View File

@@ -1,91 +1,116 @@
(function (global) {
const cacheKey = 'umami-share-cache';
const cacheTTL = 3600_000; // 1h
((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}/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;
}
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}/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;
};
/**
* 获取 Umami 分享数据websiteId、token
* 在缓存 TTL 内复用;并用全局 Promise 避免并发请求
* @param {string} baseUrl
* @param {string} shareId
* @returns {Promise<{websiteId: string, token: string}>}
*/
global.getUmamiShareData = (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;
};
global.clearUmamiShareCache = () => {
localStorage.removeItem(cacheKey);
delete global.__umamiSharePromise;
};
/**
* 获取 Umami 统计数据
* 自动处理 token 获取和过期重试
* @param {string} baseUrl
* @param {string} shareId
* @param {object} queryParams
* @returns {Promise<any>}
*/
global.fetchUmamiStats = async function (baseUrl, shareId, queryParams) {
async function doFetch(isRetry = false) {
const { websiteId, token } = await global.getUmamiShareData(baseUrl, shareId);
const currentTimestamp = Date.now();
const params = new URLSearchParams({
startAt: 0,
endAt: currentTimestamp,
unit: 'hour',
timezone: queryParams.timezone || 'Asia/Shanghai',
compare: false,
...queryParams
});
const statsUrl = `${baseUrl}/api/websites/${websiteId}/stats?${params.toString()}`;
const res = await fetch(statsUrl, {
headers: {
'x-umami-share-token': token
}
});
// 初始化全局缓存 Map
if (!global.__umamiDataCache) {
global.__umamiDataCache = new Map();
}
if (!res.ok) {
if (res.status === 401 && !isRetry) {
global.clearUmamiShareCache();
return doFetch(true);
}
throw new Error('获取统计数据失败');
}
/**
* 获取 Umami 统计数据
* 自动处理 token 获取和过期重试
* @param {string} baseUrl
* @param {string} shareId
* @param {object} queryParams
* @returns {Promise<any>}
*/
global.fetchUmamiStats = async (baseUrl, shareId, queryParams) => {
// 生成缓存键baseUrl + shareId + queryParams的字符串表示
const cacheKey = `${baseUrl}|${shareId}|${JSON.stringify(queryParams)}`;
// 检查全局内存缓存
if (global.__umamiDataCache.has(cacheKey)) {
const data = global.__umamiDataCache.get(cacheKey);
// 标记数据来自缓存
return { ...data, _fromCache: true };
}
return await res.json();
}
async function doFetch(isRetry = false) {
const { websiteId, token } = await global.getUmamiShareData(
baseUrl,
shareId,
);
const currentTimestamp = Date.now();
const params = new URLSearchParams({
startAt: 0,
endAt: currentTimestamp,
unit: "hour",
timezone: queryParams.timezone || "Asia/Shanghai",
compare: false,
...queryParams,
});
return doFetch();
};
const statsUrl = `${baseUrl}/api/websites/${websiteId}/stats?${params.toString()}`;
})(window);
const res = await fetch(statsUrl, {
headers: {
"x-umami-share-token": token,
},
});
if (!res.ok) {
if (res.status === 401 && !isRetry) {
global.clearUmamiShareCache();
return doFetch(true);
}
throw new Error("获取统计数据失败");
}
const data = await res.json();
// 写入全局缓存
global.__umamiDataCache.set(cacheKey, data);
return data;
}
return doFetch();
};
})(window);