Greasy Fork is available in English.

Oh My Rockness Spotify Link

Add a spotify link to a band in the shows calander to make it easier to listen to them.

// ==UserScript==
// @name         Oh My Rockness Spotify Link
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Add a spotify link to a band in the shows calander to make it easier to listen to them.
// @author       Kent Widman
// @match        https://*.ohmyrockness.com
// @match        https://*.ohmyrockness.com/shows*
// @match        https://open.spotify.com/search/*?autoplay
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    debugger;

    const svg = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjxzdmcgaGVpZ2h0PSI2MHB4IiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjAgMCA2MCA2MCIgd2lkdGg9IjYwcHgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6c2tldGNoPSJodHRwOi8vd3d3LmJvaGVtaWFuY29kaW5nLmNvbS9za2V0Y2gvbnMiIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj48dGl0bGUvPjxkZWZzLz48ZyBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGlkPSJnbG9zcyIgc3Ryb2tlPSJub25lIiBzdHJva2Utd2lkdGg9IjEiPjxnIGlkPSJTcG90aWZ5Ij48cGF0aCBkPSJNMzAsNjAgQzQ2LjU2ODU0MzMsNjAgNjAsNDYuNTY4NTQzMyA2MCwzMCBDNjAsMTMuNDMxNDU2NyA0Ni41Njg1NDMzLDAgMzAsMCBDMTMuNDMxNDU2NywwIDAsMTMuNDMxNDU2NyAwLDMwIEMwLDQ2LjU2ODU0MzMgMTMuNDMxNDU2Nyw2MCAzMCw2MCBaIiBmaWxsPSIjNTdCQjYzIi8+PHBhdGggZD0iTTUxLjIxMzIwMzcsOC43ODY3OTYyNiBDNTYuNjQyMTM1OCwxNC4yMTU3MjgzIDYwLDIxLjcxNTcyODMgNjAsMz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MjQuNzY2MzE5NCA0Ny41NzQyMjI4LDI1Ljk2MDE4ODMgNDcuMDE4MTgwMywyNi44NjM1NDIgWiIgZmlsbD0iI0ZGRkZGRiIvPjwvZz48L2c+PC9zdmc+';
    var observerPage = null;
    var firstTime = true;

    // modifyPushState
    function modifyPushState() {
        var pushState = history.pushState;
        history.pushState = function () {
            pushState.apply(history, arguments);
            // todo fix this
            setTimeout(function(){
                //connectCbserver();
                addLinkToBand(document.body)
            }, 1000);
            // fireEvents('pushState', arguments);  // Some event-handling function
        };
    }

    // helper
    function insertAfter(el, referenceNode) {
        referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
    }

    function addLinkToBand(target) {
        let bands = target.querySelectorAll('.bands a');
        if (bands !== null) {
            bands.forEach(function(band) {
                console.log(band);
                let bandName = band.textContent;
                let bandNameEncoded = encodeURI(bandName);

                // Create element
                let a = document.createElement('a');
                //let linkText = document.createTextNode("spotify");
                let img = document.createElement('img');
                //let linkText = document.createTextNode("spotify");
                img.src = svg;
                img.width = 18;
                img.height = 18;
                a.target = 'spotify_player';
                a.appendChild(img);
                a.href = "https://open.spotify.com/search/" + bandNameEncoded + '?autoplay';

                insertAfter(a, band);
            });
        }
    }

    // Callback function to execute when mutations are observed
    const callback = function(mutationsList, observer) {
        for(let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                if (mutation.addedNodes.length > 0) {
                    for(let node of mutation.addedNodes) {
                        if (node.nodeType === 1){
                            addLinkToBand(node);
                        }
                    }
                }
                console.log('A child node has been added or removed.');
            }
            else if (mutation.type === 'attributes') {
                console.log('The ' + mutation.attributeName + ' attribute was modified.');
            }
        }
    };

    function connectCbserver() {
        if (observerPage !== null) {
            // stop observing
            observerPage.disconnect();
        }

        // Select the node that will be observed for mutations
        // const targetNode = document.getElementById('mainContainer');
        const targetNode = document.body ;

        // Options for the observer (which mutations to observe)
        const config = { attributes: true, childList: true, subtree: true };

        // Create an observer instance linked to the callback function
        observerPage = new MutationObserver(callback);

        // Start observing the target node for configured mutations
        observerPage.observe(targetNode, config);
    }

    function ohMyRockness() {
        modifyPushState();
        connectCbserver();
    }

    function autoPlay() {
        //find artist
        let showsListing = document.querySelector('.cover-art-playback');
        let artistLink = document.querySelector('.cover-art');
        if(!showsListing) {
            return;
        }

        setTimeout(function(){
            if (firstTime) {
                firstTime = false;
                showsListing.click();
                //artistLink.click();
            }
        }, 1000);
        observerPage.disconnect();
    }

    function spotify(){
        let body = document.querySelector('body');
        if(!body) {
            return;
        }

        observerPage = new MutationObserver(autoPlay);
        observerPage.observe(body, {
            'childList': true,
            'subtree': true
        });
    }

    if (window.location.hostname.match(/^([a-zA-Z0-9]+\.)?ohmyrockness.com$/i)) {
        ohMyRockness();
    } else if (window.location.hostname.match(/^([a-zA-Z0-9]+\.)?spotify.com$/i)) {
        spotify();
    }
})();