rstat1 / AutoMuteAds on Google Play Music Radio

// ==UserScript==
// @name         AutoMuteAds on Google Play Music Radio
// @namespace    http://twitter.com/rstat1
// @version      0.1
// @description  Auto mute/unmute ads that play through the ad-supported version of the radio feature on Google Play Music.
// @author       rstat1
// @match        https://play.google.com/music/listen?u=0
// @match        https://play.google.com/music/listen?u=0#/now
// @match        https://play.google.com/music/listen?u=0#*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @grant        GM_log
// @run-at document-idle
// ==/UserScript==

var currentVolume = -1;

(function() {
    'use strict';
    function muteOrUnmute(title) {
        var volumeSlider = document.getElementById("material-vslider");
        if (title === "We'll be right back") {
            GM_log("Oh look an ad. Time to mute.");
            if(volumeSlider.value !== 0) { currentVolume = volumeSlider.value; }
            volumeSlider.value = 0;
        }
        else {
            GM_log("That looks like something you'd actually want to hear! Unmuting...");
            if (currentVolume == -1) { currentVolume = volumeSlider.value; }
            volumeSlider.value = currentVolume;
        }
        GM_log(volumeSlider.value);
    }
    function setObservers() {
        setTimeout(function() {
            GM_log("Starting...");
            var volumeSlider = document.getElementById("material-vslider");
            var playerSongInfo = document.getElementById("playerSongInfo");
            var currentSongTitle;

            currentVolume = volumeSlider.value;

            var VolumeChangeObserver = new MutationObserver(function(mutations) {
                if (volumeSlider.value !== 0) { currentVolume = volumeSlider.value; }
            });
            var SongTitleChangeObserver = new MutationObserver(function(mutations) {
                currentSongTitle = document.getElementById("currently-playing-title");
                if (currentSongTitle !== undefined) { muteOrUnmute(currentSongTitle.innerHTML); }
            });
            SongTitleChangeObserver.observe(playerSongInfo, { attributes: false, childList: true, characterData: false });
            VolumeChangeObserver.observe(volumeSlider, { attributes: true, childList: false, characterData: false });
        }, 4500);
    }
    $(window).load(setObservers());
})();