Skip to content

Instantly share code, notes, and snippets.

@tobia
Created November 29, 2021 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobia/f6a2aa9c7828706fc0a48bc55d834654 to your computer and use it in GitHub Desktop.
Save tobia/f6a2aa9c7828706fc0a48bc55d834654 to your computer and use it in GitHub Desktop.
YouTube - Hide Live Chat (by @lbmaian)
// ==UserScript==
// @name YouTube - Hide Live Chat
// @namespace https://gist.github.com/LazyMammal/1c60c45e9df26602f688d025f3b20f0c#gistcomment-3656586
// @version 0.4
// @description Hide live chat by default on live streams
// @author LM, bastiMQ, IrisNebula, lbmaian
// @match https://www.youtube.com/*
// @exclude https://www.youtube.com/embed/*
// @run-at document-end
// @grant none
// @icon https://www.youtube.com/favicon.ico
// ==/UserScript==
function log(str) {
console.log('[YouTube - Hide Live Chat] ' + str);
}
function pressHideButton() {
var el = document.getElementById('chat');
if(el) {
if(el.hasAttribute('collapsed')) {
log('Live Chat already hidden');
return true;
} else {
el = document.getElementById('show-hide-button');
if(el) {
while(el.children.length > 0) {
if(el.firstChild.getAttribute('id') == 'button') {
el.firstChild.click();
log('Live Chat hidden');
return true;
}
el = el.firstChild;
}
}
}
}
return false;
}
function keepTrying(func, attempts, delayMillis) {
log('Trying to hide Live Chat, remaining attempts: ' + attempts);
if(!func() && (attempts-1 > 0)) {
window.setTimeout(function() {
keepTrying(func, attempts-1, delayMillis);
}, delayMillis);
}
}
function checkForWatchPage(url) {
if(url.startsWith('https://www.youtube.com/watch')) {
log('Matched ' + url);
keepTrying(pressHideButton, 12, 200);
}
}
(function() {
'use strict';
// Navigating to YouTube watch page can happen via AJAX rather than new page load, so keep polling for URL changes.
// This also requires watching all YouTube pages, rather than just watching https://www.youtube.com/watch*
var curURL = document.URL;
window.setInterval(function() {
var newURL = document.URL;
if(curURL != newURL) {
curURL = newURL;
checkForWatchPage(curURL);
}
}, 500);
checkForWatchPage(curURL);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment