Created
September 20, 2017 11:54
-
-
Save LazyMammal/1c60c45e9df26602f688d025f3b20f0c to your computer and use it in GitHub Desktop.
Hide live chat by default on YouTube live streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name YouTube - Hide Live Chat | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Hide live chat by default on live streams | |
// @author LM | |
// @match https://www.youtube.com/watch* | |
// @run-at document-end | |
// @grant none | |
// ==/UserScript== | |
function AddClass() { | |
var el = document.getElementById("live-chat-iframe"); | |
if(el) { | |
el.parentElement.classList.add("yt-uix-expander-collapsed"); | |
console.log('Hide Live Chat'); | |
return true; | |
} | |
return false; | |
} | |
function KeepTrying(func, attempts, delay) { | |
console.log('Keep Trying ' + attempts); | |
if( !func() && attempts-1 > 0 ) { | |
window.setTimeout( function() { | |
KeepTrying(func, attempts-1, delay); | |
}); | |
} | |
} | |
(function() { | |
'use strict'; | |
KeepTrying( AddClass, 5, 10 ); | |
})(); |
It's one problem... It's add new chat after resize (cause there are different for desktop and mobile), sow you need to register keep trying in resize event window.addEventListener('resize', KeepTrying(PressHideButton, 12, 200));
Based off @milroneth's version with following improvements:
- Avoid opening live chat if it's closed by default (premiered video pages).
- Works when navigating to YouTube watch page from another YouTube page, which involves AJAX rather than new page load, via polling for URL changes and watching all non-embed YouTube pages.
// ==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);
})();
I'd love to see this as a repo so updates can be automatically received via a userscript manager.
Continuing this in https://gist.github.com/lbmaian/94824cef728917a53d3c6e6ea885469c
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated again to counteract YouTube's lazy loading of other elements than the video, based on original and bastiMQ's changes.
If your computer is too slow and it doesn't work, try increasing the 200 in that last line by 100 until it does.
EDIT: I just realized this version works for realtime Live chat only, but not for videos that premiered live and now have a "Live chat" replay section. Will look into it when I find the time unless someone else wants to step in and fix it instead.