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

UMLAUTaxl commented Sep 1, 2019

Doesn't work on google-chrome-stable 75.0.3770.142-1 amd64
with Tampermonkey v4.8.41

@Jakobimatrix
Copy link
Author

Thank you for your feedback! Yes Youtube changed things and now there is no click() function anymore at the toggle button to call.
So one has to attach it before calleing it. Thus Firefox had the same problem.

New version is v1.3. I tested it on Firefox and Chrome (newest public builds)

Also there is now a "debug mode" . Just set the variable debug to true and see the console.log

@UMLAUTaxl
Copy link

UMLAUTaxl commented Sep 7, 2019

Oh sorry, I misunderstood. I thought this would stop clips from playing automatically when I load the page.
(This is the simplest, working one I found: https://greasyfork.org/en/scripts/36732-autoplay-disabled-for-youtube)

@Jakobimatrix
Copy link
Author

Ah, Yes sorry this scripts stops only YouTube from auto playing the next video. But your problem can be solved within the Browser Settings:
Firefox: https://support.mozilla.org/en-US/kb/block-autoplay (thats per default activated)
Chrome: "Launch Chrome and in the Address Bar type: chrome://flags/#autoplay-policy and hit Enter. That will bring you directly to the flag that you need to change. From the drop-down box, change the setting from Default to “Document user activation is required” and relaunch the browser." (source: https://www.groovypost.com/howto/disable-autoplay-videos-on-sites-in-google-chrome/)

@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