Forked from jeffmbellucci/disable_youtube_autoplay.js
Last active
September 9, 2019 16:20
-
-
Save Jakobimatrix/0f6f91af94c3e787df88ffa46f0709fa to your computer and use it in GitHub Desktop.
Turn off/disable YouTube autoplay feature
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
// 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); | |
})(); |
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
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/)