youtube mute code not working
Unanswered
Munchkin posted this in #help-forum
MunchkinOP
could someone make a script for tampermonkey to mute on any variation of chees, that would be any variation of cheese. make the script to mute the line before the line containing the blacklisted phrase.
ive asked chatgpt to make this script and it keeps making non functional scripts.
ive asked chatgpt to make this script and it keeps making non functional scripts.
1 Reply
MunchkinOP
the existing code is
// ==UserScript==
// @name Mute on Cheese Debugging
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Debugging Mute on Cheese for YouTube captions
// @match https://www.youtube.com/watch*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const blacklistedRegex = /chees\w*/i;
let videoElement = document.querySelector('video');
let muted = false;
function muteVideo() {
if (videoElement && !muted) {
videoElement.muted = true;
muted = true;
console.log("Video muted for 20 seconds.");
setTimeout(() => {
videoElement.muted = false;
muted = false;
console.log("Video unmuted.");
}, 20000);
}
}
function checkTranscript() {
const transcriptElements = document.querySelectorAll('.ytp-caption-segment');
console.log("Checking transcript...");
if (transcriptElements.length > 0) {
const transcriptText = Array.from(transcriptElements).map(el => el.textContent.toLowerCase());
for (let i = 1; i < transcriptText.length; i++) {
if (blacklistedRegex.test(transcriptText[i])) {
console.log(
Blacklisted phrase detected: "${transcriptText[i]}");
muteVideo();
break;
}
}
}
}
function initialize() {
videoElement = document.querySelector('video');
if (!videoElement) {
console.log("Video element not found. Retrying...");
setTimeout(initialize, 1000);
return;
}
console.log("Video element found.");
setInterval(checkTranscript, 2000);
}
initialize();
})();