ressonance
7/4/2016 - 11:13 AM

TamperMonkey - Create Youtube Search for Radionomy tracks

TamperMonkey - Create Youtube Search for Radionomy tracks

// ==UserScript==
// @name         Youtube Link for tracks
// @namespace    http://tampermonkey.net/
// @version      0.2
// @updateURL  https://gist.github.com/josep11/8766d64b775c5d35100c9d1902ab6cbc/gistfile.user.js
// @downloadURL  https://gist.github.com/josep11/8766d64b775c5d35100c9d1902ab6cbc/gistfile.user.js
// @description  Creates a link to search in youtube for the selected song
// @author       Josep Alsina <josep_a11@hotmail.com>
// @match        http://www.radionomy.com/*
// @exclude      http://www.radionomy.com/*/ad/*
// @require     http://code.jquery.com/jquery-2.1.0.min.js
// @run-at  document-end
// @grant GM_addStyle
// ==/UserScript==

console.log("running on", location.href);
GM_addStyle("\
/**{font-size: 99px !important;}*/\
#link-current-song{color:white !important; text-decoration:none !important;}\
#radioProfileTracklist .tracklist:hover{background: #dcdcdc !important};\
.rad-tracks{max-width: 100% !important; display: block !important;}\
media only screen and (max-width: 950px).rad-tracks{display:block !important;}\
            ");

String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
var slug = function(str) {
    str = str.replace(/^\s+|\s+$/g, ''); // trim
    str = str.toLowerCase();

    // remove accents, swap ñ for n, etc
    var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;";
    var to   = "aaaaaeeeeeiiiiooooouuuunc------";
    for (var i=0, l=from.length ; i<l ; i++) {
        str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
    }

    str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
        .replace(/\s+/g, '%20') // collapse whitespace and replace by -
        .replace(/-+/g, '-'); // collapse dashes

    return str;
};

var baseUrl = "https://www.youtube.com/results?search_query=";
var updateLinks = function(){
    $('.tracklistInfo').each(function(i){
        if ($(this).parent().attr('data-href')) //if link is alredy defined skip
            return;

        var str = "";
        $(this).find('p').each(function(){
            if (!$(this).hasClass('playing') && $(this).text() != "No Artist"){
                str += " ";
                str += $(this).text();
            }
        });
        $(this).parent()
            .attr('data-href', baseUrl + slug(str))
            .css('cursor','pointer')
            .on('click', function(){
            window.open($(this).attr('data-href'), '_blank');
        });
    });
    // console.log("updateLinks");
},
    createLink = function(trackName){
        return baseUrl + slug(trackName);
    };

var interval = setInterval(updateLinks, 1000);


(function () {
    // We intercept the function callback redefine the GetLastTrackPlayed
    try{
        var oldGetLastTrackPlayed = GetLastTrackPlayed;
    }catch(e){
        console.log(location.href);
        //clearInterval(interval);
        return;
    }

    GetLastTrackPlayed = function () {
        if (interval)
            clearInterval(interval);
        // oldGetLastTrackPlayed();
        if (lastTracksPlayedTimeout) clearTimeout(lastTracksPlayedTimeout);
        if (controller != 'radio') return;

        $.post('/' + language + '/OnAir/GetLastTracksPlayed',
               { radioUID: radioUID },
               function (result) {
            if (result) {
                $('#radioProfileTracklist .tracklist, #radioProfileTracklist script').remove();

                $('#radioProfileTracklist h3').after(result);

                updateLinks();
            }
        });

    };
})();

function currentSongLink(){
    $parent = $('div.rad-tracks > ul > li');
    if ( $parent.find('a').length ) return;
    var trackName = $parent.text(),
        a = $("<a>", { href: createLink(trackName), text: trackName, target: "_blank", id: 'link-current-song'
                      // , style:"color:white !important; text-decoration:none !important;"
                     });
    console.log(a);
    $parent.html(a[0]);
    // $parent.html('').append(a);
    console.log($parent);
}
setInterval(currentSongLink, 1000);