((root) => { const STORAGE_KEY = 'traffic_link_history_v1'; const MAX_RECORDS = 100; function normalize(records) { if (!Array.isArray(records)) return []; return records.filter((item) => item && item.id && item.url).map((item) => ({ ...item, username: item.username || '未标记', userRole: item.userRole || 'unknown' })).slice(0, MAX_RECORDS); } function list() { try { return normalize(JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')); } catch (_) { return []; } } function write(records) { const normalized = normalize(records); try { localStorage.setItem(STORAGE_KEY, JSON.stringify(normalized)); return { ok:true, records:normalized }; } catch (_) { return { ok:false, records:normalized }; } } function add(record) { const next = list(); next.unshift({ id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, username: record.username, userRole: record.userRole, platform: record.platform || '—', pkg: record.pkg || '—', type: record.type || 'click', createdAt: new Date().toISOString(), url: record.url }); return write(next); } function remove(id) { return write(list().filter((item) => item.id !== id)); } function replace(records) { return write(records); } function clear() { return write([]); } root.LinkHistoryStore = { STORAGE_KEY, MAX_RECORDS, list, add, remove, replace, clear }; })(typeof window === 'undefined' ? globalThis : window);