javascript:(function(){ /* ---- settings: the one place a reader edits ---- */ var useRawArrayAudioSimpleProfanityFilter = false; /* true: filter the words typed below, not the built-in list */ var useBothListsAudioSimpleProfanityFilter = false; /* true: filter the words typed below and the built-in list together */ var customWordListAudioSimpleProfanityFilter = []; /* for example ["heck", "darn"], read when either line above is true */ var muteMsAudioSimpleProfanityFilter = 1500; /* how long each filtered word mutes the video, in milliseconds; 0 holds the mute for as long as a filtered word is on screen */ var captionsAudioSimpleProfanityFilter = "mask"; /* the captions on screen: "mask", "hide" or "hideLine" */ /* AUDIO - SIMPLE PROFANITY FILTER: One click and it is on. While a video plays, it mutes the video when the captions show a filtered word, for a second and a half, and masks the word in the captions as ****. Every filtered word new on screen mutes again, so words close behind one another are each muted, and the mute ends early once no filtered word is left on screen. A word further along a caption that shows its whole line at once keeps the mute on until about when it is said. The settings at the top choose the words (the built-in list, your own, or both), how long a mute lasts (0: as long as a filtered word is on screen), and what the captions on screen do: "mask" masks the word, "hide" keeps the captions off the screen with display: none while the filter still reads them, and "hideLine" hides a caption only while it holds a filtered word. YouTube's own censored captions, written [ __ ], are filtered too. Run it again, or press Off, and it puts back everything it changed. It hears the words through the captions, the way the Advanced Profanity Filter browser extension (github.com/FrostCo/AdvancedProfanityFilter) does its audio muting: the video's own caption track, and the captions a player draws on the page. No code is taken from that project. No speech is analysed, and nothing leaves the page. The built-in list keeps harmless stand-in words, made into the filtered words when it runs. To take a word off, delete its line. */ /* ---- built-in list: harmless stand-ins, made into the filtered words at run time ---- */ var wordListAudioSimpleProfanityFilter = [ { word: "as", add: "s", match: "whole", ends: ["es", "hole", "holes"] }, { word: "bit", add: "ch", match: "start" }, { word: "cant", swap: ["a", "u"], match: "start" }, { word: "duck", swap: ["d", "f"], match: "any" }, { word: "shut", swap: ["u", "i"], match: "any" }, { word: "dam", add: "n", match: "any" }, { word: "custard", swap: ["cu", "ba"], match: "start" }, { word: "dock", swap: ["o", "i"], match: "whole", ends: ["s", "head", "heads"] }, { word: "kiss", swap: ["k", "p"], match: "start" }, { raw: "\\[\\s*_+\\s*\\]|\\(\\s*_+\\s*\\)|\\b_{2,}\\b", match: "any" } /* YouTube's own censoring, written [ __ ] */ ]; var badWordListAudioSimpleProfanityFilter = []; /* the filtered words, built by makeBadWordListAudioSimpleProfanityFilter */ var noteAudioSimpleProfanityFilter = ""; /* a line for the badge, such as an empty custom list */ /* ---- names this bookmarklet adds to the page ---- */ var stateKeyAudioSimpleProfanityFilter = "--__-unlikely_-_name-__--AudioSimpleProfanityFilter"; var badgeIdAudioSimpleProfanityFilter = "badge--__-unlikely_-_name-__--AudioSimpleProfanityFilter"; var maskAudioSimpleProfanityFilter = "****"; var hintDelayAudioSimpleProfanityFilter = 4000; /* how long to wait for a caption before asking for one */ var charsPerSecondAudioSimpleProfanityFilter = 12; /* a little under the pace of speech, so a guess at when a word is said runs late, not early */ var recentMaxAudioSimpleProfanityFilter = 40; /* how many caption lines are kept, to tell a line that grows from a new one */ var fontAudioSimpleProfanityFilter = "13px/1.45 -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif"; /************************************* SUPPORT FUNCTIONS *************************************/ /* The filtered words: the built-in list, the custom list in its place, or both together, as the settings at the top ask. A custom list that holds no word leaves the built-in one in use. */ const makeBadWordListAudioSimpleProfanityFilter = (list) => { if (badWordListAudioSimpleProfanityFilter.length > 0) { return badWordListAudioSimpleProfanityFilter; /* built already in this run */ } let both = useBothListsAudioSimpleProfanityFilter === true; let custom = []; if (both || useRawArrayAudioSimpleProfanityFilter === true) { custom = customWordListAudioSimpleProfanityFilter .map((w) => String(w).trim().toLowerCase()) .filter((w) => w !== "") .map((w) => ({ word: w, match: "start", ends: [] })); if (custom.length > 0 && !both) { badWordListAudioSimpleProfanityFilter = custom; return badWordListAudioSimpleProfanityFilter; } if (custom.length === 0) { noteAudioSimpleProfanityFilter = "The custom list is empty, so only the built-in list is in use."; } } /* swap first: an entry that adds nothing is not one that swaps; raw passes through as written */ badWordListAudioSimpleProfanityFilter = list.map(function(e) { if (e.raw) { return { raw: e.raw, match: e.match, ends: [] }; } return { word: e.swap ? e.word.replace(e.swap[0], e.swap[1]) : e.word + (e.add || ""), match: e.match, ends: e.ends || [] }; }).concat(custom); return badWordListAudioSimpleProfanityFilter; }; const escapeAudioSimpleProfanityFilter = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); /* The pattern, with g, for finding each word and masking it. Only replace() and matchAll() use it, and both leave its lastIndex at 0, where test() or exec() would carry it from one call to the next. A raw entry is its own pattern, joined in unescaped and unwrapped. */ const makePatternAudioSimpleProfanityFilter = (bad, flags) => new RegExp(bad.map((e) => { if (e.raw) { return e.raw; } const w = escapeAudioSimpleProfanityFilter(e.word); if (e.match === "any") { return "[a-z]*" + w + "[a-z]*"; } if (e.match === "whole") { return "\\b" + w + (e.ends.length > 0 ? "(?:" + e.ends.map(escapeAudioSimpleProfanityFilter).join("|") + ")?" : "") + "\\b"; } return "\\b" + w + "[a-z]*"; }).join("|"), flags); /* Style through the CSSOM, which a strict content security policy still allows. */ const styleAudioSimpleProfanityFilter = (el, css) => { Object.keys(css).forEach(function(k) { el.style.setProperty(k, css[k], "important"); }); return el; }; const makeAudioSimpleProfanityFilter = (tag, css, text) => { let el = document.createElement(tag); styleAudioSimpleProfanityFilter(el, css); if (text) { el.textContent = text; } return el; }; /* Parent element, stepping out of a shadow root to its host. */ const parentOfAudioSimpleProfanityFilter = (el) => { return el.parentElement || (el.getRootNode && el.getRootNode().host) || null; }; /* Whether an element holds the video, its shadow roots included. */ const holdsVideoAudioSimpleProfanityFilter = (el, video) => { for (let up = video; up; up = parentOfAudioSimpleProfanityFilter(up)) { if (up === el) { return true; } } return false; }; /* The page's videos: the document first, then open shadow roots and frames from the same site. A frame from another site is out of reach, so it is counted rather than read. */ const findVideosAudioSimpleProfanityFilter = (root, found) => { found = found || { videos: [], farFrames: 0 }; root.querySelectorAll("video").forEach(function(el) { found.videos.push(el); }); root.querySelectorAll("*").forEach(function(el) { if (el.shadowRoot) { findVideosAudioSimpleProfanityFilter(el.shadowRoot, found); } }); root.querySelectorAll("iframe, frame").forEach(function(frame) { let inner = null; try { inner = frame.contentDocument; } catch (err) { inner = null; } if (inner) { findVideosAudioSimpleProfanityFilter(inner, found); } else { found.farFrames++; } }); return found; }; /* The player around a video: the nearest ancestor at least the video's size, then on up through any wrapper the same size as that, which is where a player draws its captions. */ const playerOfAudioSimpleProfanityFilter = (video) => { let v = video.getBoundingClientRect(), el = parentOfAudioSimpleProfanityFilter(video), player = null; while (el && el !== document.documentElement) { let r = el.getBoundingClientRect(); if (r.width >= v.width && r.height >= v.height) { player = el; break; } el = parentOfAudioSimpleProfanityFilter(el); } if (!player) { player = parentOfAudioSimpleProfanityFilter(video) || document.body; } for (let up = parentOfAudioSimpleProfanityFilter(player); up && up !== document.body && up !== document.documentElement; up = parentOfAudioSimpleProfanityFilter(up)) { let a = player.getBoundingClientRect(), b = up.getBoundingClientRect(); if (Math.abs(a.width - b.width) > 2 || Math.abs(a.height - b.height) > 2) { break; } player = up; } return player; }; /* Every text node in a node a mutation added. */ const textNodesInAudioSimpleProfanityFilter = (node, out) => { if (node.nodeType === 3) { out.push(node); return out; } if (node.nodeType !== 1 && node.nodeType !== 11) { return out; } let walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null), t; while ((t = walker.nextNode())) { out.push(t); } return out; }; const isCaptionTrackAudioSimpleProfanityFilter = (t) => t.kind === "captions" || t.kind === "subtitles"; /*************************************** THE FILTER ***************************************/ /* The filtered words in a caption's text, line by line. Each is keyed by its line up to and including the word, so a line drawn again, or one that grows a word at a time, keeps the keys of the words it already showed, and only a word new on screen gets a key not seen before. lead guesses how long until the word is said, from how far it sits into the text just shown: past the longest line read before that this line starts with, or from the line's start. */ const wordsInAudioSimpleProfanityFilter = (state, text, isCaption) => { let out = []; text.split("\n").forEach(function(raw) { let line = raw.replace(/\s+/g, " ").trim(), low = line.toLowerCase(), from = 0; if (line === "") { return; } state.recent.forEach(function(r) { if (r.length > from && low.indexOf(r) === 0) { from = r.length; } }); Array.from(line.matchAll(state.mask)).forEach(function(m) { out.push({ key: low.slice(0, m.index + m[0].length), lead: Math.max(0, m.index - from) / charsPerSecondAudioSimpleProfanityFilter * 1000 }); }); if (isCaption && state.recent.indexOf(low) < 0) { state.recent.push(low); if (state.recent.length > recentMaxAudioSimpleProfanityFilter) { state.recent.shift(); } } }); return out; }; /* End this filter's mute. A video the reader muted is left as it is. */ const releaseAudioSimpleProfanityFilter = (watch) => { clearTimeout(watch.capTimer); watch.capTimer = 0; watch.until = 0; if (watch.mutedByUs && watch.video.muted) { watch.video.muted = false; } watch.mutedByUs = false; }; /* The filtered words on screen now, against the ones on screen before. A word new on screen mutes the video for muteMs from when it is due to be said, and a word close behind another pushes that end later, never earlier. The mute ends then, or once no filtered word is left on screen, whichever is first. A word both ways in show, as a player that draws the video's own track does, is counted once: the larger of the two counts for each key is what is on screen. */ const decideAudioSimpleProfanityFilter = (state, watch) => { let now = new Map(), fresh = [], v = watch.video; [watch.domHits, watch.cueHits].forEach(function(hits) { let counts = new Map(); hits.forEach(function(words) { words.forEach(function(w) { let n = (counts.get(w.key) || 0) + 1; counts.set(w.key, n); if (n > (watch.onScreen.get(w.key) || 0)) { fresh.push(w); } if (n > (now.get(w.key) || 0)) { now.set(w.key, n); } }); }); }); watch.onScreen = now; if (fresh.length > 0) { if (!v.muted) { v.muted = true; watch.mutedByUs = true; } if (watch.mutedByUs) { state.count++; showBadgeAudioSimpleProfanityFilter(state); let cap = Math.max(0, Number(muteMsAudioSimpleProfanityFilter) || 0); if (cap > 0) { let wait = cap + Math.max.apply(null, fresh.map((w) => w.lead)); if (Date.now() + wait > watch.until) { watch.until = Date.now() + wait; clearTimeout(watch.capTimer); watch.capTimer = setTimeout(function() { releaseAudioSimpleProfanityFilter(watch); }, wait); } } } } if (now.size === 0) { releaseAudioSimpleProfanityFilter(watch); } }; /* A caption has been read, so the badge stops asking for them. */ const heardAudioSimpleProfanityFilter = (state, text) => { if (state.heard || !/[a-z]/i.test(text)) { return; } state.heard = true; showBadgeAudioSimpleProfanityFilter(state); }; /* The caption-like elements around a text node inside the player, innermost first: text a player draws as captions, rather than a tooltip, a title or a menu, sits in a layer the player names as caption, subtitle, timedtext, text-track or cue. The climb stops at an element that holds the video, since a caption layer never does, while a player's own element can carry such a name (YouTube's carries ytp-modern-caption), and hiding it would hide the video. Every text the player adds is still filtered; this says whether the captions are on, and what to hide. */ const captionLayersAudioSimpleProfanityFilter = (node, watch) => { let out = []; for (let el = node.parentElement; el && el !== watch.player && !holdsVideoAudioSimpleProfanityFilter(el, watch.video); el = el.parentElement) { if (/caption|subtitle|timedtext|text-?track|cue/i.test(String(el.className) + " " + el.id)) { out.push(el); } } return out; }; /* "mask", "hide" or "hideLine", as the setting at the top asks. */ const captionsModeAudioSimpleProfanityFilter = () => { let m = String(captionsAudioSimpleProfanityFilter).toLowerCase(); return m === "hide" ? "hide" : m === "hideline" ? "hideLine" : "mask"; }; /* Take a caption element off the screen through the CSSOM, keeping the display it had. */ const hideAudioSimpleProfanityFilter = (state, el) => { if (state.hidden.has(el)) { return; } state.hidden.set(el, { value: el.style.getPropertyValue("display"), priority: el.style.getPropertyPriority("display") }); el.style.setProperty("display", "none", "important"); }; const showAgainAudioSimpleProfanityFilter = (state, el) => { let was = state.hidden.get(el); if (!was) { return; } state.hidden.delete(el); if (was.value) { el.style.setProperty("display", was.value, was.priority); } else { el.style.removeProperty("display"); } }; /* A track the browser draws is not something css can reach, so it is hidden by its mode, and the mode it had is kept to put back. */ const hideTrackAudioSimpleProfanityFilter = (state, t) => { if (t.mode !== "showing") { return false; } if (!state.modes.has(t)) { state.modes.set(t, t.mode); } t.mode = "hidden"; return true; }; /* The video's own caption track: read each active cue, and mask it where the track is showing. */ const onCueAudioSimpleProfanityFilter = (state, watch) => { let mode = captionsModeAudioSimpleProfanityFilter(); watch.cueHits = new Map(); watch.tracks.forEach(function(entry) { let t = entry.track, lineHit = false; if (mode === "hide") { hideTrackAudioSimpleProfanityFilter(state, t); } if (t.mode === "disabled" || !t.activeCues) { return; } Array.prototype.forEach.call(t.activeCues, function(cue) { if (typeof cue.text !== "string") { return; } let text = state.cueText.has(cue) ? state.cueText.get(cue) : cue.text; heardAudioSimpleProfanityFilter(state, text); let words = wordsInAudioSimpleProfanityFilter(state, text, true); if (words.length === 0) { return; } watch.cueHits.set(cue, words); lineHit = true; if (t.mode === "showing" && !state.cueText.has(cue)) { state.cueText.set(cue, cue.text); cue.text = cue.text.replace(state.mask, maskAudioSimpleProfanityFilter); } }); /* hideLine: off the screen while a filtered cue is on, and back once it has gone */ if (mode === "hideLine") { if (lineHit && hideTrackAudioSimpleProfanityFilter(state, t)) { watch.lineTracks.add(t); } if (!lineHit && watch.lineTracks.has(t)) { watch.lineTracks.delete(t); t.mode = "showing"; } } }); decideAudioSimpleProfanityFilter(state, watch); }; const listenToTrackAudioSimpleProfanityFilter = (state, watch, track) => { if (!isCaptionTrackAudioSimpleProfanityFilter(track) || watch.tracks.some((e) => e.track === track)) { return; } let listener = function() { onCueAudioSimpleProfanityFilter(state, watch); }; track.addEventListener("cuechange", listener); watch.tracks.push({ track: track, listener: listener }); }; /* Load the cues of the first caption track, without drawing them, when none is on. */ const turnOnTrackAudioSimpleProfanityFilter = (state, watch) => { let tracks = Array.prototype.filter.call(watch.video.textTracks || [], isCaptionTrackAudioSimpleProfanityFilter); if (tracks.length === 0 || tracks.some((t) => t.mode === "showing" || t.mode === "hidden")) { return; } state.modes.set(tracks[0], tracks[0].mode); tracks[0].mode = "hidden"; }; /* Captions a player draws as page elements: test only the text a mutation adds or changes. */ const checkTextAudioSimpleProfanityFilter = (state, watch, node) => { let parent = node.parentElement; if (!node.isConnected || !parent || (state.badge && state.badge.contains(node))) { return; } if (/^(SCRIPT|STYLE|NOSCRIPT|TEXTAREA)$/.test(parent.tagName)) { return; } let text = node.nodeValue || "", mine = state.masked.get(node); if (mine && text === mine.masked) { return; } /* its own edit, which it does not read again */ let layers = captionLayersAudioSimpleProfanityFilter(node, watch), mode = captionsModeAudioSimpleProfanityFilter(); if (layers.length) { heardAudioSimpleProfanityFilter(state, text); } /* hide: the whole caption layer off the screen, while the filter goes on reading it */ if (mode === "hide" && layers.length) { hideAudioSimpleProfanityFilter(state, layers[layers.length - 1]); } let words = wordsInAudioSimpleProfanityFilter(state, text, layers.length > 0); if (words.length > 0) { let masked = text.replace(state.mask, maskAudioSimpleProfanityFilter); state.masked.set(node, { original: text, masked: masked }); node.nodeValue = masked; watch.domHits.set(node, words); /* hideLine: the caption holding the word, until the word has gone */ if (mode === "hideLine") { let line = layers[0] || node.parentElement; hideAudioSimpleProfanityFilter(state, line); watch.lineEls.add(line); } } else { watch.domHits.delete(node); state.masked.delete(node); /* the page wrote over it, so there is nothing to put back */ } }; const onMutationsAudioSimpleProfanityFilter = (state, watch, records) => { let seen = []; records.forEach(function(rec) { if (rec.type === "characterData") { seen.push(rec.target); return; } rec.addedNodes.forEach(function(n) { textNodesInAudioSimpleProfanityFilter(n, seen); }); }); seen.forEach(function(node) { checkTextAudioSimpleProfanityFilter(state, watch, node); }); /* a line taken off the player no longer holds the mute */ watch.domHits.forEach(function(words, node) { if (!node.isConnected) { watch.domHits.delete(node); state.masked.delete(node); } }); watch.lineEls.forEach(function(el) { let holds = false; watch.domHits.forEach(function(words, node) { if (el.contains(node)) { holds = true; } }); if (!holds) { watch.lineEls.delete(el); showAgainAudioSimpleProfanityFilter(state, el); } }); decideAudioSimpleProfanityFilter(state, watch); }; /* Both ways in, for one video. */ const watchVideoAudioSimpleProfanityFilter = (state, video) => { if (state.watches.some((w) => w.video === video)) { return; } let watch = { video: video, player: playerOfAudioSimpleProfanityFilter(video), tracks: [], domHits: new Map(), cueHits: new Map(), onScreen: new Map(), mutedByUs: false, until: 0, observer: null, onVolume: null, onAddTrack: null, capTimer: 0, lineEls: new Set(), lineTracks: new Set() }; state.watches.push(watch); /* The reader unmuting during this filter's mute is theirs to keep, until the next filtered word. A change of volume alone leaves the mute as it is, so it still ends on time. */ watch.onVolume = function() { if (watch.mutedByUs && !video.muted) { releaseAudioSimpleProfanityFilter(watch); } }; video.addEventListener("volumechange", watch.onVolume); if (video.textTracks) { turnOnTrackAudioSimpleProfanityFilter(state, watch); Array.prototype.forEach.call(video.textTracks, function(t) { listenToTrackAudioSimpleProfanityFilter(state, watch, t); }); watch.onAddTrack = function(event) { listenToTrackAudioSimpleProfanityFilter(state, watch, event.track); turnOnTrackAudioSimpleProfanityFilter(state, watch); }; video.textTracks.addEventListener("addtrack", watch.onAddTrack); if (captionsModeAudioSimpleProfanityFilter() === "hide") { Array.prototype.forEach.call(video.textTracks, function(t) { if (isCaptionTrackAudioSimpleProfanityFilter(t)) { hideTrackAudioSimpleProfanityFilter(state, t); } }); } } watch.observer = new MutationObserver(function(records) { onMutationsAudioSimpleProfanityFilter(state, watch, records); }); watch.observer.observe(watch.player, { childList: true, subtree: true, characterData: true }); }; /*************************************** THE BADGE ***************************************/ /* What the badge says: on, how many times it has muted, and what it is waiting for. */ const showBadgeAudioSimpleProfanityFilter = (state) => { if (!state.badge) { return; } let n = state.count, hint = ""; state.countLine.textContent = "Muted " + n + (n === 1 ? " time" : " times"); if (state.watches.length === 0 && state.farFrames > 0) { hint = "The player is in a frame from another site, out of this filter's reach."; } else if (state.watches.length === 0) { hint = "No video here yet. A video the page adds later is filtered too."; } else if (!state.heard && state.hintDue) { hint = "Turn captions on (CC) so the filter can read them."; } let mode = captionsModeAudioSimpleProfanityFilter(); let notes = [noteAudioSimpleProfanityFilter, mode === "hide" ? "Captions are hidden while the filter reads them." : mode === "hideLine" ? "A caption with a filtered word is hidden while it shows." : ""].filter(Boolean).join(" "); state.noteLine.textContent = notes; styleAudioSimpleProfanityFilter(state.noteLine, { display: notes ? "block" : "none" }); state.hintLine.textContent = hint; styleAudioSimpleProfanityFilter(state.hintLine, { display: hint ? "block" : "none" }); }; const makeBadgeAudioSimpleProfanityFilter = (state) => { let badge = makeAudioSimpleProfanityFilter("div", { position: "fixed", right: "12px", bottom: "12px", "z-index": "2147483647", "max-width": "300px", padding: "8px 10px", background: "#1f2328", color: "#ffffff", font: fontAudioSimpleProfanityFilter, "border-radius": "6px", "box-shadow": "0 2px 8px rgba(0,0,0,.35)", "text-align": "left" }); badge.id = badgeIdAudioSimpleProfanityFilter; badge.setAttribute("role", "status"); badge.appendChild(makeAudioSimpleProfanityFilter("strong", { display: "block", "font-weight": "600" }, "Profanity filter on")); state.countLine = badge.appendChild(makeAudioSimpleProfanityFilter("span", { display: "block" })); state.noteLine = badge.appendChild(makeAudioSimpleProfanityFilter("span", { display: "none", "margin-top": "4px" })); state.hintLine = badge.appendChild(makeAudioSimpleProfanityFilter("span", { display: "none", "margin-top": "4px" })); let off = makeAudioSimpleProfanityFilter("button", { display: "block", "margin-top": "6px", padding: "2px 10px", font: fontAudioSimpleProfanityFilter, color: "#1f2328", background: "#ffffff", border: "0", "border-radius": "4px", cursor: "pointer" }, "Off"); off.type = "button"; off.addEventListener("click", function() { state.stop(); }); badge.appendChild(off); (document.body || document.documentElement).appendChild(badge); state.badge = badge; }; /* Off, and a second run: put back every change, then take the badge away. */ const stopAudioSimpleProfanityFilter = () => { let state = window[stateKeyAudioSimpleProfanityFilter]; if (!state) { return; } delete window[stateKeyAudioSimpleProfanityFilter]; clearTimeout(state.hintTimer); if (state.pageObserver) { state.pageObserver.disconnect(); } state.watches.forEach(function(watch) { if (watch.observer) { watch.observer.disconnect(); } watch.tracks.forEach(function(entry) { entry.track.removeEventListener("cuechange", entry.listener); }); if (watch.onAddTrack) { watch.video.textTracks.removeEventListener("addtrack", watch.onAddTrack); } watch.video.removeEventListener("volumechange", watch.onVolume); clearTimeout(watch.capTimer); if (watch.mutedByUs && watch.video.muted) { watch.video.muted = false; } }); state.hidden.forEach(function(was, el) { showAgainAudioSimpleProfanityFilter(state, el); }); state.modes.forEach(function(was, track) { if (track.mode === "hidden") { track.mode = was; } }); state.cueText.forEach(function(original, cue) { cue.text = original; }); state.masked.forEach(function(m, node) { if (node.isConnected && node.nodeValue === m.masked) { node.nodeValue = m.original; } }); if (state.badge && state.badge.parentNode) { state.badge.parentNode.removeChild(state.badge); } }; /*************************************** MAIN FUNCTION ***************************************/ function mainAudioSimpleProfanityFilter() { if (window[stateKeyAudioSimpleProfanityFilter]) { window[stateKeyAudioSimpleProfanityFilter].stop(); /* a second run turns it off */ return; } let bad = makeBadWordListAudioSimpleProfanityFilter(wordListAudioSimpleProfanityFilter); let state = window[stateKeyAudioSimpleProfanityFilter] = { stop: stopAudioSimpleProfanityFilter, mask: makePatternAudioSimpleProfanityFilter(bad, "gi"), watches: [], farFrames: 0, count: 0, heard: false, hintDue: false, hintTimer: 0, recent: [], modes: new Map(), cueText: new Map(), masked: new Map(), hidden: new Map(), pageObserver: null, badge: null, countLine: null, noteLine: null, hintLine: null }; makeBadgeAudioSimpleProfanityFilter(state); let found = findVideosAudioSimpleProfanityFilter(document); state.farFrames = found.farFrames; found.videos.forEach(function(video) { watchVideoAudioSimpleProfanityFilter(state, video); }); /* A video the page adds later, as a page that loads its player on a click does. */ state.pageObserver = new MutationObserver(function(records) { let before = state.watches.length; records.forEach(function(rec) { rec.addedNodes.forEach(function(n) { if (n.nodeType !== 1) { return; } if (n.tagName === "VIDEO") { watchVideoAudioSimpleProfanityFilter(state, n); } n.querySelectorAll("video").forEach(function(v) { watchVideoAudioSimpleProfanityFilter(state, v); }); }); }); if (state.watches.length !== before) { showBadgeAudioSimpleProfanityFilter(state); } }); state.pageObserver.observe(document.documentElement, { childList: true, subtree: true }); state.hintTimer = setTimeout(function() { state.hintDue = true; showBadgeAudioSimpleProfanityFilter(state); }, hintDelayAudioSimpleProfanityFilter); showBadgeAudioSimpleProfanityFilter(state); } mainAudioSimpleProfanityFilter(); })();