Skip to content

Instantly share code, notes, and snippets.

@deluvas
Last active December 6, 2018 22:16
Show Gist options
  • Save deluvas/487b8aa508274b4dc64c974080b85830 to your computer and use it in GitHub Desktop.
Save deluvas/487b8aa508274b4dc64c974080b85830 to your computer and use it in GitHub Desktop.
A collection of tweaks to improve the YouTube user experience (greasemonkey)
// ==UserScript==
// @name mytube
// @namespace https://github.com/deluvas
// @version 0.3.0
// @description A collection of tweaks to improve the YouTube user experience
// @author deluvas
// @match *://www.youtube.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function () {
/**
* CUSTOMIZE THE SCRIPT BY MODIFYING THESE SETTINGS
*/
let settings = {
remove_recommended_videos: true,
remove_live_videos: true, // remove live videos from related (cause they're mostly unrelated)
remove_endscreen_videos: true,
disable_autoplay_playlist: true, // disable autoplaying the next video in a playlist
remove_player_navigation_buttons: true, // remove the prev and next buttons from the player
}
function removePlayerNavigationButtons() {
if ( location.pathname.match( /watch/ ) == false ) {
return;
}
let buttons = document.querySelectorAll( "a.ytp-prev-button,a.ytp-next-button" );
for ( let button of buttons ) {
button.remove();
}
console.info( "Removed player next/prev buttons" );
}
function disableAutoplayPlaylist() {
if ( location.href.match( /&list=/ ) == false ) {
return;
}
let player = document.querySelector( "div#player video" );
if ( player == null ) {
console.warn( "Cannot stop autoplay because player control was not found" );
return;
}
player.addEventListener( "timeupdate", ( e ) => {
let time = e.target.currentTime; // sec
let duration = e.target.duration; // sec
let video = e.target;
if ( video.paused || isFinite( duration ) == false ) {
return;
}
if ( time >= duration - 1.4 ) {
video.pause();
console.log( "Prevented from autoplaying the next video" );
}
})
console.log( "Adding playlist autoplay prevention" );
}
function removeRecommendedVideos() {
let count = 0;
let related = Array.from( document.querySelectorAll("li.related-list-item") );
for (let node of related) {
if (node.innerText.indexOf("Recommended for you") >= 0) {
node.remove();
count++;
}
}
console.debug( `Removed ${count} pesky recommended videos` );
}
function removeLiveVideos() {
let related = Array.from( document.querySelectorAll("li.related-list-item") );
for (let node of related) {
if ( node.querySelector( "span.yt-badge-live" ) !== null ) {
node.remove();
console.debug( `Removing pesky live video (${node.querySelector( "span.title" ).innerText.trim()})` );
}
}
}
function removeEndscreenVideos() {
let node = document.querySelector( ".html5-endscreen" );
if ( node == null ) {
console.warn( "Endscreen videos not found" );
// might have changed class or removed by other extension
return;
}
node.remove();
console.debug( "Removed pesky endscreen videos" );
}
if ( settings.remove_player_navigation_buttons ) {
document.addEventListener( "spfdone", removePlayerNavigationButtons );
removePlayerNavigationButtons();
}
if ( settings.disable_autoplay_playlist ) {
document.addEventListener( "spfdone", disableAutoplayPlaylist );
disableAutoplayPlaylist();
}
if ( settings.remove_recommended_videos ) {
document.addEventListener( "spfdone", () => {
if ( location.pathname.match( /\/watch/ ) ) {
removeRecommendedVideos();
}
})
removeRecommendedVideos();
}
if ( settings.remove_live_videos ) {
document.addEventListener( "spfdone", () => {
if ( location.pathname.match( /\/watch/ ) ) {
removeLiveVideos();
}
})
removeLiveVideos();
}
if ( settings.remove_endscreen_videos ) {
document.addEventListener( "spfdone", () => {
if ( location.pathname.match( /\/watch/ ) ) {
removeEndscreenVideos();
}
})
setTimeout( removeEndscreenVideos, 250 );
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment