TheDragShot / Tumblr mp3 Download

// ==UserScript==
// @name            Tumblr mp3 Download
// @namespace       ds.js.tumblr
// @author          DragShot
// @oujs:author     TheDragShot
// @released        2015-06-23
// @updated         2015-06-24
// @lastchanges     Now it adds progressively buttons in non-iframe posts found in the dashboard
// @description     Adds a download button in audio frames
// @copyright       2015, DragShot
// @license         GNU GPL version 3
// @icon            https://secure.assets.tumblr.com/images/apple-touch-icon-152x152.png
// @include         *www.tumblr.com/dashboard*
// @include         *www.tumblr.com/tagged/*
// @include         *.tumblr.com/post/*/audio_player_iframe/*?audio_file=*
// @version         1.1
// @grant           none
// downloadURL      https://openuserjs.org/install/TheDragShot/Tumblr_mp3_Download.user.js
// ==/UserScript==
//Utils
function addCSS(css){
  var head = document.head || document.getElementsByTagName('head')[0],
      style = document.createElement('style');
  style.type = 'text/css';
  if (style.styleSheet){
    style.styleSheet.cssText = css;
  } else {
    style.appendChild(document.createTextNode(css));
  }
  head.appendChild(style);
}
function hasClass(element, cls) {
  return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
//Assitance functions
function addDownloadButton(target){
  var audioElem = target.getElementsByTagName("audio")[0];
  var link = document.createElement('a');
  link.target = '_blank';
  link.className = 'dwn-btn dwn-btn-disabled';
  link.innerHTML = '.mp3';
  target.getElementsByClassName('progress_container')[0].appendChild(link);
  audioElem.addEventListener('loadedmetadata', function(){
    link.href = audioElem.src;
    link.className = 'dwn-btn';
  });
}
function addDownloadLink(target){
  if(!hasClass(target,'mp3-plugin-checked')){
    target.className += ' mp3-plugin-checked';
    var node=target.getElementsByClassName('audio_player_container')[0];
    var url = node.getAttribute("data-stream-url")+'?play_key='+node.getAttribute("data-post-key");
    var div = document.createElement('div');
    div.className = 'post_body';
    div.innerHTML = "<a class='dwn-btn' target='_blank' href='"+url+"' style='position: relative !important; margin-top: 20px; margin-bottom: -20px;'>.mp3</a>";
    target.appendChild(div);
  }
}
//Trigger
document.addEventListener("DOMContentLoaded", function(event) {
  //Styles
  addCSS("\
  .dwn-btn {\n\
      z-index: 25;\n\
      position: absolute;\n\
      top: 50%;\n\
      right: 10px;\n\
      -webkit-transform: translateY(-50%);\n\
      -ms-transform: translateY(-50%);\n\
      transform: translateY(-50%);\n\
      display: inline-block;\n\
      box-sizing: border-box;\n\
      font-weight: 400;\n\
      border: 1px solid transparent;\n\
      border-color: #E38D13;\n\
      background: #F0AD4E !important;\n\
      background-image: linear-gradient(to bottom, #F0AD4E 0px, #EB9316 100%) !important;\n\
      background-repeat: repeat-x;\n\
      color: black;\n\
      text-decoration: none;\n\
      padding: 1px 5px;\n\
      font-size: 14px !important;\n\
      line-height: 1.5;\n\
      border-radius: 3px;\n\
      box-shadow: 0px 1px 0px rgba(255, 255, 255, 0.15) inset, 0px 1px 1px rgba(0, 0, 0, 0.075);\n\
  }\n\
  .dwn-btn-disabled{\n\
      background: #CCC !important;\n\
      border-color: #666;\n\
  }");
  //Main code block
  var url=document.URL;
  if(~url.indexOf('/audio_player_iframe/')){
    addDownloadButton(document);
  }else{
    setInterval(function(){
      var conts = document.getElementsByClassName('audio_player_container');
      for (var i=0, item; item = conts[i]; i++) {
        var playbtn = item.getElementsByClassName('play_button')[0];
        var post_media = item.parentNode;
        addDownloadLink(post_media);
      }
    },1000);
  }
});