(() => { if (!window.HTMLDialogElement?.prototype.showModal) return; const thumbnails = [...document.querySelectorAll('main a[href]:not([download]) > img')] .filter(({parentElement: link}) => link.protocol === location.protocol && link.origin === location.origin && /\.(?:png|jpe?g|webp|gif|avif)$/i.test(link.pathname)); if (!thumbnails.length) return; const dialog = document.createElement("dialog"); dialog.className = "screenshot-viewer"; dialog.setAttribute("aria-label", "Screenshot preview"); dialog.innerHTML = `
`; const screenshot = dialog.querySelector("img"); document.body.append(dialog); let opener; let pointerStartedOutside = false; for (const thumbnail of thumbnails) { const link = thumbnail.parentElement; link.setAttribute("aria-haspopup", "dialog"); link.addEventListener("click", (event) => { if (event.defaultPrevented || event.button !== 0 || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return; event.preventDefault(); opener = link; screenshot.alt = thumbnail.alt; screenshot.src = link.href; dialog.showModal(); }); } dialog.addEventListener("pointerdown", (event) => { pointerStartedOutside = event.target === dialog; }); dialog.addEventListener("click", (event) => { if (pointerStartedOutside && event.target === dialog) dialog.close(); }); dialog.addEventListener("close", () => { opener.focus({preventScroll: true}); }); })();