Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name GPForums Youtube Autoplay
- // @namespace awt
- // @description Autoplay embedded YouTube clips one at a time
- // @include http://www.gpforums.co.nz/*
- // ==/UserScript==
- /* Define all unsafeWindow functions before the rest of script (thanks ED) */
- unsafeWindow.onYouTubePlayerReady = function(playerid){
- if (unsafeWindow.totalNo == undefined){
- unsafeWindow.totalNo = 0;
- } else {
- unsafeWindow.totalNo++;
- }
- player = document.getElementById(playerid).wrappedJSObject;
- // Add Event Listener for State Change and for situation where a video errors.
- player.addEventListener("onStateChange", "whenStateChanges");
- player.addEventListener("onError", "whenError");
- if (unsafeWindow.totalNo == unsafeWindow.totalClips) {
- alert(unsafeWindow.totalClips + " YouTube clips lined up. Starting first video now.");
- unsafeWindow.currentPlayer = 0;
- player = document.getElementById("0").wrappedJSObject.playVideo();
- }
- }
- unsafeWindow.whenStateChanges = function(state) {
- if (state == 0){ // Called when clip ends (state = 0)
- unsafeWindow.nextClip();
- }
- }
- unsafeWindow.whenError = function() {
- // Called if a clip doesn't play due to an error
- unsafeWindow.nextClip();
- }
- // Plays the next unique clip on the page
- unsafeWindow.nextClip = function(){
- unsafeWindow.currentPlayer++;
- document.getElementById(unsafeWindow.currentPlayer).wrappedJSObject.playVideo();
- unsafeWindow.scrollTo(0, (unsafeWindow.getOffset( document.getElementById(unsafeWindow.currentPlayer) ).top - 200));
- }
- // To get the XY coordinates of an element
- // ** Thanks to meouw and cwallenpoole at stackoverflow **
- unsafeWindow.getOffset = function(el) {
- var _x = 0;
- var _y = 0;
- while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
- _x += el.offsetLeft - el.scrollLeft;
- _y += el.offsetTop - el.scrollTop;
- el = el.offsetParent;
- }
- return { top: _y, left: _x };
- }
- /* Initial script to detect embedded clips, remove duplicates, and re-embed with desired attributes */
- var nodeList = document.getElementsByTagName("embed"); // Get all embedded objects
- // Convert NodeList "nodeList" to an Array of Nodes "objects"
- var objects = new Array();
- for (i=0;i<nodeList.length;i++){
- objects.push(nodeList[i]);
- }
- // Ensure embedded objects are YouTube, remove duplicates, and re-embed the objects with desired changes.
- for (i=0; i<objects.length; i++) {
- var stringA;
- var stringB;
- // Get source of the embedded object
- var attributes = objects[i].attributes;
- var source = attributes.getNamedItem("src");
- // Checks that embedded file is youtube. **Don't know if this works, haven't come across any non-youtube embeds.**
- if (source == -1|| source.nodeValue.indexOf("http://www.youtube.com/v/") == -1) {
- alert("Non-YouTube embedded object found");
- // ... and will need to write something else here, if this situation ever arises.
- }
- // Ensure each equivalent (same source) object is only present once in the array.
- stringA = source.nodeValue;
- for (j=0; j<objects.length; j++) {
- var compAttributes = objects[j].attributes;
- var compSource = compAttributes.getNamedItem("src");
- stringB = compSource.nodeValue;
- if (stringA == stringB && !(i==j) ){ // If same source but not the same index
- objects.splice(j,1);
- j--; // array length changed by splice, so need to re-check this index
- }
- }
- // Re-embed the YT clips, enabling the JS API, giving each a unique ID
- objects[i].setAttribute("src", objects[i].getAttribute("src").concat("&enablejsapi=1&playerapiid=" + i));
- // Put an "id" atribute in <embed>
- objects[i].setAttribute("id", i);
- }
- // "Pass" number of unique clips to the unsafeWindow namespace
- unsafeWindow.totalClips = objects.length - 1;
Advertisement
Add Comment
Please, Sign In to add comment