Last active
March 28, 2016 12:38
-
-
Save KoRMaK/f1e81bd8068fd28bd77f to your computer and use it in GitHub Desktop.
Allows you to run a playlist and override it with some automation
This file contains hidden or 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 Youtube Playlist Automation | |
| // @namespace liquid-software.com | |
| // @version 0.01 | |
| // @description Allows you to run a playlist and override it with some automation | |
| // @author You | |
| // @match https://www.youtube.com/*list=PLL2432vvJ7x-nG555LeuvpJ47eBnceLQt* | |
| // ==/UserScript== | |
| var big_data_hash = {}; //the override for the videos | |
| //example hash | |
| //video-id : video to substitute | |
| //{"Gxu248aD6PY": {"substitute_video": "-ecNe7eS-F8"}} | |
| //you can store other stuff in the hash like "playback_speed" if you want to increase the playback speed | |
| big_data_hash["Gxu248aD6PY"] = {"substitute_video": "-ecNe7eS-F8"}; //justice new lands | |
| big_data_hash["2uYs0gJD-LE"] = {"substitute_video": "Iw3aoIIKPps", "offset" : 13}; //mia bad girls, offset is in seconds | |
| big_data_hash["GiDsLRQg_g4"] = {"substitute_video": "UzcD3Hi11d4"}; //justice dvno | |
| //--------------------- functions --------------------------------- | |
| var already_inited = false; | |
| function onAutomationPlayerStateChange(event){ | |
| //removeEventListener doesn't work, so we have to be hacky | |
| if(already_inited){ | |
| return; | |
| } | |
| if(event.data == 1){ | |
| already_inited = true; | |
| automation_player.seekTo(0); | |
| //automation_player.playVideo(); | |
| main_yt_player.seekTo(0); | |
| main_yt_player.mute(); | |
| var video_id = get_video_id_from_url(main_yt_player.getVideoUrl()); | |
| if( big_data_hash[video_id]["offset"] ) | |
| { | |
| main_yt_player.seekTo(big_data_hash[video_id]["offset"]); | |
| } | |
| } | |
| } | |
| yt_player_state_change = function (event){ | |
| console.log("yt player state change called"); | |
| var video_id = get_video_id_from_url(main_yt_player.getVideoUrl()); | |
| if(event == -1) | |
| { | |
| automation_player = undefined; | |
| if( big_data_hash[video_id] ){ | |
| main_yt_player.pauseVideo(); | |
| main_yt_player.mute(); | |
| }else{ | |
| main_yt_player.unMute(); | |
| } | |
| } | |
| if(event == 1) | |
| { | |
| if((typeof(automation_player) == "undefined") && big_data_hash[video_id]) | |
| { | |
| main_yt_player.mute(); | |
| already_inited = false; | |
| $("#watch-description").prepend("<div id='automation_player'>automation player</div>") | |
| window.automation_player = new YT.Player('automation_player', { | |
| height: '30', | |
| width: '640', | |
| videoId: big_data_hash[video_id]["substitute_video"], | |
| events: { | |
| 'onReady': onAutomationReady, | |
| 'onStateChange': onAutomationPlayerStateChange | |
| } | |
| }); | |
| }else{ | |
| //main_yt_player.unMute(); | |
| } | |
| } | |
| } | |
| loadScript("https://code.jquery.com/jquery-2.2.2.min.js", scripts_loaded_init); | |
| //loadScript("https://www.youtube.com/iframe_api", onYouTubeIframeAPIReady()); | |
| function scripts_loaded(){ | |
| //$("#watch-description").prepend("<div id='automation_player'>stuff</div>") | |
| } | |
| function scripts_loaded_init(){ | |
| var tag = document.createElement('script'); | |
| tag.src = "https://www.youtube.com/iframe_api"; | |
| var firstScriptTag = document.getElementsByTagName('script')[0]; | |
| firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
| tag.onreadystatechange = youtube_script_loaded; | |
| tag.onload = youtube_script_loaded; | |
| } | |
| function youtube_script_loaded(){ | |
| debugger; | |
| setTimeout(onYouTubeIframeAPIReady, 500); | |
| setTimeout(grab_ytplayer, 500); | |
| setTimeout(function(){yt_player_state_change(1)}, 550); | |
| } | |
| function grab_ytplayer(){ | |
| main_yt_player = document.getElementById("movie_player"); | |
| main_yt_player.addEventListener("onStateChange", "yt_player_state_change"); | |
| console.log("main_yt_player.constructor.name" + main_yt_player.constructor.name); | |
| } | |
| function get_video_id_from_url(url){ | |
| var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; | |
| var match = url.match(regExp); | |
| if (match && match[2].length == 11) { | |
| return match[2]; | |
| } else { | |
| //error | |
| } | |
| } | |
| //copied from snackoverflow because I coudln't understand twitter's version | |
| //http://stackoverflow.com/questions/950087/include-a-javascript-file-in-another-javascript-file | |
| function loadScript(url, callback, id) | |
| { | |
| // Adding the script tag to the head as suggested before | |
| var head = document.getElementsByTagName('head')[0]; | |
| var script = document.createElement('script'); | |
| script.type = 'text/javascript'; | |
| script.src = url; | |
| script.id = id; | |
| // Then bind the event to the callback function. | |
| // There are several events for cross browser compatibility. | |
| script.onreadystatechange = callback; | |
| script.onload = callback; | |
| // Fire the loading | |
| head.appendChild(script); | |
| } | |
| window.main_yt_player; | |
| function onAutomationReady(event){ | |
| event.target.playVideo(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment