Guest User

Untitled

a guest
Dec 9th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           GPForums Youtube Autoplay
  3. // @namespace      awt
  4. // @description    Autoplay embedded YouTube clips one at a time
  5. // @include        http://www.gpforums.co.nz/*
  6. // ==/UserScript==
  7.  
  8.  
  9. /* Define all unsafeWindow functions before the rest of script (thanks ED) */
  10.  
  11. unsafeWindow.onYouTubePlayerReady = function(playerid){
  12.    
  13.     if (unsafeWindow.totalNo == undefined){
  14.         unsafeWindow.totalNo = 0;
  15.     } else {
  16.         unsafeWindow.totalNo++;
  17.     }
  18.    
  19.     player = document.getElementById(playerid).wrappedJSObject;
  20.     // Add Event Listener for State Change and for situation where a video errors.
  21.     player.addEventListener("onStateChange", "whenStateChanges");
  22.     player.addEventListener("onError", "whenError");
  23.    
  24.     if (unsafeWindow.totalNo == unsafeWindow.totalClips) {
  25.         alert(unsafeWindow.totalClips + " YouTube clips lined up. Starting first video now.");
  26.         unsafeWindow.currentPlayer = 0;
  27.         player = document.getElementById("0").wrappedJSObject.playVideo();
  28.     }
  29. }
  30.  
  31.  
  32. unsafeWindow.whenStateChanges = function(state) {
  33.     if (state == 0){ // Called when clip ends (state = 0)
  34.         unsafeWindow.nextClip();
  35.     }
  36. }
  37.  
  38. unsafeWindow.whenError = function() {
  39.     // Called if a clip doesn't play due to an error
  40.     unsafeWindow.nextClip();
  41. }
  42.  
  43. // Plays the next unique clip on the page
  44. unsafeWindow.nextClip = function(){
  45.     unsafeWindow.currentPlayer++;
  46.     document.getElementById(unsafeWindow.currentPlayer).wrappedJSObject.playVideo();
  47.     unsafeWindow.scrollTo(0, (unsafeWindow.getOffset( document.getElementById(unsafeWindow.currentPlayer) ).top - 200));
  48. }
  49.  
  50. // To get the XY coordinates of an element
  51. // ** Thanks to meouw and cwallenpoole at stackoverflow **
  52. unsafeWindow.getOffset = function(el) {
  53.     var _x = 0;
  54.     var _y = 0;
  55.     while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
  56.         _x += el.offsetLeft - el.scrollLeft;
  57.         _y += el.offsetTop - el.scrollTop;
  58.         el = el.offsetParent;
  59.     }
  60.     return { top: _y, left: _x };
  61. }
  62.  
  63.  
  64.  
  65.  
  66. /* Initial script to detect embedded clips, remove duplicates, and re-embed with desired attributes */
  67.  
  68. var nodeList = document.getElementsByTagName("embed"); // Get all embedded objects
  69.  
  70. // Convert NodeList "nodeList" to an Array of Nodes "objects"
  71. var objects = new Array();
  72. for (i=0;i<nodeList.length;i++){
  73.    objects.push(nodeList[i]);
  74. }
  75.  
  76. // Ensure embedded objects are YouTube, remove duplicates, and re-embed the objects with desired changes.
  77. for (i=0; i<objects.length; i++) {
  78.    var stringA;
  79.    var stringB;
  80.    
  81.    // Get source of the embedded object
  82.    var attributes = objects[i].attributes;
  83.    var source = attributes.getNamedItem("src");
  84.    
  85.    // Checks that embedded file is youtube.  **Don't know if this works, haven't come across any non-youtube embeds.**
  86.    if (source == -1|| source.nodeValue.indexOf("http://www.youtube.com/v/") == -1) {
  87.       alert("Non-YouTube embedded object found");
  88.       // ... and will need to write something else here, if this situation ever arises.
  89.    }
  90.    
  91.     // Ensure each equivalent (same source) object is only present once in the array.
  92.     stringA = source.nodeValue;
  93.     for (j=0; j<objects.length; j++) {
  94.         var compAttributes = objects[j].attributes;
  95.         var compSource = compAttributes.getNamedItem("src");
  96.        
  97.         stringB = compSource.nodeValue;
  98.        
  99.         if (stringA == stringB &&  !(i==j) ){ // If same source but not the same index
  100.            objects.splice(j,1);
  101.            j--; // array length changed by splice, so need to re-check this index
  102.         }
  103.     }
  104.    
  105.     // Re-embed the YT clips, enabling the JS API, giving each a unique ID
  106.     objects[i].setAttribute("src", objects[i].getAttribute("src").concat("&enablejsapi=1&playerapiid=" + i));
  107.     // Put an "id" atribute in <embed>
  108.     objects[i].setAttribute("id", i);
  109. }
  110.  
  111. // "Pass" number of unique clips to the unsafeWindow namespace
  112. unsafeWindow.totalClips = objects.length - 1;
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment