javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var thumbHeightImageImageGallery = 160; /* the tallest a thumbnail is drawn, in pixels */ var includeBackgroundsImageImageGallery = true; /* true: CSS background images are listed as well */ /* IMAGE - IMAGE GALLERY: Every image on the page in one grid: plain images, the largest size a srcset or a picture element offers, CSS backgrounds, video posters, and images that load only when scrolled to. Each tile gives the image's natural size, its alt text and a link to open it on its own. Run it again, press Close or press Escape to take the grid away. The thumbnails and the links are asked for with no referrer, so the page's address does not go with them. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyImageImageGallery = "--__-unlikely_-_name-__--ImageImageGallery"; var galleryIdImageImageGallery = "gallery--__-unlikely_-_name-__--ImageImageGallery"; var eagerTilesImageImageGallery = 24; /* thumbnails past this many load as they are scrolled to */ var fontImageImageGallery = "13px/1.45 -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif"; /************************************* SUPPORT FUNCTIONS *************************************/ /* Style through the CSSOM, which a strict content security policy still allows. */ const styleImageImageGallery = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeImageImageGallery = (tag, css, text) => { let el = document.createElement(tag); styleImageImageGallery(el, css); if (text) { el.textContent = text; } return el; }; /* Short name for an element, like div#banner.hero */ const describeImageImageGallery = (el) => { let s = el.tagName.toLowerCase(); if (el.id) { s += "#" + el.id; } let cls = typeof el.className == "string" ? el.className.trim().split(/\s+/).filter(Boolean).slice(0, 2) : []; if (cls.length) { s += "." + cls.join("."); } return s; }; /* An address as the page means it, or "" when it is not one. */ const resolveImageImageGallery = (url, doc) => { try { return new URL(String(url).trim(), doc.baseURI).href; } catch (err) { return ""; } }; /* The candidates of a srcset, each with a width (160w) or a density (2x). A URL runs to the next space, so a comma inside one, as a data address has, stays in it. */ const parseSrcsetImageImageGallery = (text) => { let out = [], s = String(text || ""), i = 0; while (i < s.length) { while (i < s.length && /[\s,]/.test(s[i])) { i++; } if (i >= s.length) { break; } let start = i; while (i < s.length && !/\s/.test(s[i])) { i++; } let url = s.slice(start, i), desc = ""; if (/,$/.test(url)) { url = url.replace(/,+$/, ""); } else { start = i; while (i < s.length && s[i] !== ",") { i++; } desc = s.slice(start, i).trim(); } let w = /^(\d+)w$/.exec(desc), x = /^([\d.]+)x$/.exec(desc); out.push({ url: url, w: w ? Number(w[1]) : 0, x: x ? Number(x[1]) : (w ? 0 : 1) }); } return out; }; /* The largest candidate: by width, or by density times the width the image is laid out at. */ const largestImageImageGallery = (list, laidOut) => { let best = null, bestScore = -1; list.forEach(function(c) { let score = c.w || c.x * (laidOut || 1); if (score > bestScore) { best = c; bestScore = score; } }); return best; }; /* Every url(...) in a computed background-image, gradients left out. */ const backgroundUrlsImageImageGallery = (value) => { let out = [], re = /url\(\s*(['"]?)(.*?)\1\s*\)/g, m; while ((m = re.exec(String(value || "")))) { if (m[2]) { out.push(m[2]); } } return out; }; /* The documents to read: this one, open shadow roots, and frames from the same site. */ const rootsImageImageGallery = (root, out) => { out = out || []; out.push(root); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { rootsImageImageGallery(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { rootsImageImageGallery(inner, out); } }); return out; }; /* Every image the page holds, one entry per address, in page order. */ const collectImageImageGallery = (panel) => { let found = [], byUrl = new Map(); const add = (url, kind, alt, doc) => { let href = resolveImageImageGallery(url, doc); if (!href) { return; } let seen = byUrl.get(href); if (seen) { seen.uses++; return; } seen = { url: href, kind: kind, alt: alt, uses: 1 }; byUrl.set(href, seen); found.push(seen); }; rootsImageImageGallery(document).forEach(function(root) { let doc = root.ownerDocument || root, view = doc.defaultView || window; root.querySelectorAll("img").forEach(function(img) { let pic = img.parentElement && img.parentElement.tagName === "PICTURE" ? img.parentElement : null, cands = []; if (pic) { pic.querySelectorAll("source").forEach(function(src) { cands = cands.concat(parseSrcsetImageImageGallery(src.getAttribute("srcset") || src.getAttribute("data-srcset"))); }); } cands = cands.concat(parseSrcsetImageImageGallery(img.getAttribute("srcset") || img.getAttribute("data-srcset"))); let lazy = img.getAttribute("data-src") || img.getAttribute("data-lazy-src") || img.getAttribute("data-original"); let best = largestImageImageGallery(cands, img.width || img.naturalWidth); let url = best ? best.url : (lazy || img.getAttribute("src") || img.currentSrc); let kind = pic && cands.length ? "picture" : cands.length ? "srcset" : (lazy || img.loading === "lazy") ? "lazy" : "image"; let alt = !img.hasAttribute("alt") ? "No alt text" : img.alt.trim() === "" ? "Empty alt, marked as decoration" : img.alt; if (url) { add(url, kind, alt, doc); } }); root.querySelectorAll("video[poster]").forEach(function(video) { add(video.getAttribute("poster"), "poster", video.title || "Poster of " + describeImageImageGallery(video), doc); }); if (!includeBackgroundsImageImageGallery) { return; } root.querySelectorAll("*").forEach(function(el) { if (panel.contains(el)) { return; } ["", "::before", "::after"].forEach(function(pseudo) { let bg = view.getComputedStyle(el, pseudo || null).backgroundImage; if (!bg || bg === "none") { return; } let named = el.getAttribute("aria-label") || el.getAttribute("title") || "Background of " + describeImageImageGallery(el) + pseudo; backgroundUrlsImageImageGallery(bg).forEach(function(url) { add(url, "background", named, doc); }); }); }); }); return found; }; var kindWordsImageImageGallery = { image: "Image", srcset: "Largest of its sizes", picture: "Picture, largest source", lazy: "Loads when scrolled to", poster: "Video poster", background: "CSS background" }; /* One tile: the thumbnail, its natural size once it loads, its alt text and where it opens. */ const tileImageImageGallery = (item, index) => { let tile = makeImageImageGallery("figure", { margin: "0", padding: "8px", background: "#ffffff", color: "#1f2328", "border-radius": "6px", display: "flex", "flex-direction": "column", gap: "4px", "min-width": "0" }); tile.setAttribute("data-kind", item.kind); tile.setAttribute("data-url", item.url); let frame = makeImageImageGallery("div", { height: thumbHeightImageImageGallery + "px", display: "flex", "align-items": "center", "justify-content": "center", overflow: "hidden", background: "#f6f8fa", "border-radius": "4px" }); let thumb = makeImageImageGallery("img", { "max-width": "184px", "max-height": thumbHeightImageImageGallery + "px", width: "auto", height: "auto" }); thumb.alt = ""; thumb.referrerPolicy = "no-referrer"; thumb.loading = index < eagerTilesImageImageGallery ? "eager" : "lazy"; let size = makeImageImageGallery("span", { "font-weight": "600" }, "Loading..."); thumb.addEventListener("load", function() { tile.setAttribute("data-natural", thumb.naturalWidth + "x" + thumb.naturalHeight); size.textContent = thumb.naturalWidth + " × " + thumb.naturalHeight; }); thumb.addEventListener("error", function() { tile.setAttribute("data-natural", "none"); size.textContent = "Did not load"; }); thumb.src = item.url; frame.appendChild(thumb); tile.appendChild(frame); let words = makeImageImageGallery("figcaption", { display: "flex", "flex-direction": "column", gap: "2px", "overflow-wrap": "anywhere" }); words.appendChild(size); words.appendChild(makeImageImageGallery("span", { color: "#57606a" }, kindWordsImageImageGallery[item.kind] + (item.uses > 1 ? ", on the page " + item.uses + " times" : ""))); words.appendChild(makeImageImageGallery("span", {}, item.alt)); if (/^data:/i.test(item.url)) { words.appendChild(makeImageImageGallery("span", { color: "#57606a" }, "Inline image, with no address to open")); } else { let open = makeImageImageGallery("a", { color: "#0969da", "text-decoration": "underline" }, "Open"); open.href = item.url; open.target = "_blank"; open.rel = "noopener noreferrer"; open.title = item.url; words.appendChild(open); } tile.appendChild(words); return tile; }; /* Close, Escape and a second run: the grid and its key listener go, and nothing else changed. */ const closeImageImageGallery = () => { let state = window[stateKeyImageImageGallery]; if (!state) { return; } delete window[stateKeyImageImageGallery]; document.removeEventListener("keydown", state.onKey, true); if (state.panel.parentNode) { state.panel.parentNode.removeChild(state.panel); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainImageImageGallery() { if (window[stateKeyImageImageGallery]) { closeImageImageGallery(); /* a second run takes the grid away */ return; } let panel = makeImageImageGallery("div", { position: "fixed", top: "0", right: "0", bottom: "0", left: "0", "z-index": "2147483647", overflow: "auto", padding: "16px", background: "rgba(13,17,23,.92)", color: "#ffffff", font: fontImageImageGallery, "text-align": "left", "box-sizing": "border-box" }); panel.id = galleryIdImageImageGallery; panel.setAttribute("role", "dialog"); panel.setAttribute("aria-label", "Image gallery"); let state = window[stateKeyImageImageGallery] = { panel: panel, onKey: null }; let items = collectImageImageGallery(panel); let head = makeImageImageGallery("div", { display: "flex", "align-items": "center", gap: "12px", "margin-bottom": "12px" }); head.appendChild(makeImageImageGallery("strong", { "font-size": "16px" }, "Image gallery: " + items.length + (items.length === 1 ? " image" : " images"))); let close = makeImageImageGallery("button", { padding: "4px 12px", font: fontImageImageGallery, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, "Close"); close.type = "button"; close.addEventListener("click", closeImageImageGallery); head.appendChild(close); panel.appendChild(head); let grid = makeImageImageGallery("div", { display: "grid", gap: "12px", "grid-template-columns": "repeat(auto-fill, minmax(200px, 1fr))" }); items.forEach(function(item, i) { grid.appendChild(tileImageImageGallery(item, i)); }); if (items.length === 0) { grid.appendChild(makeImageImageGallery("p", { margin: "0" }, "No images found on this page.")); } panel.appendChild(grid); state.onKey = function(event) { if (event.key === "Escape") { event.stopPropagation(); closeImageImageGallery(); } }; document.addEventListener("keydown", state.onKey, true); (document.body || document.documentElement).appendChild(panel); close.focus(); } mainImageImageGallery(); })();