javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var fasterKeyVideoPlaybackSpeed = "d"; /* the key that speeds the video up */ var slowerKeyVideoPlaybackSpeed = "s"; /* the key that slows it down */ var resetKeyVideoPlaybackSpeed = "r"; /* the key that puts it back to normal speed */ var stepVideoPlaybackSpeed = 0.25; /* how much one press changes the speed */ /* VIDEO - PLAYBACK SPEED: Set any HTML5 video from 0.25x to 4x with keys, beyond what a site's own speed menu offers. Each press acts on the largest video in view. A key typed into a text field, or pressed with Ctrl, Alt or the Windows or Command key, is left to the page. Run it again, or press Off, and the keys go back to the page. Each video keeps the speed it was set to, so press the reset key first to put it back. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyVideoPlaybackSpeed = "--__-unlikely_-_name-__--VideoPlaybackSpeed"; var badgeIdVideoPlaybackSpeed = "badge--__-unlikely_-_name-__--VideoPlaybackSpeed"; var slowestVideoPlaybackSpeed = 0.25; var fastestVideoPlaybackSpeed = 4; var fontVideoPlaybackSpeed = "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 styleVideoPlaybackSpeed = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeVideoPlaybackSpeed = (tag, css, text) => { let el = document.createElement(tag); styleVideoPlaybackSpeed(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 findVideosVideoPlaybackSpeed = (root, out) => { out = out || []; root.querySelectorAll("video").forEach(function(el) { out.push(el); }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findVideosVideoPlaybackSpeed(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findVideosVideoPlaybackSpeed(inner, out); } }); return out; }; /* The video with the most of itself in view, which is the one being watched. */ const targetVideoPlaybackSpeed = () => { let best = null, bestArea = 0; findVideosVideoPlaybackSpeed(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; }; /* 1.25 for 1.25, 4 for 4.00: the speed as a person writes it. */ const wordsVideoPlaybackSpeed = (rate) => String(Math.round(rate * 100) / 100); /* A key the reader is typing somewhere stays theirs: a text field, a select, an editable region. */ const typingVideoPlaybackSpeed = (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"); }; /* Off, and a second run: the keys go back to the page and the badge goes. */ const stopVideoPlaybackSpeed = () => { let state = window[stateKeyVideoPlaybackSpeed]; if (!state) { return; } delete window[stateKeyVideoPlaybackSpeed]; window.removeEventListener("keydown", state.onKey, true); document.removeEventListener("ratechange", state.onRate, true); if (state.badge.parentNode) { state.badge.parentNode.removeChild(state.badge); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainVideoPlaybackSpeed() { if (window[stateKeyVideoPlaybackSpeed]) { stopVideoPlaybackSpeed(); /* a second run turns it off */ return; } let badge = makeVideoPlaybackSpeed("div", { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontVideoPlaybackSpeed, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left" }); badge.id = badgeIdVideoPlaybackSpeed; badge.setAttribute("role", "status"); let speed = makeVideoPlaybackSpeed("strong", { display: "block", "font-size": "16px" }); let keys = makeVideoPlaybackSpeed("span", { display: "block", color: "#d0d7de" }, fasterKeyVideoPlaybackSpeed.toUpperCase() + " faster, " + slowerKeyVideoPlaybackSpeed.toUpperCase() + " slower, " + resetKeyVideoPlaybackSpeed.toUpperCase() + " normal"); let off = makeVideoPlaybackSpeed("button", { display: "block", "margin-top": "6px", padding: "2px 10px", font: fontVideoPlaybackSpeed, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, "Off"); off.type = "button"; off.addEventListener("click", stopVideoPlaybackSpeed); badge.appendChild(speed); badge.appendChild(keys); badge.appendChild(off); const show = (video) => { if (!video) { speed.textContent = "No video in view"; badge.removeAttribute("data-rate"); return; } speed.textContent = "Speed " + wordsVideoPlaybackSpeed(video.playbackRate) + "x"; badge.setAttribute("data-rate", wordsVideoPlaybackSpeed(video.playbackRate)); }; let state = window[stateKeyVideoPlaybackSpeed] = { badge: badge, onKey: null, onRate: null }; let faster = String(fasterKeyVideoPlaybackSpeed).toLowerCase(), slower = String(slowerKeyVideoPlaybackSpeed).toLowerCase(), reset = String(resetKeyVideoPlaybackSpeed).toLowerCase(); state.onKey = function(event) { if (event.ctrlKey || event.altKey || event.metaKey || typingVideoPlaybackSpeed(event)) { return; } let key = String(event.key || "").toLowerCase(), video = null; if (key !== faster && key !== slower && key !== reset) { return; } video = targetVideoPlaybackSpeed(); event.preventDefault(); event.stopPropagation(); if (!video) { show(null); return; } let rate = key === reset ? 1 : video.playbackRate + (key === faster ? stepVideoPlaybackSpeed : -stepVideoPlaybackSpeed); rate = Math.min(fastestVideoPlaybackSpeed, Math.max(slowestVideoPlaybackSpeed, Math.round(rate * 100) / 100)); video.playbackRate = rate; show(video); }; /* The site's own speed menu, or another player control, moves the speed too. */ state.onRate = function(event) { if (event.target === targetVideoPlaybackSpeed()) { show(event.target); } }; window.addEventListener("keydown", state.onKey, true); document.addEventListener("ratechange", state.onRate, true); (document.body || document.documentElement).appendChild(badge); show(targetVideoPlaybackSpeed()); } mainVideoPlaybackSpeed(); })();