javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var formatImageCopyImageList = "ask"; /* "ask" each time, or always "list" or "markdown" */ var includeBackgroundsImageCopyImageList = true; /* true: CSS background images are listed as well */ /* IMAGE - COPY IMAGE LIST: Copies the address of every image on the page, one to a line, ready for a download tool: as a plain list, or as Markdown images with their alt text. It finds what the Image Gallery bookmarklet finds - plain images, the largest size a srcset or a picture element offers, CSS backgrounds, video posters and images that load only when scrolled to - and leaves out inline images, which have no address to download. A small panel shows what was copied, so a copy the browser refuses can be made by hand. Run it again, press Close or press Escape to take it away. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyImageCopyImageList = "--__-unlikely_-_name-__--ImageCopyImageList"; var panelIdImageCopyImageList = "panel--__-unlikely_-_name-__--ImageCopyImageList"; var fontImageCopyImageList = "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 styleImageCopyImageList = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeImageCopyImageList = (tag, css, text) => { let el = document.createElement(tag); styleImageCopyImageList(el, css); if (text) { el.textContent = text; } return el; }; /* An address as the page means it, or "" when it is not one. */ const resolveImageCopyImageList = (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 parseSrcsetImageCopyImageList = (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 largestImageCopyImageList = (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 backgroundUrlsImageCopyImageList = (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 rootsImageCopyImageList = (root, out) => { out = out || []; out.push(root); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { rootsImageCopyImageList(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { rootsImageCopyImageList(inner, out); } }); return out; }; /* Every image address the page holds, once each, in page order, and how many were inline. */ const collectImageCopyImageList = () => { let found = [], seen = new Set(), inline = new Set(); const add = (url, alt, doc) => { let href = resolveImageCopyImageList(url, doc); if (!href) { return; } if (/^data:/i.test(href)) { inline.add(href); return; } if (seen.has(href)) { return; } seen.add(href); found.push({ url: href, alt: String(alt || "").trim() }); }; rootsImageCopyImageList(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(parseSrcsetImageCopyImageList(src.getAttribute("srcset") || src.getAttribute("data-srcset"))); }); } cands = cands.concat(parseSrcsetImageCopyImageList(img.getAttribute("srcset") || img.getAttribute("data-srcset"))); let lazy = img.getAttribute("data-src") || img.getAttribute("data-lazy-src") || img.getAttribute("data-original"); let best = largestImageCopyImageList(cands, img.width || img.naturalWidth); let url = best ? best.url : (lazy || img.getAttribute("src") || img.currentSrc); if (url) { add(url, img.alt, doc); } }); root.querySelectorAll("video[poster]").forEach(function(video) { add(video.getAttribute("poster"), video.title, doc); }); if (!includeBackgroundsImageCopyImageList) { return; } root.querySelectorAll("*").forEach(function(el) { ["", "::before", "::after"].forEach(function(pseudo) { let bg = view.getComputedStyle(el, pseudo || null).backgroundImage; if (!bg || bg === "none") { return; } let named = pseudo ? "" : (el.getAttribute("aria-label") || el.getAttribute("title") || ""); backgroundUrlsImageCopyImageList(bg).forEach(function(url) { add(url, named, doc); }); }); }); }); return { list: found, inline: inline.size }; }; /* One line per image: the address alone, or a Markdown image with its alt text. */ const formatLinesImageCopyImageList = (list, markdown) => list.map(function(item) { if (!markdown) { return item.url; } let alt = item.alt.replace(/[\[\]\\]/g, "\\$&").replace(/\s+/g, " "); let url = /[\s()<>]/.test(item.url) ? "<" + item.url + ">" : item.url; return "![" + alt + "](" + url + ")"; }).join("\n"); /* The clipboard, or failing that a copy made from the selected text. */ const copyImageCopyImageList = (text, box) => { const byHand = () => { box.focus(); box.select(); let done = false; try { done = document.execCommand("copy"); } catch (err) { done = false; } return done; }; if (navigator.clipboard && navigator.clipboard.writeText) { return navigator.clipboard.writeText(text).then(function() { return true; }, function() { return byHand(); }); } return Promise.resolve(byHand()); }; /* Close, Escape and a second run: the panel and its key listener go, and nothing else changed. */ const closeImageCopyImageList = () => { let state = window[stateKeyImageCopyImageList]; if (!state) { return; } delete window[stateKeyImageCopyImageList]; document.removeEventListener("keydown", state.onKey, true); if (state.panel.parentNode) { state.panel.parentNode.removeChild(state.panel); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainImageCopyImageList() { if (window[stateKeyImageCopyImageList]) { closeImageCopyImageList(); /* a second run takes the panel away */ return; } let found = collectImageCopyImageList(), n = found.list.length; let format = String(formatImageCopyImageList).toLowerCase(), markdown = format === "markdown"; if (format === "ask" && n > 0) { markdown = confirm("Copy " + n + (n === 1 ? " image address" : " image addresses") + " as Markdown?\n\n" + "OK: Markdown images, ![alt text](address), one to a line.\n" + "Cancel: a plain list, one address to a line."); } let text = formatLinesImageCopyImageList(found.list, markdown); let panel = makeImageCopyImageList("div", { position: "fixed", right: "12px", bottom: "12px", "z-index": "2147483647", width: "420px", "max-width": "calc(100vw - 24px)", padding: "10px 12px", background: "#1f2328", color: "#ffffff", font: fontImageCopyImageList, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left", "box-sizing": "border-box" }); panel.id = panelIdImageCopyImageList; panel.setAttribute("role", "dialog"); panel.setAttribute("aria-label", "Image list"); let said = makeImageCopyImageList("p", { margin: "0 0 6px" }, "Copying..."); let box = makeImageCopyImageList("textarea", { display: "block", width: "396px", "max-width": "calc(100vw - 48px)", height: "120px", margin: "0", padding: "6px", font: "12px/1.4 ui-monospace,SFMono-Regular,Consolas,monospace", color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", "box-sizing": "border-box", resize: "vertical" }); box.readOnly = true; box.textContent = text; box.setAttribute("aria-label", markdown ? "Image list as Markdown" : "Image list"); let row = makeImageCopyImageList("div", { display: "flex", gap: "8px", "margin-top": "8px" }); const button = (words, act) => { let b = makeImageCopyImageList("button", { padding: "3px 12px", font: fontImageCopyImageList, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, words); b.type = "button"; b.addEventListener("click", act); row.appendChild(b); return b; }; let state = window[stateKeyImageCopyImageList] = { panel: panel, onKey: null }; const report = (copied) => { let what = n + (n === 1 ? " image address" : " image addresses") + (markdown ? " as Markdown" : " as a plain list"); let left = found.inline ? " " + found.inline + (found.inline === 1 ? " inline image has" : " inline images have") + " no address and is left out." : ""; said.textContent = (n === 0 ? "No image addresses found on this page." : copied ? "Copied " + what + "." : "Could not copy " + what + ": it is selected below, so press Ctrl+C.") + left; panel.setAttribute("data-copied", copied ? "yes" : "no"); }; button("Copy again", function() { copyImageCopyImageList(text, box).then(report); }); button("Close", closeImageCopyImageList); panel.appendChild(said); panel.appendChild(box); panel.appendChild(row); state.onKey = function(event) { if (event.key === "Escape") { event.stopPropagation(); closeImageCopyImageList(); } }; document.addEventListener("keydown", state.onKey, true); (document.body || document.documentElement).appendChild(panel); if (n === 0) { report(false); return; } copyImageCopyImageList(text, box).then(report); } mainImageCopyImageList(); })();