javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var sizeCaptureRecordingFrame = ""; /* "1280x720", "1080x1080" or "1080x1920" to draw at once; empty: choose in the panel */ /* CAPTURE - RECORDING FRAME: Stages a screen recording: a frame at a social video size, 1280 x 720, 1080 x 1080 or 1080 x 1920, with everything outside it dimmed, and the page's scrollbars hidden while the page still scrolls. The size is in the screen's own pixels, which is what a recording is measured in. A size the window is too small for is drawn at the same shape as large as fits, and the panel says so. Hide panel keeps it out of the recording. Run it again, press Off, or press Escape, and the frame goes and the page's own scrollbar settings come back exactly as they were. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyCaptureRecordingFrame = "--__-unlikely_-_name-__--CaptureRecordingFrame"; var hostIdCaptureRecordingFrame = "frame--__-unlikely_-_name-__--CaptureRecordingFrame"; var panelIdCaptureRecordingFrame = "panel--__-unlikely_-_name-__--CaptureRecordingFrame"; var sizesCaptureRecordingFrame = [[1280, 720], [1080, 1080], [1080, 1920]]; var fontCaptureRecordingFrame = "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 styleCaptureRecordingFrame = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeCaptureRecordingFrame = (tag, css, text) => { let el = document.createElement(tag); styleCaptureRecordingFrame(el, css); if (text) { el.textContent = text; } return el; }; /* "1080x1920" as [1080, 1920], or null. */ const parseSizeCaptureRecordingFrame = (text) => { let m = /^\s*(\d+)\s*[x×]\s*(\d+)\s*$/i.exec(String(text || "")); return m ? [Number(m[1]), Number(m[2])] : null; }; /* Change an inline style, keeping what the page had there to put back: its value and whether it was marked important, read before anything is changed. */ const changeCaptureRecordingFrame = (state, el, prop, value) => { state.kept.push({ el: el, prop: prop, value: el.style.getPropertyValue(prop), priority: el.style.getPropertyPriority(prop) }); el.style.setProperty(prop, value, "important"); }; /* Off, Escape and a second run: the page's own settings back, newest first, and the frame gone. */ const stopCaptureRecordingFrame = () => { let state = window[stateKeyCaptureRecordingFrame]; if (!state) { return; } delete window[stateKeyCaptureRecordingFrame]; window.removeEventListener("resize", state.onResize); window.removeEventListener("keydown", state.onKey, true); state.kept.slice().reverse().forEach(function(k) { if (k.value) { k.el.style.setProperty(k.prop, k.value, k.priority); } else { k.el.style.removeProperty(k.prop); } }); [state.host, state.panel].forEach(function(el) { if (el.parentNode) { el.parentNode.removeChild(el); } }); }; /*************************************** MAIN FUNCTION ***************************************/ function mainCaptureRecordingFrame() { if (window[stateKeyCaptureRecordingFrame]) { stopCaptureRecordingFrame(); /* a second run puts the page back */ return; } let host = makeCaptureRecordingFrame("div", { position: "fixed", top: "0", left: "0", width: "0", height: "0", "z-index": "2147483646", "pointer-events": "none" }); host.id = hostIdCaptureRecordingFrame; const mask = () => host.appendChild(makeCaptureRecordingFrame("div", { position: "fixed", background: "rgba(13,17,23,.6)", "pointer-events": "none" })); let masks = [mask(), mask(), mask(), mask()]; let edge = host.appendChild(makeCaptureRecordingFrame("div", { position: "fixed", "box-sizing": "border-box", outline: "2px dashed #d1242f", "pointer-events": "none" })); let panel = makeCaptureRecordingFrame("div", { position: "fixed", right: "12px", bottom: "12px", "z-index": "2147483647", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontCaptureRecordingFrame, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left", "max-width": "300px" }); panel.id = panelIdCaptureRecordingFrame; panel.setAttribute("role", "dialog"); panel.setAttribute("aria-label", "Recording frame"); panel.appendChild(makeCaptureRecordingFrame("strong", { display: "block" }, "Recording frame")); let said = panel.appendChild(makeCaptureRecordingFrame("span", { display: "block", color: "#d0d7de" }, "Choose a size.")); let row = panel.appendChild(makeCaptureRecordingFrame("div", { display: "flex", "flex-wrap": "wrap", gap: "6px", "margin-top": "6px" })); let state = window[stateKeyCaptureRecordingFrame] = { host: host, panel: panel, kept: [], size: null, onResize: null, onKey: null }; /* The frame: the chosen size in the screen's pixels where it fits the window, otherwise the same shape as large as fits, centred, with the rest dimmed. */ const draw = () => { if (!state.size) { return; } let dpr = window.devicePixelRatio || 1, vw = document.documentElement.clientWidth || window.innerWidth, vh = window.innerHeight; let w = state.size[0] / dpr, h = state.size[1] / dpr, fit = Math.min(1, vw / w, vh / h); w = Math.round(w * fit); h = Math.round(h * fit); let left = Math.floor((vw - w) / 2), top = Math.floor((vh - h) / 2); styleCaptureRecordingFrame(masks[0], { left: "0", top: "0", width: vw + "px", height: top + "px" }); styleCaptureRecordingFrame(masks[1], { left: "0", top: (top + h) + "px", width: vw + "px", height: Math.max(0, vh - top - h) + "px" }); styleCaptureRecordingFrame(masks[2], { left: "0", top: top + "px", width: left + "px", height: h + "px" }); styleCaptureRecordingFrame(masks[3], { left: (left + w) + "px", top: top + "px", width: Math.max(0, vw - left - w) + "px", height: h + "px" }); styleCaptureRecordingFrame(edge, { left: left + "px", top: top + "px", width: w + "px", height: h + "px" }); let drawnW = Math.round(w * dpr), drawnH = Math.round(h * dpr), exact = fit === 1; host.setAttribute("data-size", state.size[0] + "x" + state.size[1]); host.setAttribute("data-drawn", drawnW + "x" + drawnH); host.setAttribute("data-exact", exact ? "yes" : "no"); said.textContent = exact ? state.size[0] + " × " + state.size[1] + " at full size. Record the dashed box." : state.size[0] + " × " + state.size[1] + " is bigger than this window, so the box is the same shape at " + drawnW + " × " + drawnH + ". Make the window bigger, or zoom out, for full size."; }; const choose = (size) => { state.size = size; if (!host.parentNode) { (document.body || document.documentElement).appendChild(host); } draw(); }; sizesCaptureRecordingFrame.forEach(function(size) { let b = row.appendChild(makeCaptureRecordingFrame("button", { padding: "2px 8px", font: fontCaptureRecordingFrame, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, size[0] + " × " + size[1])); b.type = "button"; b.addEventListener("click", function() { choose(size); }); }); [["Hide panel", function() { styleCaptureRecordingFrame(panel, { display: "none" }); }], ["Off", stopCaptureRecordingFrame]].forEach(function(pair) { let b = row.appendChild(makeCaptureRecordingFrame("button", { padding: "2px 8px", font: fontCaptureRecordingFrame, color: "#ffffff", background: "transparent", border: "1px solid #8c959f", "border-radius": "4px", cursor: "pointer" }, pair[0])); b.type = "button"; b.addEventListener("click", pair[1]); }); /* The scrollbars go and the page still scrolls; where the browser cannot hide them that way, the page stops scrolling instead, and the panel says so. */ let root = document.documentElement, body = document.body; if (window.CSS && CSS.supports && CSS.supports("scrollbar-width", "none")) { changeCaptureRecordingFrame(state, root, "scrollbar-width", "none"); if (body) { changeCaptureRecordingFrame(state, body, "scrollbar-width", "none"); } } else { changeCaptureRecordingFrame(state, root, "overflow", "hidden"); if (body) { changeCaptureRecordingFrame(state, body, "overflow", "hidden"); } panel.appendChild(makeCaptureRecordingFrame("span", { display: "block", "margin-top": "4px", color: "#d0d7de" }, "This browser can only hide the scrollbars by stopping the page from scrolling, so scroll first.")); } state.onResize = draw; state.onKey = function(event) { if (event.key === "Escape") { event.stopPropagation(); stopCaptureRecordingFrame(); } }; window.addEventListener("resize", state.onResize); window.addEventListener("keydown", state.onKey, true); (document.body || document.documentElement).appendChild(panel); let asked = parseSizeCaptureRecordingFrame(sizeCaptureRecordingFrame); if (asked) { choose(asked); } } mainCaptureRecordingFrame(); })();