// ==UserScript== // @name 已访问链接样式修改 - 控制面板 // @namespace https://scriptcat.org/zh-CN/search // @version 2026.5.7 // @description 自定义已访问链接样式 // @tag 链接 工具 link tools // @license MIT // @author zzz妄炁 & AI // @match *://*/* // @run-at document-idle // @grant GM_addStyle // @grant GM_registerMenuCommand // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @compatible chrome // @compatible firefox // @compatible edge // @supportURL https://scriptcat.org/scripts/code/2822 // @icon https://img.soogif.com/6QlDQPIFsYakXFhmBt1a02vNk9G5ecYf.gif // ==/UserScript== (function() { 'use strict'; const underlineLabels = { 'solid': '实线', 'dashed': '虚线', 'wavy': '波浪线', 'double': '双线', 'dotted': '点状线', 'none': '无' }; const settings = { color: GM_getValue('linkColor', '#FF0000'), underline: GM_getValue('underlineType', 'solid'), weight: GM_getValue('linkWeight', '400'), glassAlpha: GM_getValue('glassAlpha', 0.7) }; let visitedLinks = new Set(); let observer = null; let styleElement = null; let saveTimer = null; let panelOverlay = null; let touchStartX = 0, touchStartY = 0; let touchHandled = false; let touchHandledTimer = null; // ========== 工具函数 ========== function initVisitedLinks() { try { const stored = GM_getValue('visitedLinks', '[]'); visitedLinks = new Set(JSON.parse(stored)); } catch (e) { visitedLinks = new Set(); } } function forceSave() { try { GM_setValue('visitedLinks', JSON.stringify([...visitedLinks])); } catch (e) {} } function saveVisitedLinks() { clearTimeout(saveTimer); saveTimer = setTimeout(forceSave, 500); } function isClickableElement(el) { if (!el || el.nodeType !== 1) return false; if (el.closest('.glass-overlay, .glass-card, #visited-links-style, #glass-dialog-styles')) return false; const tag = el.tagName.toUpperCase(); if (['A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'IMG'].includes(tag)) return true; if (el.getAttribute('onclick') || el.hasAttribute('download') || el.getAttribute('role') === 'button' || el.getAttribute('role') === 'link' || el.hasAttribute('data-href') || el.hasAttribute('data-url')) return true; return false; } function applyVisitedStyleToElement(el) { if (!el || !el.style) return; el.style.setProperty('color', settings.color, 'important'); el.style.setProperty('text-decoration', 'underline ' + settings.underline, 'important'); el.style.setProperty('font-weight', settings.weight, 'important'); el.classList.add('visited-link'); } function markLinkAsVisited(link) { if (!link) return; if (link.tagName === 'A' && link.href) { const href = link.href; if (visitedLinks.has(href)) return; visitedLinks.add(href); applyVisitedStyleToElement(link); saveVisitedLinks(); } else if (isClickableElement(link)) { applyVisitedStyleToElement(link); } } function refreshAllVisitedStyles() { document.querySelectorAll('.visited-link').forEach(el => { applyVisitedStyleToElement(el); }); } function updateStyles() { const css = ` a[href].visited-link, a.visited-link[href], .visited-link { color: ${settings.color} !important; text-decoration: underline ${settings.underline} !important; font-weight: ${settings.weight} !important; } `; if (!styleElement) { styleElement = document.createElement('style'); styleElement.id = 'visited-links-style'; (document.head || document.documentElement).appendChild(styleElement); } styleElement.textContent = css; refreshAllVisitedStyles(); } function hasValidBody() { return !!(document.body && document.body.childNodes.length > 0); } function applyVisitedStyles() { if (!hasValidBody()) return; document.querySelectorAll('a[href]').forEach(link => { if (visitedLinks.has(link.href)) { applyVisitedStyleToElement(link); } }); } function applyStylesToNewNodes(nodes) { for (const node of nodes) { if (node.nodeType !== 1) continue; if (node.tagName === 'A' && node.href && visitedLinks.has(node.href)) { applyVisitedStyleToElement(node); } if (node.querySelectorAll) { node.querySelectorAll('a[href]').forEach(link => { if (visitedLinks.has(link.href)) applyVisitedStyleToElement(link); }); } } } function handleInteraction(target) { if (!target || !target.closest) return; if (target.closest('#glass-dialog-styles, .glass-overlay, .glass-card, #visited-links-style')) return; const link = target.closest('a'); if (link && link.href) { markLinkAsVisited(link); return; } const clickable = target.closest('a, button, img, [role="button"], [role="link"], [onclick], [download], [data-href], [data-url]'); if (clickable && isClickableElement(clickable)) { markLinkAsVisited(clickable); } } // ========== 取色器 (HSV) ========== function hsvToHex(h, s, v) { const i = Math.floor(h / 60); const f = h / 60 - i; const p = v * (1 - s); const q = v * (1 - f * s); const t = v * (1 - (1 - f) * s); let r, g, b; switch (i % 6) { case 0: r = v; g = t; b = p; break; case 1: r = q; g = v; b = p; break; case 2: r = p; g = v; b = t; break; case 3: r = p; g = q; b = v; break; case 4: r = t; g = p; b = v; break; case 5: r = v; g = p; b = q; break; } const toHex = (x) => { const hex = Math.round(x * 255).toString(16); return hex.length === 1 ? '0' + hex : hex; }; return `#${toHex(r)}${toHex(g)}${toHex(b)}`; } function hexToHsv(hex) { hex = hex.replace('#', ''); const r = parseInt(hex.substring(0, 2), 16) / 255; const g = parseInt(hex.substring(2, 4), 16) / 255; const b = parseInt(hex.substring(4, 6), 16) / 255; const max = Math.max(r, g, b); const min = Math.min(r, g, b); const delta = max - min; let h = 0, s, v = max; if (delta === 0) { h = 0; s = 0; } else { s = delta / max; if (r === max) h = ((g - b) / delta) % 6; else if (g === max) h = (b - r) / delta + 2; else h = (r - g) / delta + 4; h = Math.round(h * 60); if (h < 0) h += 360; } return { h, s, v }; } // ========== 液态玻璃全局样式 ========== function injectGlobalDialogStyles() { const css = ` .glass-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.3); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); display: flex; align-items: center; justify-content: center; z-index: 2147483647; opacity: 0; pointer-events: none; transition: opacity 0.25s ease; } .glass-overlay.show { opacity: 1; pointer-events: auto; } .glass-card { background: rgba(255, 255, 255, var(--glass-alpha, 0.7)); border-radius: 20px; box-shadow: 0 25px 45px rgba(0, 0, 0, 0.15); border: 1px solid rgba(255, 255, 255, 0.5); backdrop-filter: blur(25px); -webkit-backdrop-filter: blur(25px); padding: 28px 26px; width: 440px; max-width: 94vw; max-height: 85vh; overflow-y: auto; position: relative; color: #2c2c2c; font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; transform: scale(0.92); transition: transform 0.25s ease; box-sizing: border-box; } .glass-overlay.show .glass-card { transform: scale(1); } .card-header { display: flex; align-items: center; gap: 10px; margin-bottom: 20px; padding-bottom: 14px; border-bottom: 1.5px solid rgba(255, 255, 255, 0.8); } .card-header img { width: 32px; height: 32px; border-radius: 8px; object-fit: cover; } .card-header h2 { margin: 0; font-size: 20px; font-weight: 600; color: #1a1a1a; font-family: inherit; } .close-btn { position: absolute; top: 14px; right: 18px; background: rgba(255, 255, 255, 0.6); border: none; font-size: 20px; cursor: pointer; color: #666; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; transition: background 0.2s, color 0.2s; } .close-btn:hover { background: rgba(0, 0, 0, 0.08); color: #222; } .setting-row { display: flex; align-items: center; gap: 12px; margin-bottom: 15px; flex-wrap: wrap; } .setting-row label { min-width: 70px; font-size: 14px; color: #444; font-weight: 500; font-family: inherit; } /* 主面板下划线预览按钮:文字居中 */ .preview-btn { flex: 1; display: flex; align-items: center; justify-content: center; gap: 10px; padding: 10px 16px; background: rgba(255, 255, 255, 0.5); border: 1px solid rgba(255, 255, 255, 0.7); border-radius: 12px; cursor: pointer; font-size: 14px; color: #333; font-family: inherit; transition: background 0.2s, border-color 0.2s; } .preview-btn:hover { background: rgba(255, 255, 255, 0.8); border-color: #4a90e2; } .weight-row { display: flex; align-items: center; gap: 10px; flex: 1; } .weight-row input[type="range"] { flex: 1; height: 8px; -webkit-appearance: none; background: rgba(255, 255, 255, 0.45); border-radius: 8px; outline: none; border: 1px solid rgba(255,255,255,0.6); backdrop-filter: blur(4px); transition: background 0.2s; } .weight-row input[type="range"]:hover { background: rgba(255, 255, 255, 0.65); } .weight-row input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 26px; height: 26px; background: white; border-radius: 50%; box-shadow: 0 2px 10px rgba(0,0,0,0.15), 0 0 0 2px rgba(255,255,255,0.5); cursor: pointer; transition: transform 0.15s, box-shadow 0.15s; } .weight-row input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.1); box-shadow: 0 4px 14px rgba(0,0,0,0.2), 0 0 0 3px rgba(74,144,226,0.3); } .weight-value { min-width: 45px; text-align: center; font-weight: 700; font-size: 16px; font-family: inherit; } .danger-btn { background: rgba(255, 255, 255, 0.55); border: 1px solid rgba(229, 62, 62, 0.6); color: #d63031; padding: 12px 18px; border-radius: 14px; font-size: 15px; font-weight: 500; cursor: pointer; width: 100%; font-family: inherit; transition: all 0.2s; margin-top: 10px; } .danger-btn:hover { background: #d63031; color: white; border-color: #d63031; } .confirm-buttons { display: flex; gap: 14px; justify-content: flex-end; margin-top: 24px; } .confirm-buttons button { padding: 12px 24px; border-radius: 12px; font-size: 15px; font-weight: 500; cursor: pointer; font-family: inherit; border: 1px solid rgba(255, 255, 255, 0.5); background: rgba(255, 255, 255, 0.45); color: #333; transition: all 0.2s; } .confirm-buttons button:hover { background: rgba(255, 255, 255, 0.8); } .confirm-buttons button.confirm-yes { background: #d63031; color: white; border-color: #d63031; } .confirm-buttons button.confirm-yes:hover { background: #c0262c; } .picker-area { display: flex; flex-direction: column; gap: 10px; margin: 12px 0 20px; } .picker-row { display: flex; gap: 12px; align-items: stretch; } .sv-panel { position: relative; flex: 1; min-height: 160px; border-radius: 12px; overflow: hidden; cursor: crosshair; border: 1px solid rgba(255,255,255,0.7); box-shadow: 0 4px 10px rgba(0,0,0,0.05); } .sv-panel .sv-bg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, transparent); } .sv-panel .sv-cursor { position: absolute; width: 16px; height: 16px; border: 2px solid white; border-radius: 50%; box-shadow: 0 0 0 1px rgba(0,0,0,0.3); pointer-events: none; transform: translate(-50%, -50%); } .hue-slider { position: relative; width: 100%; height: 18px; border-radius: 9px; background: linear-gradient(to right, red, yellow, lime, cyan, blue, magenta, red); cursor: pointer; border: 1px solid rgba(255,255,255,0.7); box-shadow: 0 4px 10px rgba(0,0,0,0.05); } .hue-slider .hue-thumb { position: absolute; top: -5px; width: 28px; height: 28px; background: white; border-radius: 50%; box-shadow: 0 2px 8px rgba(0,0,0,0.3); pointer-events: none; transform: translateX(-50%); transition: left 0.05s linear; } .color-preview-block { display: flex; flex-direction: column; align-items: center; gap: 8px; width: 80px; } .color-preview-block .preview-box { width: 100%; height: 36px; border-radius: 9px; border: 1px solid rgba(255,255,255,0.8); box-shadow: 0 2px 6px rgba(0,0,0,0.08); } .color-preview-block input[type="text"] { width: 100%; padding: 6px 4px; background: rgba(255,255,255,0.45); border: 1px solid rgba(255,255,255,0.7); border-radius: 8px; font-size: 12px; font-family: inherit; text-align: center; outline: none; box-sizing: border-box; } /* 下划线选项弹窗按钮:文字绝对居中 */ .underline-options { display: flex; flex-wrap: wrap; gap: 8px; margin: 15px 0; } .underline-option { flex: 1 1 auto; display: flex; align-items: center; justify-content: center; padding: 10px 14px; background: rgba(255, 255, 255, 0.5); border: 1px solid rgba(255, 255, 255, 0.7); border-radius: 10px; cursor: pointer; font-size: 14px; font-family: inherit; text-align: center; transition: all 0.15s; } .underline-option:hover { background: rgba(255,255,255,0.8); border-color: #4a90e2; } .underline-option.active { background: #4a90e2; color: white; border-color: #4a90e2; font-weight: 600; } @media (max-width: 480px) { .glass-card { padding: 22px 18px; width: 95%; border-radius: 16px; } .card-header h2 { font-size: 18px; } .setting-row { flex-direction: column; align-items: flex-start; } .setting-row label { min-width: auto; margin-bottom: 4px; } .preview-btn { width: 100%; } .weight-row { width: 100%; } .picker-row { flex-direction: column; align-items: stretch; } .sv-panel { width: 100%; min-height: 140px; } .color-preview-block { flex-direction: row; width: 100%; gap: 10px; } .color-preview-block .preview-box { width: 56px; height: 36px; } .color-preview-block input[type="text"] { flex: 1; } .underline-options { flex-direction: column; } } `; const style = document.createElement('style'); style.id = 'glass-dialog-styles'; style.textContent = css; (document.head || document.documentElement).appendChild(style); } // ========== 主面板 (水平色相条) ========== function createSettingsPanel() { const overlay = document.createElement('div'); overlay.className = 'glass-overlay'; const initHsv = hexToHsv(settings.color); let currentH = initHsv.h, currentS = initHsv.s, currentV = initHsv.v; overlay.innerHTML = `
Logo

