javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var backKeyVideoFrameStep = ","; /* the key that steps back one frame */ var forwardKeyVideoFrameStep = "."; /* the key that steps forward one frame */ var assumedFpsVideoFrameStep = 30; /* the frame rate used until the video's own can be read */ /* VIDEO - FRAME STEP: Pauses the largest video in view and steps it one frame at a time, back or forward, with the time shown to the millisecond and the frame's number. A frame is one over the video's frame rate. Where the browser can say when each frame is shown (requestVideoFrameCallback), the rate is read from the video as it plays, so a video already playing is read for a few frames and then paused. Otherwise 30 frames a second is assumed, and the badge says which of the two it is using. A key typed into a text field is left to the page. Run it again, or press Off, to take it away; the video stays paused where it is. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyVideoFrameStep = "--__-unlikely_-_name-__--VideoFrameStep"; var badgeIdVideoFrameStep = "badge--__-unlikely_-_name-__--VideoFrameStep"; var framesToReadVideoFrameStep = 4; /* frames read before a playing video is paused */ var fontVideoFrameStep = "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 styleVideoFrameStep = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeVideoFrameStep = (tag, css, text) => { let el = document.createElement(tag); styleVideoFrameStep(el, css); if (text) { el.textContent = text; } return el; }; /* The page's videos: the document first, then open shadow roots and frames from the same site. */ const findVideosVideoFrameStep = (root, out) => { out = out || []; root.querySelectorAll("video").forEach(function(el) { out.push(el); }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findVideosVideoFrameStep(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findVideosVideoFrameStep(inner, out); } }); return out; }; /* The video with the most of itself in view, which is the one being watched. */ const targetVideoFrameStep = () => { let best = null, bestArea = 0; findVideosVideoFrameStep(document).forEach(function(v) { let r = v.getBoundingClientRect(); let w = Math.min(r.right, window.innerWidth) - Math.max(r.left, 0); let h = Math.min(r.bottom, window.innerHeight) - Math.max(r.top, 0); let area = w > 0 && h > 0 ? w * h : 0; if (area > bestArea) { best = v; bestArea = area; } }); return best; }; /* A key the reader is typing somewhere stays theirs: a text field, a select, an editable region. */ const typingVideoFrameStep = (event) => { let el = (event.composedPath && event.composedPath()[0]) || event.target; if (!el || el.nodeType !== 1) { return false; } if (el.isContentEditable) { return true; } let tag = el.tagName; if (tag === "TEXTAREA" || tag === "SELECT") { return true; } if (tag !== "INPUT") { return false; } return !/^(button|checkbox|color|file|image|radio|range|reset|submit)$/i.test(el.type || "text"); }; /* The middle of a list of numbers, which one late frame cannot move. */ const medianVideoFrameStep = (list) => { let sorted = list.slice().sort(function(a, b) { return a - b; }); return sorted[Math.floor(sorted.length / 2)]; }; /* Off, and a second run: keys, listeners and the badge go, and the video stays where it is. */ const stopVideoFrameStep = () => { let state = window[stateKeyVideoFrameStep]; if (!state) { return; } delete window[stateKeyVideoFrameStep]; window.removeEventListener("keydown", state.onKey, true); clearTimeout(state.giveUp); if (state.video) { ["seeked", "timeupdate", "pause", "play"].forEach(function(type) { state.video.removeEventListener(type, state.show); }); if (state.frameCall && state.video.cancelVideoFrameCallback) { state.video.cancelVideoFrameCallback(state.frameCall); } } if (state.badge.parentNode) { state.badge.parentNode.removeChild(state.badge); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainVideoFrameStep() { if (window[stateKeyVideoFrameStep]) { stopVideoFrameStep(); /* a second run turns it off */ return; } let video = targetVideoFrameStep(); let canRead = !!(video && video.requestVideoFrameCallback); let state = window[stateKeyVideoFrameStep] = { video: video, frame: 1 / assumedFpsVideoFrameStep, anchor: 0, read: false, gaps: [], last: null, frameCall: 0, giveUp: 0, badge: null, show: null, onKey: null }; let badge = state.badge = makeVideoFrameStep("div", { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontVideoFrameStep, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left" }); badge.id = badgeIdVideoFrameStep; badge.setAttribute("role", "status"); badge.appendChild(makeVideoFrameStep("strong", { display: "block" }, "Frame step")); let when = badge.appendChild(makeVideoFrameStep("span", { display: "block", "font-size": "16px", "font-variant-numeric": "tabular-nums" })); let rate = badge.appendChild(makeVideoFrameStep("span", { display: "block", color: "#d0d7de" })); let row = badge.appendChild(makeVideoFrameStep("div", { display: "flex", gap: "6px", "margin-top": "6px" })); badge.appendChild(makeVideoFrameStep("span", { display: "block", "margin-top": "4px", color: "#d0d7de" }, backKeyVideoFrameStep + " and " + forwardKeyVideoFrameStep + " step a frame")); /* The frame on screen: its number from the start, counted from the last frame the video showed. */ const indexAt = (t) => Math.floor((t - state.anchor) / state.frame + 1e-6); state.show = () => { if (!video) { when.textContent = "No video in view"; return; } let t = video.currentTime, fps = Math.round(100 / state.frame) / 100; when.textContent = t.toFixed(3) + " s, frame " + Math.round(t / state.frame); rate.textContent = state.read ? fps + " fps, read from the video" : canRead ? fps + " fps assumed until the video plays" : fps + " fps assumed: this browser cannot say when a frame is shown"; badge.setAttribute("data-time", t.toFixed(3)); badge.setAttribute("data-fps", String(fps)); badge.setAttribute("data-fps-from", state.read ? "video" : "assumed"); }; const step = (by) => { if (!video) { return; } if (!video.paused) { video.pause(); } let k = Math.max(0, indexAt(video.currentTime) + by); let t = state.anchor + k * state.frame; /* A microsecond past the frame's start, so a start a hair short in floating point cannot show the frame before it. */ t = Math.round(t * 1e6 + 1) / 1e6; if (isFinite(video.duration)) { t = Math.min(t, video.duration); } video.currentTime = Math.max(0, t); state.show(); }; /* Line up on the start of the frame on screen, so a step moves exactly one. */ const pauseOnFrame = () => { if (!video.paused) { video.pause(); } step(0); }; const button = (words, act) => { let b = makeVideoFrameStep("button", { padding: "2px 10px", font: fontVideoFrameStep, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, words); b.type = "button"; b.addEventListener("click", act); row.appendChild(b); }; button("Back a frame", function() { step(-1); }); button("Forward a frame", function() { step(1); }); button("Off", stopVideoFrameStep); state.onKey = function(event) { if (event.ctrlKey || event.altKey || event.metaKey || typingVideoFrameStep(event)) { return; } if (event.key !== backKeyVideoFrameStep && event.key !== forwardKeyVideoFrameStep) { return; } event.preventDefault(); event.stopPropagation(); step(event.key === forwardKeyVideoFrameStep ? 1 : -1); }; window.addEventListener("keydown", state.onKey, true); (document.body || document.documentElement).appendChild(badge); if (!video) { state.show(); return; } ["seeked", "timeupdate", "pause", "play"].forEach(function(type) { video.addEventListener(type, state.show); }); /* The frame rate, from the gap between two frames shown one after the other, on every frame the video shows while this is on. A playing video is paused once enough have been read. */ let pauseWhenRead = !video.paused; const onFrame = (now, meta) => { if (state.last && meta.presentedFrames === state.last.presentedFrames + 1 && meta.mediaTime > state.last.mediaTime) { state.gaps.push(meta.mediaTime - state.last.mediaTime); if (state.gaps.length > 12) { state.gaps.shift(); } if (state.gaps.length >= framesToReadVideoFrameStep - 1) { state.frame = Math.round(medianVideoFrameStep(state.gaps) * 1e6) / 1e6; state.read = true; } } state.last = { presentedFrames: meta.presentedFrames, mediaTime: meta.mediaTime }; state.anchor = Math.round((meta.mediaTime - Math.floor(meta.mediaTime / state.frame + 1e-6) * state.frame) * 1e6) / 1e6; if (pauseWhenRead && state.read) { pauseWhenRead = false; pauseOnFrame(); } state.show(); state.frameCall = video.requestVideoFrameCallback(onFrame); }; if (canRead) { state.frameCall = video.requestVideoFrameCallback(onFrame); /* A video that shows no frames, as in a tab out of sight, is paused on the rate assumed. */ if (pauseWhenRead) { state.giveUp = setTimeout(function() { if (pauseWhenRead) { pauseWhenRead = false; pauseOnFrame(); } }, 1500); } } else { pauseOnFrame(); } state.show(); } mainVideoFrameStep(); })();