javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var noteSecondsVideoForcePictureInPicture = 4; /* how long the note saying what happened stays up */ /* VIDEO - FORCE PICTURE IN PICTURE: Opens the largest video on the page in a picture-in-picture window, the small window that stays on top while other tabs and programs are used, including where the site marks its player disablePictureInPicture to refuse one. Run it again to close the window. When the window closes, the site's own setting goes back as it was. The click that runs it is what lets the page ask for the window, so it asks straight away. A video that has not loaded yet cannot open: play it for a moment, then run this again. */ /* ---- names this bookmarklet adds to the page ---- */ var noteIdVideoForcePictureInPicture = "note--__-unlikely_-_name-__--VideoForcePictureInPicture"; var fontVideoForcePictureInPicture = "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 styleVideoForcePictureInPicture = (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 findVideosVideoForcePictureInPicture = (root, out) => { out = out || []; root.querySelectorAll("video").forEach(function(el) { out.push(el); }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findVideosVideoForcePictureInPicture(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findVideosVideoForcePictureInPicture(inner, out); } }); return out; }; /* The video with the most area on the page. */ const largestVideoForcePictureInPicture = (list) => { let best = null, bestArea = -1; list.forEach(function(v) { let r = v.getBoundingClientRect(), area = r.width * r.height; if (area > bestArea) { best = v; bestArea = area; } }); return best; }; /* A short note saying what happened, which takes itself away. */ const noteVideoForcePictureInPicture = (words, result) => { let old = document.getElementById(noteIdVideoForcePictureInPicture); if (old && old.parentNode) { old.parentNode.removeChild(old); } let note = document.createElement("div"); styleVideoForcePictureInPicture(note, { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", "max-width": "320px", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontVideoForcePictureInPicture, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left" }); note.id = noteIdVideoForcePictureInPicture; 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); } }, noteSecondsVideoForcePictureInPicture * 1000); }; /* Put the site's own setting back once the window closes, however it is closed. */ const restoreVideoForcePictureInPicture = (video, hadIt) => { if (!hadIt) { return; } video.addEventListener("leavepictureinpicture", function() { video.setAttribute("disablepictureinpicture", ""); }, { once: true }); }; /*************************************** MAIN FUNCTION ***************************************/ function mainVideoForcePictureInPicture() { /* A second run, with a window open, closes it. */ if (document.pictureInPictureElement && document.exitPictureInPicture) { document.exitPictureInPicture().then(function() { noteVideoForcePictureInPicture("Picture-in-picture closed.", "closed"); }, function(err) { noteVideoForcePictureInPicture("The picture-in-picture window would not close: " + err.message, "refused"); }); return; } let video = largestVideoForcePictureInPicture(findVideosVideoForcePictureInPicture(document)); if (!video) { noteVideoForcePictureInPicture("There is no video on this page. A player in a frame from another site is out of reach.", "none"); return; } if (!document.pictureInPictureEnabled || !video.requestPictureInPicture) { noteVideoForcePictureInPicture("This browser has no picture-in-picture a page can ask for. Firefox offers its own button on the video instead.", "unsupported"); return; } /* The site's refusal is an attribute and a property; both go, and the window is asked for at once, while the click that ran this still counts as one. */ let hadIt = video.hasAttribute("disablepictureinpicture") || video.disablePictureInPicture === true; video.removeAttribute("disablepictureinpicture"); video.disablePictureInPicture = false; let asked; try { asked = video.requestPictureInPicture(); } catch (err) { asked = Promise.reject(err); } asked.then(function() { restoreVideoForcePictureInPicture(video, hadIt); noteVideoForcePictureInPicture("Picture-in-picture opened" + (hadIt ? ", although the site refuses it." : ".") + " Run this again to close it.", "opened"); }, function(err) { if (hadIt) { video.setAttribute("disablepictureinpicture", ""); } let why = err && err.name === "InvalidStateError" && video.readyState < 1 ? "the video has not loaded yet. Play it for a moment, then run this again." : err && err.name === "NotAllowedError" ? "the browser wants a click first. Click the page, then run this again." : (err && err.message) || "the browser refused."; noteVideoForcePictureInPicture("Picture-in-picture did not open: " + why, "refused"); }); } mainVideoForcePictureInPicture(); })();