已访问链接样式

${underlineLabels[settings.underline]} (${settings.underline})
${settings.weight}
${settings.glassAlpha}
`; (document.body || document.documentElement).appendChild(overlay); const svPanel = overlay.querySelector('#sv-panel'); const svBg = overlay.querySelector('.sv-bg'); const svCursor = overlay.querySelector('.sv-cursor'); const hueSlider = overlay.querySelector('#hue-slider'); const hueThumb = overlay.querySelector('.hue-thumb'); const previewBox = overlay.querySelector('#color-preview-box'); const hexInput = overlay.querySelector('#hex-input'); const applyColor = (hex) => { settings.color = hex; GM_setValue('linkColor', hex); updateStyles(); previewBox.style.backgroundColor = hex; if (document.activeElement !== hexInput) hexInput.value = hex; }; const updateFromHsv = () => { const hex = hsvToHex(currentH, currentS, currentV); applyColor(hex); updateCursor(); }; const updateCursor = () => { if (svCursor && svPanel) { const rect = svPanel.getBoundingClientRect(); svCursor.style.left = (currentS * rect.width) + 'px'; svCursor.style.top = ((1 - currentV) * rect.height) + 'px'; } if (hueThumb && hueSlider) { hueThumb.style.left = (currentH / 360 * 100) + '%'; } }; const updateSvBackground = () => { svBg.style.backgroundColor = hsvToHex(currentH, 1, 1); }; updateSvBackground(); updateFromHsv(); const setSvFromEvent = (e) => { const rect = svPanel.getBoundingClientRect(); const clientX = e.touches ? e.touches[0].clientX : e.clientX; const clientY = e.touches ? e.touches[0].clientY : e.clientY; currentS = Math.min(1, Math.max(0, (clientX - rect.left) / rect.width)); currentV = 1 - Math.min(1, Math.max(0, (clientY - rect.top) / rect.height)); updateFromHsv(); }; svPanel.addEventListener('mousedown', (e) => { e.preventDefault(); setSvFromEvent(e); const moveH = (e) => { e.preventDefault(); setSvFromEvent(e); }; const upH = () => { document.removeEventListener('mousemove', moveH); document.removeEventListener('mouseup', upH); }; document.addEventListener('mousemove', moveH); document.addEventListener('mouseup', upH); }); svPanel.addEventListener('touchstart', (e) => { e.preventDefault(); setSvFromEvent(e); const moveH = (e) => { e.preventDefault(); setSvFromEvent(e); }; const endH = () => { document.removeEventListener('touchmove', moveH); document.removeEventListener('touchend', endH); }; document.addEventListener('touchmove', moveH, { passive: false }); document.addEventListener('touchend', endH); }); const setHueFromEvent = (e) => { const rect = hueSlider.getBoundingClientRect(); const clientX = e.touches ? e.touches[0].clientX : e.clientX; let x = clientX - rect.left; x = Math.min(rect.width, Math.max(0, x)); currentH = Math.round((x / rect.width) * 360); updateSvBackground(); updateFromHsv(); }; hueSlider.addEventListener('mousedown', (e) => { e.preventDefault(); setHueFromEvent(e); const moveH = (e) => { e.preventDefault(); setHueFromEvent(e); }; const upH = () => { document.removeEventListener('mousemove', moveH); document.removeEventListener('mouseup', upH); }; document.addEventListener('mousemove', moveH); document.addEventListener('mouseup', upH); }); hueSlider.addEventListener('touchstart', (e) => { e.preventDefault(); setHueFromEvent(e); const moveH = (e) => { e.preventDefault(); setHueFromEvent(e); }; const endH = () => { document.removeEventListener('touchmove', moveH); document.removeEventListener('touchend', endH); }; document.addEventListener('touchmove', moveH, { passive: false }); document.addEventListener('touchend', endH); }); hexInput.addEventListener('keydown', (e) => { if (e.key === 'Enter') { const hex = hexInput.value.trim(); if (/^#[0-9a-fA-F]{6}$/.test(hex)) { const newHsv = hexToHsv(hex); currentH = newHsv.h; currentS = newHsv.s; currentV = newHsv.v; updateSvBackground(); updateFromHsv(); } } }); overlay.querySelector('#underline-preview-btn').addEventListener('click', () => { showUnderlinePicker(settings.underline, (val) => { settings.underline = val; GM_setValue('underlineType', val); updateStyles(); overlay.querySelector('#underline-label').textContent = `${underlineLabels[val]} (${val})`; }); }); overlay.querySelector('#weight-slider').addEventListener('input', (e) => { const val = e.target.value; overlay.querySelector('#weight-display').textContent = val; settings.weight = val; GM_setValue('linkWeight', val); updateStyles(); }); overlay.querySelector('#alpha-slider').addEventListener('input', (e) => { const val = parseFloat(e.target.value).toFixed(2); overlay.querySelector('#alpha-display').textContent = val; settings.glassAlpha = parseFloat(val); document.documentElement.style.setProperty('--glass-alpha', val); GM_setValue('glassAlpha', settings.glassAlpha); }); document.documentElement.style.setProperty('--glass-alpha', settings.glassAlpha); overlay.querySelector('#clear-btn').addEventListener('click', () => { showConfirmDialog('⚠️ 确定要清除所有已访问链接的记录吗?', () => { visitedLinks.clear(); forceSave(); document.querySelectorAll('.visited-link').forEach(el => { el.classList.remove('visited-link'); el.style.removeProperty('color'); el.style.removeProperty('text-decoration'); el.style.removeProperty('font-weight'); }); }); }); const closeBtn = overlay.querySelector('.close-btn'); const closePanel = () => overlay.classList.remove('show'); closeBtn.addEventListener('click', closePanel); overlay.addEventListener('click', (e) => { if (e.target === overlay) closePanel(); }); return overlay; } // ========== 下划线弹窗 ========== function showUnderlinePicker(current, onSelect) { const overlay = document.createElement('div'); overlay.className = 'glass-overlay show'; const options = ['solid', 'dashed', 'wavy', 'double', 'dotted', 'none']; overlay.innerHTML = `
icon

