Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jakobimatrix/0f6f91af94c3e787df88ffa46f0709fa to your computer and use it in GitHub Desktop.
Save Jakobimatrix/0f6f91af94c3e787df88ffa46f0709fa to your computer and use it in GitHub Desktop.
Turn off/disable YouTube autoplay feature
// To run, install GreaseMonkey or TamperMonkey extension in your browser
// Copy this code into new user script, and enable
// !! If the toggle button is not been toggled automaticly:
// !! That means youtube changed for reasons the id of the toggle button again.
// !! In Chrome/Firefox: right-click the toggle button and choose "Inspect Element (Q)"
// !! You will find something like "<div id="TOGGLE_BUTTON_ID" class="toggle-button...."
// !! Copy whatever TOGGLE_BUTTON_ID is and replace down in "function disableAfterLoad()" the place holder named "TOGGLE_BUTTON_ID".
// !! Save script ctrl+S and reload youtube page.
// ==UserScript==
// @name Disable Youtube autoplay
// @version 1.3
// @description This script turns off Youtube's newest autoplay feature after the page loads
// @author Jeff Bellucci, Jakob Wandel
// @match *://www.youtube.com/*
// @run-at document-end
// @grant none
// ==/UserScript==
(function() {
'use strict';
let debug = false;
let bottonIds = [];
let i = 0;
let MAX_TRIES = 5;
bottonIds[i++] = 'toggle';
bottonIds[i++] = 'toggleButton';
bottonIds[i++] = 'improved-toggle';
bottonIds[i++] = 'TOGGLE_BUTTON_ID';
function debugMsg(msg){
if(debug){
console.log(msg);
}
}
//https://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript
function eventFire(el, etype){
if (el.fireEvent) {
el.fireEvent('on' + etype);
} else {
var evObj = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
}
function disableAfterLoad(try_nr) {
for (let key in bottonIds) {
let autoplayToggle = document.getElementById(bottonIds[key]);
if (autoplayToggle) {
debugMsg("I found a Element called '"+bottonIds[key]+"'. I will now try to uncheck it:");
if (autoplayToggle.hasAttribute('checked')) {
eventFire(autoplayToggle, 'click');
debugMsg("I assume, that if '"+bottonIds[key]+"' was the right Element, its now untoggled!");
delete bottonIds[key];
return;
}else{
debugMsg("The element '"+bottonIds[key]+"' has no attribute checked. So I don't click it and check the other elements but not this one anymore.");
delete bottonIds[key];
}
} else {
debugMsg("I could not finde an Element called '"+bottonIds[key]+"'. Maybe the site is not loaded jet? I will try again in 1 second.");
}
}
if(try_nr > 0){
try_nr--;
setTimeout(function() {
disableAfterLoad(try_nr);
}, 1000);
}
}
disableAfterLoad(MAX_TRIES);
})();
@UMLAUTaxl
Copy link

Thanks. But this only works when the tab is in the background. As soon as I put it in foreground it plays.
Well, the userscript works. All is good. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment