javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var startAVideoLoopSection = ""; /* seconds, as "12.5", to start with A set; empty: set it with the button */ var endBVideoLoopSection = ""; /* seconds, as "20", to start with B set; empty: set it with the button */ /* VIDEO - LOOP SECTION: Loops part of a video. Press Set A where the part starts and Set B where it ends, and from then on the video goes back to A each time it reaches B, on the largest video in view. Clear stops the loop and keeps playing. Run it again, or press Off, to take it all away. A and B can be set at the top of the file instead, in seconds, for a part played over and over, such as a passage of music being learned. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyVideoLoopSection = "--__-unlikely_-_name-__--VideoLoopSection"; var badgeIdVideoLoopSection = "badge--__-unlikely_-_name-__--VideoLoopSection"; var fontVideoLoopSection = "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 styleVideoLoopSection = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeVideoLoopSection = (tag, css, text) => { let el = document.createElement(tag); styleVideoLoopSection(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 findVideosVideoLoopSection = (root, out) => { out = out || []; root.querySelectorAll("video").forEach(function(el) { out.push(el); }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findVideosVideoLoopSection(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findVideosVideoLoopSection(inner, out); } }); return out; }; /* The video with the most of itself in view, which is the one being watched. */ const targetVideoLoopSection = () => { let best = null, bestArea = 0; findVideosVideoLoopSection(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 setting in seconds, or null when it is empty or not a number. */ const secondsVideoLoopSection = (text) => { let n = parseFloat(String(text).trim()); return isFinite(n) && n >= 0 ? n : null; }; /* Off, and a second run: the loop, its listeners and the badge go, and the video plays on. */ const stopVideoLoopSection = () => { let state = window[stateKeyVideoLoopSection]; if (!state) { return; } delete window[stateKeyVideoLoopSection]; cancelAnimationFrame(state.frame); if (state.video) { state.video.removeEventListener("timeupdate", state.check); state.video.removeEventListener("ended", state.onEnded); } if (state.badge.parentNode) { state.badge.parentNode.removeChild(state.badge); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainVideoLoopSection() { if (window[stateKeyVideoLoopSection]) { stopVideoLoopSection(); /* a second run turns it off */ return; } let video = targetVideoLoopSection(); let state = window[stateKeyVideoLoopSection] = { video: video, a: secondsVideoLoopSection(startAVideoLoopSection), b: secondsVideoLoopSection(endBVideoLoopSection), loops: 0, frame: 0, badge: null, check: null, onEnded: null }; let badge = state.badge = makeVideoLoopSection("div", { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontVideoLoopSection, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left" }); badge.id = badgeIdVideoLoopSection; badge.setAttribute("role", "status"); badge.appendChild(makeVideoLoopSection("strong", { display: "block" }, "Loop section")); let points = badge.appendChild(makeVideoLoopSection("span", { display: "block", "font-variant-numeric": "tabular-nums" })); let said = badge.appendChild(makeVideoLoopSection("span", { display: "block", color: "#d0d7de" })); let row = badge.appendChild(makeVideoLoopSection("div", { display: "flex", gap: "6px", "margin-top": "6px" })); const show = () => { let looping = video && state.a !== null && state.b !== null; points.textContent = "A " + (state.a === null ? "not set" : state.a.toFixed(3) + " s") + ", B " + (state.b === null ? "not set" : state.b.toFixed(3) + " s"); said.textContent = !video ? "No video in view." : looping ? "Looping, " + state.loops + (state.loops === 1 ? " time so far" : " times so far") : "Set A and B to loop the part between them."; badge.setAttribute("data-looping", looping ? "yes" : "no"); badge.setAttribute("data-loops", String(state.loops)); }; /* B before A is the same part, named the other way round. */ const order = () => { if (state.a !== null && state.b !== null && state.b < state.a) { let t = state.a; state.a = state.b; state.b = t; } }; const button = (words, act) => { let b = makeVideoLoopSection("button", { padding: "2px 10px", font: fontVideoLoopSection, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, words); b.type = "button"; b.addEventListener("click", act); row.appendChild(b); }; button("Set A", function() { if (video) { state.a = video.currentTime; order(); show(); } }); button("Set B", function() { if (video) { state.b = video.currentTime; order(); show(); } }); button("Clear", function() { state.a = null; state.b = null; show(); }); button("Off", stopVideoLoopSection); if (state.b !== null && state.a === null) { state.a = 0; } order(); if (video) { /* Back to A on reaching B: on every frame while the tab is shown, and on each time update while it is not, when frames stop. */ state.check = function() { if (state.a === null || state.b === null || state.b - state.a < 0.05) { return; } if (video.currentTime >= state.b) { video.currentTime = state.a; state.loops++; show(); } }; state.onEnded = function() { if (state.a === null || state.b === null) { return; } video.currentTime = state.a; state.loops++; show(); let playing = video.play(); if (playing && playing.catch) { playing.catch(function() { }); } }; const everyFrame = () => { state.check(); state.frame = requestAnimationFrame(everyFrame); }; video.addEventListener("timeupdate", state.check); video.addEventListener("ended", state.onEnded); state.frame = requestAnimationFrame(everyFrame); } (document.body || document.documentElement).appendChild(badge); show(); } mainVideoLoopSection(); })();