选择下划线

${options.map(opt => `
${underlineLabels[opt]} (${opt})
`).join('')}
`; (document.body || document.documentElement).appendChild(overlay); const closePicker = () => { overlay.classList.remove('show'); setTimeout(() => overlay.remove(), 300); }; overlay.querySelector('.close-btn').addEventListener('click', closePicker); overlay.addEventListener('click', (e) => { if (e.target === overlay) closePicker(); }); overlay.querySelectorAll('.underline-option').forEach(el => { el.addEventListener('click', () => { onSelect(el.dataset.value); closePicker(); }); }); } function showConfirmDialog(message, onConfirm) { const overlay = document.createElement('div'); overlay.className = 'glass-overlay show'; overlay.innerHTML = `
icon

确认操作

${message}

`; (document.body || document.documentElement).appendChild(overlay); const closeConfirm = () => { overlay.classList.remove('show'); setTimeout(() => overlay.remove(), 300); }; overlay.querySelector('.close-btn').addEventListener('click', closeConfirm); overlay.querySelector('.cancel-btn').addEventListener('click', closeConfirm); overlay.querySelector('.confirm-yes').addEventListener('click', () => { if (onConfirm) onConfirm(); closeConfirm(); }); overlay.addEventListener('click', (e) => { if (e.target === overlay) closeConfirm(); }); } // ========== SPA 路由监听 ========== function patchRouter() { const origPush = history.pushState; const origReplace = history.replaceState; history.pushState = function(...args) { origPush.apply(this, args); onRouteChange(); }; history.replaceState = function(...args) { origReplace.apply(this, args); onRouteChange(); }; window.addEventListener('popstate', onRouteChange); window.addEventListener('hashchange', onRouteChange); } function onRouteChange() { applyVisitedStylesWithRetry(); } function applyVisitedStylesWithRetry(maxRetries = 5, delay = 400) { let retries = 0; const tryApply = () => { if (hasValidBody()) { applyVisitedStyles(); return; } if (++retries < maxRetries) setTimeout(tryApply, delay); }; tryApply(); } // ========== 移动端防误触与性能优化事件绑定 ========== function setupEventListeners() { document.addEventListener('click', (e) => { if (touchHandled) { touchHandled = false; return; } handleInteraction(e.target); }, true); document.addEventListener('auxclick', (e) => { if (e.button === 1) { if (touchHandled) { touchHandled = false; return; } handleInteraction(e.target); } }, true); document.addEventListener('touchstart', (e) => { if (e.touches.length > 0) { touchStartX = e.touches[0].clientX; touchStartY = e.touches[0].clientY; } }, { passive: true }); document.addEventListener('touchend', (e) => { const touch = e.changedTouches[0]; if (!touch) return; const dx = Math.abs(touch.clientX - touchStartX); const dy = Math.abs(touch.clientY - touchStartY); const distance = Math.sqrt(dx * dx + dy * dy); if (distance < 10) { touchHandled = true; clearTimeout(touchHandledTimer); touchHandledTimer = setTimeout(() => { touchHandled = false; }, 500); handleInteraction(e.target); } }, { passive: true }); } function setupMutationObserver() { observer = new MutationObserver((mutations) => { for (const m of mutations) { if (m.type === 'childList' && m.addedNodes.length) { applyStylesToNewNodes(m.addedNodes); } } }); observer.observe(document.documentElement, { childList: true, subtree: true }); } function coreInit() { initVisitedLinks(); updateStyles(); injectGlobalDialogStyles(); document.documentElement.style.setProperty('--glass-alpha', settings.glassAlpha); panelOverlay = createSettingsPanel(); GM_registerMenuCommand('⚙️ 打开已访问链接样式面板', () => { if (panelOverlay) panelOverlay.classList.add('show'); }); setupEventListeners(); setupMutationObserver(); applyVisitedStyles(); patchRouter(); console.log('[已访问链接样式] 初始化成功 (下划线居中修复 v1.7.4)'); } function attemptInit() { if (document.body) { coreInit(); } else { const observer = new MutationObserver(() => { if (document.body) { observer.disconnect(); coreInit(); } }); observer.observe(document.documentElement, { childList: true, subtree: true }); setTimeout(() => { observer.disconnect(); if (document.body) coreInit(); }, 5000); } } attemptInit(); window.addEventListener('beforeunload', () => { forceSave(); if (observer) observer.disconnect(); clearTimeout(saveTimer); }); })();