javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var showHiddenSvgExportSvgs = false; /* true: list drawings with no size on the page too, as icon sprites are */ /* SVG - EXPORT SVGS: Lists every SVG drawing written into the page, with a preview, to copy or download as a standalone .svg file that opens on its own anywhere. A drawing on a page leans on the page: on its namespace, on symbols and gradients kept elsewhere on the page, and on the page's text colour. The file each one becomes brings those with it: the SVG namespace, a copy of every element it points to by #id, its colour where it is drawn in currentColor, and a width and height where it had none. The preview is drawn from that file, so what it shows is what the file holds. Run it again, press Close or press Escape to take the list away. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeySvgExportSvgs = "--__-unlikely_-_name-__--SvgExportSvgs"; var panelIdSvgExportSvgs = "panel--__-unlikely_-_name-__--SvgExportSvgs"; var svgNsSvgExportSvgs = "http://www.w3.org/2000/svg"; var fontSvgExportSvgs = "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 styleSvgExportSvgs = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeSvgExportSvgs = (tag, css, text) => { let el = document.createElement(tag); styleSvgExportSvgs(el, css); if (text) { el.textContent = text; } return el; }; /* The drawings: top-level svg elements in the document, open shadow roots and frames from the same site, not one inside another, and not this bookmarklet's own. */ const findSvgsSvgExportSvgs = (root, out) => { out = out || []; root.querySelectorAll("svg").forEach(function(el) { if (el.parentElement && el.parentElement.closest("svg")) { return; } if (el.closest("#" + panelIdSvgExportSvgs)) { return; } if (!showHiddenSvgExportSvgs && el.getClientRects().length === 0) { return; } out.push(el); }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findSvgsSvgExportSvgs(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findSvgsSvgExportSvgs(inner, out); } }); return out; }; /* A name for the drawing: its title, its label, its id, or its place on the page. */ const nameOfSvgExportSvgs = (svg, index) => { let title = svg.querySelector("title"); let labelled = svg.getAttribute("aria-labelledby"); let byId = labelled && svg.ownerDocument.getElementById(labelled.split(/\s+/)[0]); return ((title && title.textContent) || (byId && byId.textContent) || svg.getAttribute("aria-label") || svg.id || "Drawing " + (index + 1)).trim(); }; /* The ids a drawing points to: href="#id" on use and the like, and url(#id) in its paint. */ const pointedAtSvgExportSvgs = (el) => { let ids = []; el.querySelectorAll("*").forEach(function(node) { Array.prototype.forEach.call(node.attributes, function(a) { let m; if ((a.name === "href" || a.name === "xlink:href") && (m = /^#(.+)$/.exec(a.value.trim()))) { ids.push(m[1]); } let re = /url\(\s*['"]?#([^'")\s]+)['"]?\s*\)/g; while ((m = re.exec(a.value))) { ids.push(m[1]); } }); }); return ids; }; /* The drawing as a file of its own: what it points to elsewhere copied into its defs, its colour where it draws in currentColor, and its size where it gave none. The serializer writes the SVG namespace on the root, as a file needs and a page does not. */ const exportSvgExportSvgs = (svg) => { let doc = svg.ownerDocument, clone = svg.cloneNode(true), brought = 0, defs = null; for (let round = 0; round < 5; round++) { let missing = pointedAtSvgExportSvgs(clone).filter(function(id, i, all) { return all.indexOf(id) === i && !clone.querySelector("[id=\"" + id.replace(/["\\]/g, "\\$&") + "\"]"); }); let found = missing.map(function(id) { return doc.getElementById(id); }).filter(Boolean); if (!found.length) { break; } if (!defs) { /* after the title, which a reader of the file meets first */ let title = clone.querySelector(":scope > title"); defs = doc.createElementNS(svgNsSvgExportSvgs, "defs"); clone.insertBefore(defs, title ? title.nextSibling : clone.firstChild); } found.forEach(function(el) { defs.appendChild(el.cloneNode(true)); brought++; }); } if (/currentcolor/i.test(svg.outerHTML)) { clone.style.setProperty("color", (doc.defaultView || window).getComputedStyle(svg).color); } let r = svg.getBoundingClientRect(); if (!clone.getAttribute("width") && r.width) { clone.setAttribute("width", String(Math.round(r.width))); } if (!clone.getAttribute("height") && r.height) { clone.setAttribute("height", String(Math.round(r.height))); } let text = new XMLSerializer().serializeToString(clone); if (!/^]*\sxmlns="http:\/\/www\.w3\.org\/2000\/svg"/.test(text)) { text = text.replace(/^ (String(name).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "drawing") + ".svg"; /* Close, Escape and a second run: the list, its addresses and its key listener go. */ const closeSvgExportSvgs = () => { let state = window[stateKeySvgExportSvgs]; if (!state) { return; } delete window[stateKeySvgExportSvgs]; document.removeEventListener("keydown", state.onKey, true); state.urls.forEach(function(u) { URL.revokeObjectURL(u); }); if (state.panel.parentNode) { state.panel.parentNode.removeChild(state.panel); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainSvgExportSvgs() { if (window[stateKeySvgExportSvgs]) { closeSvgExportSvgs(); /* a second run takes the list away */ return; } let svgs = findSvgsSvgExportSvgs(document); let panel = makeSvgExportSvgs("div", { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", width: "420px", "max-width": "calc(100vw - 24px)", "max-height": "calc(100vh - 24px)", overflow: "auto", padding: "10px 12px", background: "#1f2328", color: "#ffffff", font: fontSvgExportSvgs, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left", "box-sizing": "border-box" }); panel.id = panelIdSvgExportSvgs; panel.setAttribute("role", "dialog"); panel.setAttribute("aria-label", "SVG drawings"); let state = window[stateKeySvgExportSvgs] = { panel: panel, urls: [], onKey: null }; let head = panel.appendChild(makeSvgExportSvgs("div", { display: "flex", "align-items": "center", gap: "8px", "margin-bottom": "8px" })); head.appendChild(makeSvgExportSvgs("strong", { "font-size": "15px", "margin-right": "auto" }, svgs.length + (svgs.length === 1 ? " SVG drawing" : " SVG drawings") + " on this page")); let close = head.appendChild(makeSvgExportSvgs("button", { padding: "2px 10px", font: fontSvgExportSvgs, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, "Close")); close.type = "button"; close.addEventListener("click", closeSvgExportSvgs); let list = panel.appendChild(makeSvgExportSvgs("ol", { margin: "0", padding: "0", "list-style": "none" })); svgs.forEach(function(svg, i) { let name = nameOfSvgExportSvgs(svg, i), file = exportSvgExportSvgs(svg); let url = URL.createObjectURL(new Blob([file.text], { type: "image/svg+xml" })); state.urls.push(url); let li = list.appendChild(makeSvgExportSvgs("li", { display: "flex", gap: "10px", "align-items": "center", margin: "0 0 8px", padding: "6px", background: "#2d333b", "border-radius": "4px" })); li.setAttribute("data-name", name); li.setAttribute("data-brought", String(file.brought)); let frame = li.appendChild(makeSvgExportSvgs("div", { width: "72px", height: "72px", "flex": "0 0 72px", display: "flex", "align-items": "center", "justify-content": "center", background: "#ffffff", "border-radius": "3px", overflow: "hidden" })); let preview = frame.appendChild(makeSvgExportSvgs("img", { "max-width": "64px", "max-height": "64px" })); preview.alt = ""; preview.addEventListener("load", function() { li.setAttribute("data-preview", "loaded"); }); preview.addEventListener("error", function() { li.setAttribute("data-preview", "refused"); }); preview.src = url; let words = li.appendChild(makeSvgExportSvgs("div", { "min-width": "0", flex: "1 1 auto" })); words.appendChild(makeSvgExportSvgs("strong", { display: "block", "overflow-wrap": "anywhere" }, name)); words.appendChild(makeSvgExportSvgs("span", { display: "block", color: "#d0d7de" }, file.width + " \u00d7 " + file.height + ", " + file.text.length + " characters" + (file.brought ? ", " + file.brought + (file.brought === 1 ? " part" : " parts") + " brought from elsewhere on the page" : ""))); let said = makeSvgExportSvgs("span", { display: "block", color: "#d0d7de" }); let row = words.appendChild(makeSvgExportSvgs("div", { display: "flex", gap: "6px", "margin-top": "4px" })); const button = (label, act) => { let b = row.appendChild(makeSvgExportSvgs("button", { padding: "2px 8px", font: fontSvgExportSvgs, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, label)); b.type = "button"; b.addEventListener("click", act); }; button("Copy", function() { let copied = navigator.clipboard && navigator.clipboard.writeText ? navigator.clipboard.writeText(file.text) : Promise.reject(new Error("no clipboard")); copied.then(function() { said.textContent = "Copied."; li.setAttribute("data-copied", "yes"); }, function() { said.textContent = "The browser refused the clipboard."; }); }); button("Download", function() { let link = document.createElement("a"); link.href = url; link.download = fileNameSvgExportSvgs(name); styleSvgExportSvgs(link, { display: "none" }); panel.appendChild(link); link.click(); link.parentNode.removeChild(link); said.textContent = "Saved " + link.download + "."; }); words.appendChild(said); }); if (!svgs.length) { list.appendChild(makeSvgExportSvgs("li", {}, "No SVG drawings written into this page. A drawing in an image file is an image, which the Image Gallery bookmarklet lists.")); } state.onKey = function(event) { if (event.key === "Escape") { event.stopPropagation(); closeSvgExportSvgs(); } }; document.addEventListener("keydown", state.onKey, true); (document.body || document.documentElement).appendChild(panel); } mainSvgExportSvgs(); })();