Repository Public License
1// ORIX Navigator — media state detection and tab mute.2// World: orix.bridge (isolated). Frames: all. Injection: document start.3// WKWebView has no public "is playing audio" property and no public page mute, so both are derived from the4// media elements of the document. Known gap (documented in WEBKIT_LIMITATIONS.md): elements never attached to5// the document (`new Audio()`) and Web Audio graphs are invisible from an isolated world.6(function () {7 "use strict";8 if (window.__orixMedia) return;9 var handler = window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.orixBridge;10 if (!handler) return;11 var frameID = Math.random().toString(36).slice(2) + Date.now().toString(36);12 var forcedMute = window.__orixInitialMute === true;13 var mutedByUs = new WeakSet();14 var known = new Set();15 var last = "";1617 function elements() {18 var live = [];19 document.querySelectorAll("video, audio").forEach(function (el) { known.add(el); });20 known.forEach(function (el) {21 if (el.isConnected || !el.paused) live.push(el); else known.delete(el);22 });23 return live;24 }25 function enforceMute(list) {26 list.forEach(function (el) {27 if (forcedMute) {28 if (!el.muted) { el.muted = true; mutedByUs.add(el); }29 } else if (mutedByUs.has(el)) {30 el.muted = false; mutedByUs.delete(el);31 }32 });33 }34 function report() {35 var list = elements();36 enforceMute(list);37 var playing = 0, audible = 0, pip = false;38 list.forEach(function (el) {39 var active = !el.paused && !el.ended && el.readyState > 2;40 if (!active) return;41 playing++;42 // An element we muted still counts as "would be audible": the tab keeps its (crossed) speaker badge.43 var silenced = el.volume === 0 || (el.muted && !mutedByUs.has(el));44 if (!silenced) audible++;45 if (document.pictureInPictureElement === el) pip = true;46 });47 var payload = { frame: frameID, total: list.length, playing: playing, audible: audible, pictureInPicture: pip, muted: forcedMute };48 var key = JSON.stringify(payload);49 if (key === last) return;50 last = key;51 try { handler.postMessage({ v: 1, kind: "media", payload: payload }); } catch (e) {}52 }53 function onMediaEvent(event) {54 var el = event.target;55 if (el && (el.tagName === "VIDEO" || el.tagName === "AUDIO")) known.add(el);56 report();57 }58 ["play", "playing", "pause", "ended", "volumechange", "emptied", "loadeddata", "enterpictureinpicture", "leavepictureinpicture"].forEach(function (type) {59 document.addEventListener(type, onMediaEvent, true);60 });61 // Elements present in the markup are only discoverable once the document is parsed.62 document.addEventListener("DOMContentLoaded", report);63 window.addEventListener("load", report);64 window.addEventListener("pagehide", function () {65 try { handler.postMessage({ v: 1, kind: "media", payload: { frame: frameID, total: 0, playing: 0, audible: 0, pictureInPicture: false, muted: forcedMute } }); } catch (e) {}66 last = "";67 });6869 window.__orixMedia = {70 setMuted: function (muted) { forcedMute = !!muted; report(); return forcedMute; },71 state: function () { report(); return last; }72 };73 report();74})();75