Last active
January 20, 2018 17:30
-
-
Save VioletRed/d3678cdc700cd4d62fec to your computer and use it in GitHub Desktop.
Disable Youtube Autoplay
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
// ==UserScript== | |
// @name Disable YT Autoplay | |
// @namespace user@violet.local | |
// | |
// @description No more autoplay, that's it | |
// | |
// @date 2015-03-15 | |
// @version 1 | |
// @grant none | |
// @updateURL https://gist.github.com/VioletRed/d3678cdc700cd4d62fec/raw/no_yt_autoplay.user.js | |
// @include *.youtube.com/* | |
// @include *.youtu.be/* | |
// ==/UserScript== | |
// | |
// Disable yt autoplay | |
// Injects code into the global scope. | |
function inject(fn) { | |
var script = unsafeWindow.document.createElement('script'); | |
script.innerHTML = '(' + fn.toString() + ')();'; | |
unsafeWindow.document.body.appendChild(script); | |
} | |
// This function stops the video if we're not looking. Inject | |
// it into the page. Note that we can't access any variables outside the | |
// function here. | |
function kodi_pauseyt() { | |
// Returns the player API interface or false if it's not ready. | |
function kodi_getPlayer() { | |
if (window.document | |
&& window.document.getElementsByTagName | |
&& window.document.getElementsByTagName('video') | |
&& window.document.getElementsByTagName('video')[0]) { | |
player = window.document.getElementsByTagName('video')[0] | |
if (!player["pause"]) | |
return false; | |
} else { | |
if (window.yt && window.yt.player | |
&& window.yt.player.getPlayerByElement | |
&& window.yt.player.getPlayerByElement('player-api') | |
&& window.yt.player.getPlayerByElement('player-api')["pauseVideo"]) { | |
player = window.yt.player.getPlayerByElement('player-api') | |
} else { | |
return false; | |
} | |
} | |
return player; | |
} | |
// Listen for the end of the video. | |
function kodi_activate() { | |
// console.log("GOOOO"); | |
player = kodi_getPlayer(); | |
var last_paused = document.documentURI; | |
var target = window.document.getElementById("body"); | |
// create an observer instance | |
var observer = new MutationObserver(function(mutations) { | |
mutations.forEach(function(mutation) { | |
// console.log("How many "+mutation.addedNodes.length); | |
// console.log(mutation.type) | |
// console.log(mutation.addedNodes) // | |
if (mutation.type == "childList" && document.documentURI != last_paused | |
&& mutation.addedNodes.length > 0) { | |
if (mutation.addedNodes[0].tagName == "IFRAME") { | |
kodi_wait(0); | |
observer.disconnect(); | |
} | |
} | |
}); | |
}); | |
// configuration of the observer: | |
var config = { | |
childList : true, | |
characterData : true | |
}; | |
// pass in the target node, as well as the observer options | |
observer.observe(target, config); | |
// console.log("Active") | |
if (player['pause']) { // HTML5 | |
// console.log("HTML5") | |
setTimeout(function(){player.pause()}, 300); | |
} else { // Flash | |
// console.log("STUPID FLASH"); | |
setTimeout(function(){player.pauseVideo()}, 1200); | |
} | |
// if (document.hidden || document.mozHidden || document.webkitHidden) | |
// window.yt.player.getPlayerByElement('player-api').pauseVideo(); | |
} | |
// Wait until the YouTube API's ready. | |
function kodi_wait(count) { | |
// console.log("WAIT"); | |
if (count < 50) { | |
setTimeout(kodi_getPlayer() ? kodi_activate : function() {kodi_wait(count+1)}, 100); | |
} | |
} | |
kodi_wait(0); | |
}; | |
inject(kodi_pauseyt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you make scripts that set a timer to automatically pause/stop on all Youtube embed videos? Like pause a video after X seconds!!?