javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var showHiddenAudioListMediaSources = true; /* true: list players with no size on the page as well, as most audio is */ /* AUDIO - LIST MEDIA SOURCES: Lists every audio and video player on the page: what each is playing now, the address in its src, and every it offers, with the one in use marked. An address that starts with blob: is flagged. The page made it itself, often from a stream it puts together a piece at a time, and it means nothing outside this page, so it cannot be saved from its address. A live stream from a camera, a screen or a call has no address at all, and is named as one. Copy puts the list on the clipboard as text. Run it again, press Close or press Escape to take the list away. */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyAudioListMediaSources = "--__-unlikely_-_name-__--AudioListMediaSources"; var panelIdAudioListMediaSources = "panel--__-unlikely_-_name-__--AudioListMediaSources"; var fontAudioListMediaSources = "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 styleAudioListMediaSources = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeAudioListMediaSources = (tag, css, text) => { let el = document.createElement(tag); styleAudioListMediaSources(el, css); if (text) { el.textContent = text; } return el; }; /* Short name for an element, like video#reel.player */ const describeAudioListMediaSources = (el) => { let s = el.tagName.toLowerCase(); if (el.id) { s += "#" + el.id; } let cls = typeof el.className == "string" ? el.className.trim().split(/\s+/).filter(Boolean).slice(0, 2) : []; if (cls.length) { s += "." + cls.join("."); } return s; }; /* The page's players: the document first, then open shadow roots and frames from the same site. */ const findPlayersAudioListMediaSources = (root, out) => { out = out || []; root.querySelectorAll("video, audio").forEach(function(el) { if (showHiddenAudioListMediaSources || el.getClientRects().length > 0) { out.push(el); } }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findPlayersAudioListMediaSources(el.shadowRoot, out); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findPlayersAudioListMediaSources(inner, out); } }); return out; }; /* An address as the page means it. */ const resolveAudioListMediaSources = (url, el) => { try { return new URL(String(url).trim(), el.ownerDocument.baseURI).href; } catch (err) { return String(url); } }; /* What one player holds, and what a reader most needs to know about it. */ const readAudioListMediaSources = (el) => { let current = el.currentSrc || ""; let sources = Array.prototype.map.call(el.querySelectorAll("source"), function(s) { let url = s.getAttribute("src") ? resolveAudioListMediaSources(s.getAttribute("src"), el) : ""; return { url: url, type: s.getAttribute("type") || "", media: s.getAttribute("media") || "", used: !!url && url === current }; }); let flag = ""; if (el.srcObject) { flag = "A live stream, from a camera, a screen or a call: it has no address to save."; } else if (/^blob:/i.test(current)) { flag = "A blob: address the page made itself, often from a stream it puts together a piece at a time. It means nothing outside this page, so it cannot be saved from its address."; } else if (/^data:/i.test(current)) { flag = "An inline data: address: the media is written into the page itself."; } else if (!current) { flag = "Nothing chosen yet: the browser picks a source when the player loads."; } let state = el.paused ? "paused" : "playing"; if (el.readyState === 0) { state = "not loaded"; } let length = isFinite(el.duration) && el.duration > 0 ? el.duration.toFixed(3) + " s" : "length not known"; return { el: el, kind: el.tagName.toLowerCase(), name: describeAudioListMediaSources(el), title: el.title || el.getAttribute("aria-label") || "", current: current, src: el.getAttribute("src") ? resolveAudioListMediaSources(el.getAttribute("src"), el) : "", sources: sources, flag: flag, blob: /^blob:/i.test(current), state: state, length: length }; }; /* The list as text, for the clipboard. */ const asTextAudioListMediaSources = (items) => items.map(function(it, i) { let lines = [(i + 1) + ". " + it.kind + " " + it.name + (it.title ? " (" + it.title + ")" : "") + ", " + it.state + ", " + it.length, " now: " + (it.current || "nothing yet")]; if (it.src) { lines.push(" src: " + it.src); } it.sources.forEach(function(s, k) { lines.push(" source " + (k + 1) + ": " + (s.url || "no address") + (s.type ? " (" + s.type + ")" : "") + (s.used ? ", in use" : "")); }); if (it.flag) { lines.push(" note: " + it.flag); } return lines.join("\n"); }).join("\n\n"); /* Close, Escape and a second run: the list and its key listener go, and nothing else changed. */ const closeAudioListMediaSources = () => { let state = window[stateKeyAudioListMediaSources]; if (!state) { return; } delete window[stateKeyAudioListMediaSources]; document.removeEventListener("keydown", state.onKey, true); if (state.panel.parentNode) { state.panel.parentNode.removeChild(state.panel); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainAudioListMediaSources() { if (window[stateKeyAudioListMediaSources]) { closeAudioListMediaSources(); /* a second run takes the list away */ return; } let items = findPlayersAudioListMediaSources(document).map(readAudioListMediaSources); let panel = makeAudioListMediaSources("div", { position: "fixed", top: "12px", right: "12px", "z-index": "2147483647", width: "460px", "max-width": "calc(100vw - 24px)", "max-height": "calc(100vh - 24px)", overflow: "auto", padding: "10px 12px", background: "#1f2328", color: "#ffffff", font: fontAudioListMediaSources, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left", "box-sizing": "border-box" }); panel.id = panelIdAudioListMediaSources; panel.setAttribute("role", "dialog"); panel.setAttribute("aria-label", "Media sources"); let head = panel.appendChild(makeAudioListMediaSources("div", { display: "flex", "align-items": "center", gap: "8px", "margin-bottom": "8px" })); head.appendChild(makeAudioListMediaSources("strong", { "font-size": "15px", "margin-right": "auto" }, items.length + (items.length === 1 ? " player" : " players") + " on this page")); let said = makeAudioListMediaSources("span", { color: "#d0d7de" }); const button = (words, act) => { let b = makeAudioListMediaSources("button", { padding: "2px 10px", font: fontAudioListMediaSources, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, words); b.type = "button"; b.addEventListener("click", act); head.appendChild(b); }; button("Copy", function() { let text = asTextAudioListMediaSources(items); let copied = navigator.clipboard && navigator.clipboard.writeText ? navigator.clipboard.writeText(text) : Promise.reject(new Error("no clipboard")); copied.then(function() { said.textContent = "Copied."; }, function() { said.textContent = "The browser refused the clipboard."; }); }); button("Close", closeAudioListMediaSources); panel.appendChild(said); let list = panel.appendChild(makeAudioListMediaSources("ol", { margin: "0", padding: "0 0 0 20px" })); items.forEach(function(it) { let li = list.appendChild(makeAudioListMediaSources("li", { margin: "0 0 10px", "overflow-wrap": "anywhere" })); li.setAttribute("data-kind", it.kind); li.setAttribute("data-current", it.current); li.setAttribute("data-blob", it.blob ? "yes" : "no"); li.appendChild(makeAudioListMediaSources("strong", { display: "block" }, it.kind + " " + it.name + (it.title ? " (" + it.title + ")" : ""))); li.appendChild(makeAudioListMediaSources("span", { display: "block", color: "#d0d7de" }, it.state + ", " + it.length)); li.appendChild(makeAudioListMediaSources("span", { display: "block" }, "Now: " + (it.current || "nothing yet"))); if (it.src) { li.appendChild(makeAudioListMediaSources("span", { display: "block", color: "#d0d7de" }, "src: " + it.src)); } if (it.sources.length) { let ul = li.appendChild(makeAudioListMediaSources("ul", { margin: "2px 0 0", padding: "0 0 0 16px" })); it.sources.forEach(function(s) { let item = ul.appendChild(makeAudioListMediaSources("li", { color: s.used ? "#ffffff" : "#d0d7de" }, (s.url || "no address") + (s.type ? " (" + s.type + ")" : "") + (s.media ? " for " + s.media : "") + (s.used ? ", in use" : ""))); item.setAttribute("data-used", s.used ? "yes" : "no"); }); } if (it.flag) { li.appendChild(makeAudioListMediaSources("span", { display: "block", "margin-top": "2px", padding: "2px 6px", background: it.blob ? "#8c1d18" : "#3d444d", "border-radius": "3px" }, it.flag)); } }); if (!items.length) { list.appendChild(makeAudioListMediaSources("li", { "list-style": "none" }, "No audio or video players on this page. A player in a frame from another site is out of reach.")); } let state = window[stateKeyAudioListMediaSources] = { panel: panel, onKey: null }; state.onKey = function(event) { if (event.key === "Escape") { event.stopPropagation(); closeAudioListMediaSources(); } }; document.addEventListener("keydown", state.onKey, true); (document.body || document.documentElement).appendChild(panel); } mainAudioListMediaSources(); })();