JavaScript Ninja - So You Want To Get A YouTube Video Streams Resource Without Ajax'ing Around? DAAAA!!!

Posted at

Hello Readers! some time ago I've made a nice little Git repository named YouTube Downloader having most of raw resources I've gathered around which may be useful if you are intereseted in in-depth-understanding of the the YouTube JavaScript Player API Reference

(which is a nice mental leap towards making an hybrid (Android) APP or using the more challenging YouTube Android Player API or just making a bookmarklet or even yet another youtube downloader website/extension [please don't!] ;) )

anyway..
..here is what you are really in for:

sources = (function(sources, tmp){
  return sources.split(',').map(function(source){
    tmp = {};
    source.split('&').forEach(function(part){
      part = part.split('=');
      tmp[ part[0] ] = decodeURIComponent( part[1] );
    });
    return tmp;
  });
}(
  ytplayer.config.args.url_encoded_fmt_stream_map
, []
));


and now for a pro tip, a zero-effort JSON beautifier formatting trick:

console.log(JSON.stringify(
sources
, null, 2));





hey! it's probably even works with most of embedded youtube plugin, since it uses almost no dependencies by the very raw YouTube-Player "API/SDK" (which YouTube itself uses...)

..give it a try on this lovely video:



:)




edit: Tip: ǡre yoǘ ȃ coʍʍʌ-ϝiɍst Nåƶɨ? (*cough* *cough* me?)
this will ease your O.C.D.:
sources = JSON.stringify(sources, null, 2).replace(/,\
 /g, "\
 ,")

icompile.eladkarako.com_javascript_comma_first_2016-01-31_011351

or BETTER YET:
sources = JSON.stringify(sources, null, 2).replace(/,\
 /g, "\
 ,").replace(/ *(,(\\ +))/g,"$2,")

who does not love a little bit of JavaScript RegEx backtrace replacing :)

icompile.eladkarako.com_javascript_comma_first_better_format_2016-01-31_011351

Oh yeah! comma-first bliss ;)