Bandcamp Volume Script

Adds a volume control bar to Bandcamp

// ==UserScript==
// @name         Bandcamp Volume Script
// @version      1.5
// @description  Adds a volume control bar to Bandcamp
// @author       @kirby
// @match        *://*.bandcamp.com/*
// @exclude      *://bandcamp.com/
// @grant        GM_addStyle
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.js
// @namespace https://greasyfork.org/users/1142347
// ==/UserScript==


//Awesome Tags!
$("audio").attr("id", "audioSource");
$("<div class='volumeControl'></div>").insertAfter(".inline_player");
$(".volumeControl").append("<div class='speaker'></div>");
$(".volumeControl").append("<div class='volume'></div>");
$(".volume").append("<span class='volumeInner'></span>");


//CSS Time!
var percentage = 75;
var speakerurl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEAAACxABrSO9dQAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAAklJREFUWEfN1j1rFFEYxfHEBF8KCfjWSb6BitHaV9zGEMFoZ2FrZ6GFCKKihSCkUQsF0UIEDSRdGiGNgRQifgERQcFCTcRERGX9n8s84e7dZ+7OumP0wA8yZ252n52dmZ2+ZrP5T7llRYdxH3exp+i65pYVHMMvWL5jN7y1WW7ZQQN6wzS34a3PcsuMQ1iGl8fw/sdo8La+rcjQC5S9uZIb4CSUG2jZ17KRcQLeYY9TNsAA3sJyASv744VlziI+4cqSOwL7sARFr6WvMuxLF8aG8BBV4w2wJvpbV47lDTYgDNCP03iEp4UZfEE3SQfYhhcYjro7sJxHGOBK2Ow96QCTUF5hHdRtwiKU9xhU+Sls9p54AJ14z2C5CNt3U0WRhoq6kh6BLfgIRR9yPdSPqCgy8TcHkEuwjEKdzrnPKsicirriDbADluuwfk4F+aCNuuINoJPP8gDWT6kgP7RRV/7LAXbCcg3Wr9pXcBmWo1Cnk3BBBXmuoq6kA+hOaGe7Lke7Ge1VUSRchrao18QD6EY0C0v8CzihosgR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+Ef9FURpvQEbwB1bupAF5AutyfIA1vpRZrECPWHhjqqFsdavDLFb0GPoBqk9+q8nFLN6dpO0STDmoN2vSm20AAAAASUVORK5CYII=";
var color = $("#pgBd").css("background-color");
var css = ".volumeControl { margin-bottom: 10px; }"                                            +
          ".speaker {"                                                                         +
            "position: relative;"                                                              +
            "width: 50px;"                                                                     +
            "height: 50px;"                                                                    +
            "background: url('"+speakerurl+"') rgba(2,2,2,.1) 50% 50% no-repeat;"              +
            "border-radius: 3px;"                                                              +
            "cursor: pointer;"                                                                 +
          "}"                                                                                  +
          ".volumeInner {"                                                                     +
            "position: absolute;"                                                              +
            "bottom: 0;"                                                                       +
            "width: "+percentage+"%;"                                                          +
            "height: 20px;"                                                                    +
            "background-color: #fff;"                                                          +
          "}"                                                                                  +
          ".volume {"                                                                          +
            "position: relative;"                                                              +
            "width: 80%;"                                                                      +
            "height: 20px;"                                                                    +
            "margin-top: -35px;"                                                               +
            "float: right;"                                                                    +
            "cursor: pointer;"                                                                 +
            "background-color: rgba(2,2,2,.1);"                                                +
            "border: 1px solid rgba(190,190,190,.5);"                                          ;

GM_addStyle(css);


//Sexy Script!
audioSource.volume = percentage/100;

function changeVolume(e) {
	var clickPos = (e.pageX) - $(".volume").offset().left;
	var maxWidth = $(".volume").width();
	percentage = Math.floor(clickPos / maxWidth * 100);
    if(percentage > 100) {
        percentage = 100;
        $(".volumeInner").css("width", "100%");
    } else if(percentage < 0) {
        percentage = 0;
        $(".volumeInner").css("width", "0%");
    } else {
        $(".volumeInner").css("width", percentage + "%");
    }
    
	audioSource.volume = percentage/100;
}


$(".volume").mousedown(function(e){
    changeVolume(e);
    $("body").css({
        "-webkit-user-select": "none",
        "-moz-user-select": "none"
    });
    
	$("body").mousemove(function(e){ changeVolume(e); });
});

$(document).mouseup(function(){
    $("body").off("mousemove");
    
    $("body").css({
        "-webkit-user-select": "all",
        "-moz-user-select": "all"
    });
});

var mute = false;
$(".speaker").click(function(){
    if(mute) {
        mute = false;
        $(".speaker").css("background-image", "url('"+speakerurl+"')");
        $(".volumeInner").css("width", percentage + "%");
        audioSource.volume = percentage/100;
    } else {
        mute = true;
        $(".speaker").css("background-image", "url('"+muteurl+"')");
        audioSource.volume = 0;
        $(".volumeInner").css("width", "0%");
    }
});