HTML5 thingie for Spotify Web Player

asdf

As of 2015-11-20. See the latest version.

// ==UserScript==
// @name        HTML5 thingie for Spotify Web Player
// @description asdf
// @namespace   https://gf.zukizuki.org/users/4813-swyter
// @match       https://play.spotify.com/*
// @version     2015.11.20
// @noframes
// @icon        https://i.imgur.com/CecLLKu.png
// @grant       none
// ==/UserScript==

function when_external_loaded()
{
// ---
 
/* wait until the page is ready for the code snippet to run */
document.addEventListener('DOMContentLoaded', function()
{
  console.log("!!! DOMContentLoaded");
  
  /* SWFobject.js faker, sneaky! */
  Object.defineProperty(window, 'swfobject',
  {
    configurable: false,
    writable: false,
    value:
    {
      getFlashPlayerVersion: function(){ console.log("-<-< getFlashPlayerVersion =>", arguments); return { major: 11, minor: 2, release: 202 }; },
      hasFlashPlayerVersion: function(){ console.log("-<-< hasFlashPlayerVersion =>", arguments); return true; },
      embedSWF:              function(){ console.log("-<-< embedSWF =>", arguments); }
    }
  });
  
  WebSocket.prototype.sond = WebSocket.prototype.send;
  WebSocket.prototype.send = function(msg)
  {
    if (this.onmessage && this.sucks !== true)
    {
      callback = this.onmessage;
      
      console.log("orig prev callback:", callback);

      this.onmessage = function(message)
      {
        var json_msg = JSON.parse(message.data);
        
        if (json_msg.id === window.last_track_msg)
          console.info("<- ws recv: ", window.last_track_msg, message.data);
        
        if (json_msg && json_msg.result && json_msg.result.uri)
          open(json_msg.result.uri);
        
        callback(message);
      }
      
      this.sucks = true;
    }

    var json_msg = JSON.parse(msg);

    // get http link
    if (json_msg && json_msg.name == 'sp/track_uri')
    {
      arguments[0] = msg.replace(',"rtmp"', '');
      window.last_track_msg = json_msg.id;
      console.info("-> ws send: ",  json_msg.id, msg);
    }
    //dec_msg = json_msg.name === 'sp/hm_b64' ? atob(json_msg.args[0]) : null;
    
    //console.info("-> ws send: ", msg); //json_msg, dec_msg);
    
    //if (json_msg.name !== 'sp/log')
     return WebSocket.prototype.sond.apply(this, arguments);
  }
  
});
  
// ---
}

/* inject this cleaning function right in the page to avoid silly sandbox-related greasemonkey limitations */
window.document.head.appendChild(
  inject_fn = document.createElement("script")
);

inject_fn.innerHTML = '(' + when_external_loaded.toString() + ')()';