JAVASCRIPT   40

YouTube api js

Guest on 18th August 2022 12:46:31 PM

  1. /* Load the YouTube API */
  2.  
  3. var tag = document.createElement('script');
  4. tag.src = "https://www.youtube.com/player_api";
  5. var firstScriptTag = document.getElementsByTagName('script')[0];
  6. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  7.  
  8.  
  9. // Replace the 'ytplayer_*' element(s) with an <iframe> and
  10. // YouTube player after the API code downloads.
  11. var player;
  12. function onYouTubePlayerAPIReady() {
  13.     $('[id^=ytplayer_]').each(function() {
  14.    
  15.         var varray= this.id.split('_');
  16.         // ignore varray[0] (ytplayer) we don't need it anymore
  17.         var vwidth = varray[1];
  18.         var vheight = varray[2];
  19.         // since "_" is a valid yt video id character and javascript split's limit doesn't combine the leftovers we need to combine manually...
  20.         var vidid = varray[3];
  21.         for (var i=4; i<varray.length; i++) {
  22.             vidid += "_"+varray[i];
  23.         }
  24.         player = new YT.Player(this.id, {
  25.         width: vwidth,
  26.         height: vheight,
  27.         videoId: vidid,
  28.         playerVars: {
  29.             autohide: 1,
  30.             showinfo: 0,
  31.             rel: 0,
  32.             wmode: "transparent"
  33.         },
  34.         events: {
  35.                     //'onReady': onPlayerReady
  36.                   }
  37.         });
  38.        
  39.     });
  40.   }      
  41.  
  42. /*
  43. function onPlayerReady(event) {
  44.      event.target.setPlaybackQuality('hd720');
  45. }
  46.  
  47. function onPlayerStateChange(event) {
  48.     if (event.data == YT.PlayerState.BUFFERING) {
  49.         event.target.setPlaybackQuality('hd720');
  50.     }
  51. } */

Raw Paste


Login or Register to edit or fork this paste. It's free.