javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var fileNameVideoGrabFrame = ""; /* the saved file's name without .png; empty: the video's title and time */ var noteSecondsVideoGrabFrame = 5; /* how long the note saying what happened stays up */ /* VIDEO - GRAB FRAME: Saves the frame the largest video in view is showing as a PNG, at the video's own size, named from its title and the time of the frame. Run it again to save another. A video served from another site that does not allow its pictures to be read hands back no picture: the browser marks the copy "tainted" and refuses it, and the note says so plainly. A frame that comes out all black, as a video protected against copying does, is not saved either. */ /* ---- names this bookmarklet adds to the page ---- */ var noteIdVideoGrabFrame = "note--__-unlikely_-_name-__--VideoGrabFrame"; var fontVideoGrabFrame = "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 styleVideoGrabFrame = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; /* The page's videos: the document first, then open shadow roots and frames from the same site. */ const findVideosVideoGrabFrame = (root, out) => { out = out || []; root.querySelectorAll("video").forEach(function(el) { out.push(el); }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findVideosVideoGrabFrame(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findVideosVideoGrabFrame(inner, out); } }); return out; }; /* The video with the most of itself in view, which is the one being watched. */ const targetVideoGrabFrame = () => { let best = null, bestArea = 0; findVideosVideoGrabFrame(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 note saying what happened. A second run replaces it rather than adding another. */ const noteVideoGrabFrame = (words, result) => { let old = document.getElementById(noteIdVideoGrabFrame); if (old && old.parentNode) { old.parentNode.removeChild(old); } let note = document.createElement("div"); styleVideoGrabFrame(note, { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", "max-width": "340px", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontVideoGrabFrame, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left" }); note.id = noteIdVideoGrabFrame; note.setAttribute("role", "status"); note.setAttribute("data-result", result); note.textContent = words; (document.body || document.documentElement).appendChild(note); setTimeout(function() { if (note.parentNode) { note.parentNode.removeChild(note); } }, noteSecondsVideoGrabFrame * 1000); }; /* A file name from the video's title, or the page's, and the time of the frame. */ const fileNameOfVideoGrabFrame = (video) => { let base = String(fileNameVideoGrabFrame).trim() || String(video.title || document.title || "video") .toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "video"; return (String(fileNameVideoGrabFrame).trim() ? base : base + "-" + video.currentTime.toFixed(3) + "s") + ".png"; }; /* True when every pixel is black, as a video protected against copying draws. */ const allBlackVideoGrabFrame = (canvas) => { let data = canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height).data; for (let i = 0; i < data.length; i += 4) { if (data[i] > 8 || data[i + 1] > 8 || data[i + 2] > 8) { return false; } } return true; }; /* The save: a download link on a blob address this makes, taken back once the click is done. */ const saveVideoGrabFrame = (blob, name) => { let url = URL.createObjectURL(blob); let link = document.createElement("a"); link.href = url; link.download = name; styleVideoGrabFrame(link, { display: "none" }); (document.body || document.documentElement).appendChild(link); link.click(); link.parentNode.removeChild(link); setTimeout(function() { URL.revokeObjectURL(url); }, 1000); }; /*************************************** MAIN FUNCTION ***************************************/ function mainVideoGrabFrame() { let video = targetVideoGrabFrame(); if (!video) { noteVideoGrabFrame("There is no video in view. A player in a frame from another site is out of reach.", "none"); return; } if (video.readyState < 2 || !video.videoWidth) { noteVideoGrabFrame("The video has no frame to grab yet. Play it for a moment, then run this again.", "unloaded"); return; } let canvas = document.createElement("canvas"); canvas.width = video.videoWidth; canvas.height = video.videoHeight; let at = video.currentTime.toFixed(3), name = fileNameOfVideoGrabFrame(video); try { canvas.getContext("2d").drawImage(video, 0, 0, canvas.width, canvas.height); if (allBlackVideoGrabFrame(canvas)) { noteVideoGrabFrame("The frame at " + at + " s came out all black, as a video protected against copying does, so nothing was saved." + " If the frame really is black, play on a moment and run this again.", "black"); return; } canvas.toBlob(function(blob) { if (!blob) { noteVideoGrabFrame("The browser made no picture of the frame, so nothing was saved.", "refused"); return; } saveVideoGrabFrame(blob, name); noteVideoGrabFrame("Saved " + name + ", " + canvas.width + " × " + canvas.height + ", the frame at " + at + " s.", "saved"); }, "image/png"); } catch (err) { if (err && err.name === "SecurityError") { noteVideoGrabFrame("This video comes from another site that does not let a page read its pictures, so the browser" + " hands back none (the copy is \"tainted\"). Nothing was saved.", "tainted"); return; } noteVideoGrabFrame("The frame could not be saved: " + ((err && err.message) || "the browser refused") + ".", "refused"); } } mainVideoGrabFrame(); })();