// Clipboard helper — consolidates the ~6 duplicated copy-then-flash-label // snippets and the delegated [data-copy-link] handler from mnw.js:670. Falls // back to a prompt in non-secure contexts (plain HTTP, some iframes), where the // old inline snippets silently no-op'd. /** Copy `text`, flashing `copiedLabel` on `el`, then restoring the label. */ export function copyWithFeedback(el: HTMLElement, text: string, copiedLabel = 'Copied!', resetMs = 1500): void { const defaultLabel = el.textContent ?? ''; const restore = () => { el.textContent = defaultLabel; }; if (navigator.clipboard?.writeText) { navigator.clipboard .writeText(text) .then(() => { el.textContent = copiedLabel; setTimeout(restore, resetMs); }) .catch(() => { window.prompt('Copy this link:', text); }); } else { window.prompt('Copy this link:', text); } } /** Install the delegated `[data-copy-link]` handler. `href` stays the real * destination (middle-click / no-JS / share still work); left-click copies. * Ported from mnw.js:670. */ export function initCopyLink(): void { document.addEventListener('click', (evt) => { const el = (evt.target as Element | null)?.closest('[data-copy-link]'); if (!el) return; evt.preventDefault(); let url = el.dataset.url || el.getAttribute('href') || window.location.href; if (url.charAt(0) === '/') url = window.location.origin + url; const copiedLabel = el.dataset.copiedLabel || 'Copied!'; copyWithFeedback(el, url, copiedLabel); }); }