// ==UserScript==
// @name Disable Youtube Autoplay Material Design
// @namespace https://kubath.com
// @include *youtube.com/*
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
/*
* This script disables Youtube's autoplay feature on the new 2017 material design page.
* The new design is currently in test and will not be shown to every user.
* To enforce Youtube's new material design run the command below in the console and reload the page.
* document.cookie="PREF=f6=4;path=/;domain=.youtube.com";
*
* This script will also disable autoplay on Youtube's previous page (prior to 2017).
*
* Bonus: To hide the cookie consent header uncomment setCookieConsentHideCookie(); below.
*/
var cookieDomainValue = '.youtube.com';
var prefCookieKey = 'PREF';
var prefCookieAutoplayToggleKey = 'f5';
var prefCookieAutoplayToggleValueAutonavDisabled = 30000;
setAutonavDisabledCookie(); // set autonav_disable cookie
setAutonavDisabledPrefCookie(); // set autoplay pref cookie (f5 = 30000)
// setCookieConsentHideCookie(); // uncomment to hide cookie consent bar
removeAutonavElement(); // remove autoplay element
// get cookies
function getCookie(name) {
var value = '; ' + document.cookie;
var parts = value.split('; ' + name + '=');
if (parts.length == 2) return parts.pop().split(';').shift();
}
// wait for the autoplay bar to appear and remove it
function removeAutonavElement() {
var mutationObserver = new MutationObserver(function (mutations, mutationObserverInstance) {
var element = document.getElementById('head');
if (element) {
element.remove();
mutationObserverInstance.disconnect();
return;
}
});
mutationObserver.observe(document, {
childList: true,
subtree: true
});
}
// set the autonav_disabled cookie (which appears sometimes but does not seem to have any effect)
function setAutonavDisabledCookie() {
document.cookie = 'autonav_disabled=true; path=/; domain=' + cookieDomainValue;
}
// get the PREF cookie, search for the f5 key and set the required value to disable Youtube autoplay
function setAutonavDisabledPrefCookie() {
var input = getCookie(prefCookieKey);
var output = '';
if (input && input.indexOf('=') !== -1) {
var inputArray = input.split('&');
var outputArray = {};
var found = false;
for (var i = 0; i < inputArray.length; i++) {
var temp = inputArray[i].split('=');
if(!temp[1]){
temp[1] = '';
}
outputArray[temp[0]] = temp[1];
}
for (var key in outputArray) {
if (key == prefCookieAutoplayToggleKey) {
found = true;
outputArray[key] = prefCookieAutoplayToggleValueAutonavDisabled;
}
if (output == '') {
output = key + '=' + outputArray[key];
} else {
output = output + '&' + key + '=' + outputArray[key];
}
}
}
if (!found) {
if(output == ''){
output = 'f5=30000';
}else{
output = output + '&f5=30000';
}
}
document.cookie = prefCookieKey + '=' + output + '; path=/; domain=' + cookieDomainValue;
}
// bonus: set the "remind me later" cookie for the cookie consent bar
function setCookieConsentHideCookie(){
document.cookie = 'HideTicker=true; path=/; domain=' + cookieDomainValue;
}