Robot-Chicken Bipolar Bear (and a JavaScript Download-Video Trick For adultswim Host-Website)

bipolar-bear

Scientists:

  • The escaped animal is a polar bear with the ability to survive in the Arctic and Antarctic.
  • It exhibits massive mood swings like that of a manic-depressive,
  • and has equal sexual desires for both males and females.

Army Guy:

  • Dear god, are you suggesting what I think you’re suggesting?

Scientists:

  • That’s right.. –
  • A bi-polar, bipolar, – bi polar bear.

Army Guy:

  • That is very stupid..

Robot Chicken (by adultswim.com) is a sketch-like comedy TV-show made with action-figures using stop-motion and a funny voice-over, usually filled with pun-games, which are the type of comedy that make me laugh the most :))

Original website is http://www.adultswim.com/videos/robot-chicken/bipolar-bear

and running the following code, will add a download button,
it just uses the video-url from the page’s internal-source code, and isn’t decrypting or breaking stuff or anything.. its publicly accessible (just don’t use it in any evil doing sort..)

(function(window,document, info){
  "use strict";

    window.setTimeout(function(){     //execute a non-blocker

      info.link.href = info.vid_href;
      info.link.download = info.vid_downloadfilename;
      info.link.title = info.link.title
                          .replace("##TITLE_FULL##", info.vid_title_full)
                          .replace("##VID_TYPE##", info.vid_type)
                          .replace("##VID_DURATION##", (~~info.vid_duration))
                        ;

      info.placement.insertBefore(info.link, info.placement.firstElementChild);

    }, 200);

  }(window,document, {"_":""
    ,'placement':             document.querySelector("#video-player-object-wrapper")
    ,'placement_first':       document.querySelector("#video-player-object-wrapper").firstElementChild
    ,'link':                  (function(){
                                 var a       = document.createElement('a');
                                 a.target    = "_self";
                                 a.rel       = "nofollow";
                                 a.className = "download_button";
                                 a.title     = "Click To Download \"##TITLE_FULL##\" (##VID_TYPE##: ##VID_DURATION## Seconds)";
                                 a.appendChild(document.createTextNode("Download"));
                                 return a;
                              }())
    ,'style':                  (function(){
                                 var s  = document.createElement('style');
                                 s.type = "text/css";
                                 s.appendChild(document.createTextNode(
                                 '.download_button{background-color:rgba(255,183,57,.8);border:3px solid rgba(255,183,57,.3);border-radius:10px;box-shadow:2px 2px 2px rgba(255,255,255,.3);cursor:pointer;display:block;height:30px;height:100% !important;margin:0px 0px 10px 0px;max-width:100px;outline:1px rgba(255,255,255,.3);text-align:center;transition:all .3s ease-in-out;-webkit-font-smoothing:subpixel-antialiased;}'
                               + '.download_button:hover{background-color:rgba(255,237,142,.8);border:3px solid rgba(255,237,142,.3);}'
                                 ));
                                 document.getElementsByTagName("body")[0].appendChild(s); //already add this to the document (parsed into stylesheet)
                                 return s; //unneeded..
                              }())
    ,'vid_href':              bootstrappedData.slugged_video.segments[0].assets[0].url
    ,'vid_downloadfilename':  encodeURI((bootstrappedData.slugged_video.collection_title + " - " + bootstrappedData.slugged_video.title + " - " + bootstrappedData.slugged_video.description).replace("\n","").replace("\r","").replace(".","")) + ".flv"
    ,'vid_title_full':        (bootstrappedData.slugged_video.collection_title + " - " + bootstrappedData.slugged_video.title + " - " + bootstrappedData.slugged_video.description).replace("\n","").replace("\r","").replace(".","")
    ,'vid_duration':          bootstrappedData.slugged_video.segments[0].assets[0].duration
    ,'vid_type':              bootstrappedData.slugged_video.segments[0].assets[0].mime_type
 })
);

q: what is so interesting about this code ?
a: well..

  • while the main logic is in the “big” anonymous method,
  • most of the minor work preparing the elements, or data, is done before the main-method executes,
  • kept to the minimal, the memory used is quickly disposed once its logically unneeded.
  • code uses a unique technique I’ve made and refined over the time, of not using var statements overall in the main method (pre-preparing..).
  • another concept is avoiding moving a long-list of arguments to the method, and using a data-structure such as json-like object to pass a static-data, variables, etc.. where they order is not important,
  • this way maintenance is easy adding, or removing “”arguments””- does not required “Function Signature (a.k.a Header) Change – Function looks the same.
    :)
  • 2014-11-01_183051


Leave a Reply