Advertisement
Guest User

youtube-player-api-helper.js

a guest
Feb 21st, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @description Easier way to implement the YouTube JavaScript API
  2. // @author      Rob W
  3. // @global      getFrameID(id) Quick way to find the iframe object which corresponds to the given ID.
  4. // @global      YT_ready(Function:function [, Boolean:qeue_at_start])
  5. // @global      onYouTubePlayerAPIReady()  - Used to trigger the qeued functions
  6. // @website     http://stackoverflow.com/a/7988536/938089?listening-for-youtube-event-in-javascript-or-jquery
  7.  
  8. function getFrameID(id){
  9.     var elem = document.getElementById(id);
  10.     if (elem) {
  11.         if(/^iframe$/i.test(elem.tagName)) return id; //Frame, OK
  12.         // else: Look for frame
  13.         var elems = elem.getElementsByTagName("iframe");
  14.         if (!elems.length) return null; //No iframe found, FAILURE
  15.         for (var i=0; i<elems.length; i++) {
  16.            if (/^https?:\/\/(?:www\.)?youtube(?:-nocookie)?\.com(\/|$)/i.test(elems[i].src)) break;
  17.         }
  18.         elem = elems[i]; //The only, or the best iFrame
  19.         if (elem.id) return elem.id; //Existing ID, return it
  20.         // else: Create a new ID
  21.         do { //Keep postfixing `-frame` until the ID is unique
  22.             id += "-frame";
  23.         } while (document.getElementById(id));
  24.         elem.id = id;
  25.         return id;
  26.     }
  27.     // If no element, return null.
  28.     return null;
  29. }
  30.  
  31. // Define YT_ready function.
  32. var YT_ready = (function(){
  33.     var onReady_funcs = [], api_isReady = false;
  34.     /* @param func function     Function to execute on ready
  35.      * @param func Boolean      If true, all qeued functions are executed
  36.      * @param b_before Boolean  If true, the func will added to the first
  37.                                  position in the queue*/
  38.     return function(func, b_before){
  39.         if (func === true) {
  40.             api_isReady = true;
  41.             for (var i=0; i<onReady_funcs.length; i++){
  42.                 // Removes the first func from the array, and execute func
  43.                 onReady_funcs.shift()();
  44.             }
  45.         }
  46.         else if(typeof func == "function") {
  47.             if (api_isReady) func();
  48.             else onReady_funcs[b_before?"unshift":"push"](func);
  49.         }
  50.     }
  51. })();
  52. // This function will be called when the API is fully loaded
  53. function onYouTubePlayerAPIReady() {YT_ready(true);}
  54.  
  55. // Load YouTube Frame API
  56. (function(){ //Closure, to not leak to the scope
  57.   var s = document.createElement("script");
  58.   s.src = "http://www.youtube.com/player_api"; /* Load Player API*/
  59.   var before = document.getElementsByTagName("script")[0];
  60.   before.parentNode.insertBefore(s, before);
  61. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement