javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var paddingCaptureElementBounds = 0; /* CSS pixels of room added around the element on every side */ /* CAPTURE - ELEMENT BOUNDS: Click any element to copy where it is and how big, as one line of JSON, for cropping a screen recording to exactly that element. The JSON gives the element's box on the page and in the window, in CSS pixels, the device pixel ratio, and the same box in the screen's own pixels, which is what a recording of the window is measured in. While it is picking, the element under the pointer is outlined with its size, and the page does not get the click. The pick ends on the click, or on Escape, or on a second run, and takes every listener with it. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyCaptureElementBounds = "--__-unlikely_-_name-__--CaptureElementBounds"; var hostIdCaptureElementBounds = "pick--__-unlikely_-_name-__--CaptureElementBounds"; var noteIdCaptureElementBounds = "note--__-unlikely_-_name-__--CaptureElementBounds"; var noteSecondsCaptureElementBounds = 6; var fontCaptureElementBounds = "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 styleCaptureElementBounds = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeCaptureElementBounds = (tag, css, text) => { let el = document.createElement(tag); styleCaptureElementBounds(el, css); if (text) { el.textContent = text; } return el; }; /* Short name for an element, like section#routes.card */ const describeCaptureElementBounds = (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; }; /* Two places after the point is enough for a CSS pixel. */ const roundCaptureElementBounds = (n) => Math.round(n * 100) / 100; /* The topmost page element at a point, inside open shadow roots, never this bookmarklet's own. */ const pickAtCaptureElementBounds = (x, y, host) => { let scope = document, el = null; for (let d = 0; d < 10; d++) { let list = scope.elementsFromPoint(x, y), hit = null; for (let i = 0; i < list.length; i++) { if (!host.contains(list[i])) { hit = list[i]; break; } } if (!hit || hit === el) { break; } el = hit; if (!el.shadowRoot || !el.shadowRoot.elementsFromPoint) { break; } scope = el.shadowRoot; } return (el === document.documentElement) ? null : el; }; /* The element's box: on the page, in the window, and in the screen's own pixels, edges rounded so the width and height add up. */ const boundsCaptureElementBounds = (el) => { let r = el.getBoundingClientRect(), pad = Number(paddingCaptureElementBounds) || 0; let left = r.left - pad, top = r.top - pad, width = r.width + 2 * pad, height = r.height + 2 * pad; let dpr = window.devicePixelRatio || 1; let dl = Math.round(left * dpr), dt = Math.round(top * dpr); let dr = Math.round((left + width) * dpr), db = Math.round((top + height) * dpr); return { element: describeCaptureElementBounds(el), devicePixelRatio: dpr, page: { x: roundCaptureElementBounds(left + window.scrollX), y: roundCaptureElementBounds(top + window.scrollY), width: roundCaptureElementBounds(width), height: roundCaptureElementBounds(height) }, viewport: { x: roundCaptureElementBounds(left), y: roundCaptureElementBounds(top), width: roundCaptureElementBounds(width), height: roundCaptureElementBounds(height) }, devicePixels: { x: dl, y: dt, width: dr - dl, height: db - dt } }; }; /* The clipboard, or failing that a copy made from selected text. */ const copyCaptureElementBounds = (text) => { const byHand = () => { let box = document.createElement("textarea"); box.value = text; styleCaptureElementBounds(box, { position: "fixed", top: "0", left: "0", width: "1px", height: "1px", opacity: "0" }); (document.body || document.documentElement).appendChild(box); box.select(); let done = false; try { done = document.execCommand("copy"); } catch (err) { done = false; } box.parentNode.removeChild(box); return done; }; if (navigator.clipboard && navigator.clipboard.writeText) { return navigator.clipboard.writeText(text).then(function() { return true; }, function() { return byHand(); }); } return Promise.resolve(byHand()); }; /* A note saying what was copied, which takes itself away. */ const noteCaptureElementBounds = (words, result) => { let old = document.getElementById(noteIdCaptureElementBounds); if (old && old.parentNode) { old.parentNode.removeChild(old); } let note = makeCaptureElementBounds("div", { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", "max-width": "360px", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontCaptureElementBounds, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left", "overflow-wrap": "anywhere" }, words); note.id = noteIdCaptureElementBounds; note.setAttribute("role", "status"); note.setAttribute("data-result", result); (document.body || document.documentElement).appendChild(note); setTimeout(function() { if (note.parentNode) { note.parentNode.removeChild(note); } }, noteSecondsCaptureElementBounds * 1000); }; /* The end of a pick, however it ends: every listener and the outline go. */ const stopCaptureElementBounds = () => { let state = window[stateKeyCaptureElementBounds]; if (!state) { return; } delete window[stateKeyCaptureElementBounds]; state.listeners.forEach(function(l) { window.removeEventListener(l[0], l[1], true); }); if (state.host.parentNode) { state.host.parentNode.removeChild(state.host); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainCaptureElementBounds() { if (window[stateKeyCaptureElementBounds]) { stopCaptureElementBounds(); /* a second run ends the pick */ return; } let host = makeCaptureElementBounds("div", { position: "fixed", top: "0", left: "0", width: "0", height: "0", "z-index": "2147483647", "pointer-events": "none" }); host.id = hostIdCaptureElementBounds; let box = host.appendChild(makeCaptureElementBounds("div", { position: "fixed", display: "none", "box-sizing": "border-box", border: "2px solid #d1242f", background: "rgba(209,36,47,.12)", "pointer-events": "none" })); let label = host.appendChild(makeCaptureElementBounds("div", { position: "fixed", display: "none", padding: "2px 6px", background: "#d1242f", color: "#ffffff", font: fontCaptureElementBounds, "border-radius": "3px", "pointer-events": "none", "white-space": "nowrap" })); let hint = host.appendChild(makeCaptureElementBounds("div", { position: "fixed", top: "12px", left: "12px", padding: "6px 10px", background: "#1f2328", color: "#ffffff", font: fontCaptureElementBounds, "border-radius": "6px", "pointer-events": "none" }, "Click an element to copy its bounds. Escape cancels.")); hint.setAttribute("data-role", "hint"); label.setAttribute("data-role", "label"); let state = window[stateKeyCaptureElementBounds] = { host: host, listeners: [], target: null }; const listen = (type, fn) => { window.addEventListener(type, fn, true); state.listeners.push([type, fn]); }; const draw = (el) => { state.target = el; if (!el) { styleCaptureElementBounds(box, { display: "none" }); styleCaptureElementBounds(label, { display: "none" }); return; } let r = el.getBoundingClientRect(); styleCaptureElementBounds(box, { display: "block", left: r.left + "px", top: r.top + "px", width: r.width + "px", height: r.height + "px" }); label.textContent = describeCaptureElementBounds(el) + " " + roundCaptureElementBounds(r.width) + " × " + roundCaptureElementBounds(r.height); styleCaptureElementBounds(label, { display: "block", left: Math.max(0, r.left) + "px", top: (r.top >= 24 ? r.top - 24 : Math.min(r.bottom + 4, window.innerHeight - 24)) + "px" }); }; /* The page does not get a press while the pick is on, so a link or a button under the pointer does nothing but get picked. */ const swallow = (event) => { event.preventDefault(); event.stopPropagation(); event.stopImmediatePropagation(); }; listen("mousemove", function(event) { draw(pickAtCaptureElementBounds(event.clientX, event.clientY, host)); }); listen("pointerdown", swallow); listen("mousedown", swallow); listen("mouseup", swallow); listen("pointerup", swallow); listen("click", function(event) { swallow(event); let el = pickAtCaptureElementBounds(event.clientX, event.clientY, host) || state.target; stopCaptureElementBounds(); if (!el) { return; } let found = boundsCaptureElementBounds(el), json = JSON.stringify(found); copyCaptureElementBounds(json).then(function(copied) { let said = found.element + ", " + found.page.width + " × " + found.page.height + " at " + found.page.x + ", " + found.page.y + " on the page, device pixel ratio " + found.devicePixelRatio; noteCaptureElementBounds(copied ? "Copied the bounds of " + said + "." : "Could not copy the bounds of " + said + ": " + json, copied ? "copied" : "failed"); }); }); listen("keydown", function(event) { if (event.key !== "Escape") { return; } swallow(event); stopCaptureElementBounds(); noteCaptureElementBounds("Element bounds cancelled. Nothing was copied.", "cancelled"); }); (document.body || document.documentElement).appendChild(host); } mainCaptureElementBounds(); })();