Zicmama + Last.fm

By zicmama Last update Jan 26, 2010 — Installed 257 times.

There are 13 previous versions of this script.

// ==UserScript==
// @name           Zicmama + Last.fm
// @namespace      http://www.zicmama.com/
// @description    Inserts Zicmama search links on Last.fm. Then copy your favorite artist album on your ZicDisk!!
// @include        http://last.fm/*
// @include        http://*.last.fm/*
// @include        http://lastfm.*/*
// @include        http://*.lastfm.*/*
// ==/UserScript==
//

// Last.fm seems to urlencode strings twice so remove the second urlencode step.
function cleanurl(str) {
    return str.replace(/%25([0-9]{2})/, '%$1');
}

// Remove whitespace in the beginning and end
function trim(str) {
    return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

function noparenthese(str) {
    // Keep left of "(" : Remove in parenthesis part
    var mystr = str.split('%28');
    return trim(mystr[0]);
}
// Creates a link element
function createLink(link, image) {
    var a = document.createElement('a');
    a.href = link;
    a.title = 'Search in Zicmama'
    a.setAttribute('zicmamaLink', true);
    a.setAttribute('target', 'zicmama');
    var img = document.createElement('img');
    img.style.border = 'none';
    img.style.marginLeft = '3px';
    img.src = 'data:image/png;charset=utf-8;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAGlJREFUeNpi%2BP%2F%2FPwMMuy1j%2BA%2FEBchiIMzEgAn63ZczzkcWwKaoEYgdgAr7cSraGfm%2FAUgZAnECPpNACj8AqQd4FQGtKgBSB2B8FqigArLxQPABaNoEFEVAcB6IBZCsW4DPdwewWQ8QYACnBy8V7gSvaAAAAABJRU5ErkJggg%3D%3D';

    if (image == 'p2p')
    img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9kJFxEPOxJM1rwAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAC2UlEQVQoz4WSXUiTYRTH/8+77X11W6bO9s5ykaalollSakQf60sJYmQpZJbYlV0kQRddhFjihUVSYQli0pdF2neECkmmWeYH6TSzUpu2Oefmx2zTvVO3pysLw+h3dTj/8+OciwMsgmn0x+783NNnKf2ZbHcbJR5qCVhsTvR3g1LKVNwrvsuKezNev3mu/dZp8cTFJ9UsJpP5wjIyACW/Cvfulh1nmS9lEcEuah1jYLA6ic0pb1P5xuxPSTtqIYR4JmZH4CfhwczLSn4VKKWJZlNHGSeewYARZGDwJ9m7VU7nRvUbWen7/scVDzIppSI/Cf9n8+e2FsiUCq83tY+63fZPIemHEpCe/QQNHzpx8/JhJMb7oqy8FTbXLCKiNdUx20+dVCtl/b/PbmmszXv+8GJOdqYGnI8c4QnnIWLF8Jd6obxoD0IDKCYnHci/3o6VYbw99cidVBEANNfWp/V03b8aF6GgoSoxMfZ1YUOwCI9ejaOmwAdqezvmDM0g4zrEBIESm5nrdIcsEwuCZd2L23mFhrcNVLvTQWw1w/AjFLtkcjgEF3o6BSgCp0EkbrCERXktB6LWIGubdpixTJAUqm9V2UetZHZyDN5OgAgSVOlYqJdyOFPpjWs1HJhpoLKRBb9MQZLSTrxdHsxnMgqP444tKtvJq8NRUi+jHUNLwDrdsE9QWAUJTMN2aKPdMFplmBDkEG06NRUaGZsGAGLZiuBet3MmqG1NWH1paXGUbuglPg7KkRnpgDJZjmcdDNRSBhfqOYQlHcOBg1vOKf2kxgVPQiklpn5dcVVdW9b3p4Xwd5mgCXQhMACo+ypCn3QttLk39OujN6wmhNAFslswg+E41dSUwDc3d1c33L4SKDPrqMLLRgbGfbE5p9Sxe0fivhnJSJOcUc3hX0zZbVJdd8+tkkv5NFej9hQX5NIhgyF2Pp+mVvwXo8WW0fiuqUOv1xcBwKjZtCD/BaHAOXPnD5VRAAAAAElFTkSuQmCC';

    a.appendChild(img);

    return a;
}


// Add links for the content under this element.
function addLinks(topElem) {
    // Check if we already added links for this content
    if (topElem.hasAttribute('zicmamaLinksAdded'))
        return;
    topElem.setAttribute('zicmamaLinksAdded', true);

    // This is a last.fm url that we want to rewrite
    var re = /^http:\/\/(.*\.|)(last\.fm|lastfm\.[^\/]+)\/music\/([^\?#]*)$/i;
    var elems = topElem.getElementsByTagName('a');
    for (var i = 0; i < elems.length; i++) {
        var elem = elems[i];
        // Ignore image links
        if (!elem.href || trim(elem.textContent) == '' || elem.className.match(/\blfmButton\b/))
            continue;
        // Check if the link matches
        if (m = re.exec(elem.href)) {
            var found = false;
            var songlink = false;
            // Go though parts and check if it is an url that we want to change
            parts = m[3].split('/');
            for (var j = 0; j < parts.length; j++) {
                if (parts[j][0] == '+') {
                    found = true;
                    break;
                }
//		if (parts[j][0] == '_') {
//                    songlink = true;
//                    break;
//                }
            }
	    // Identify song links like Artist/_/Title
	    if (parts[0] && parts[1] == '_' && parts[2] && parts[0].length > 1 && parts[1].length == 1 && parts[2].length > 1)
		songlink = true;

            if (found)
                continue;

            // Ignore links in the left menu and some other places
            var p = elem;
            while (p != null) {
                if (p.id && p.id.match(/^(secondaryNavigation|featuredArtists)$/) || p.className && p.className.match(/\b(pagehead|image|artistsMegaWithFeatured)\b/)) {
                    found = true;
                    break;
                }
                p = p.parentNode;
            }

	    if (songlink) {
		// Create the Zicmama url
           	q = [ cleanurl( noparenthese(parts[0]) ) ];
//            	if (parts[1] && parts[1] == '_')
//               		q.push('+');
		if (parts[2])
			q.push( cleanurl( noparenthese(parts[2]) ) );
		var a = createLink('http://www.zicmama.com/index.php?last=song&CORE_OP=s_action&search=&minsize=2&maxsize=20&keywords='+ q.join('+'), 'p2p');
		// Insert the link after the found link
		// Check if it already have a Zicmama url
		if (!elem.nextSibling || !elem.nextSibling.hasAttribute || !elem.nextSibling.hasAttribute('zicmamaLink')) {
			elem.parentNode.insertBefore(a, elem.nextSibling);
		}
	    }

            if (found)
                continue;
            // Create the Zicmama url
            q = [ cleanurl( noparenthese(parts[0]) ) ];
            if (parts[1] && parts[1] != '_')
                q.push( cleanurl( noparenthese( parts[1] )));
            if (parts[2])
                q.push( cleanurl( noparenthese( parts[2] )));
		var a = createLink('http://www.zicmama.com/index.php?last=z&CORE_OP=amazon_action&SearchIndex=Music&Action=Search&ItemPage=1&letters=all&Keywords='+ q.join('+'), '');

            // Insert the link after the found link
            // Check if it already have a Zicmama url
            if (!elem.nextSibling || !elem.nextSibling.hasAttribute || !elem.nextSibling.hasAttribute('zicmamaLink')) {
                elem.parentNode.insertBefore(a, elem.nextSibling);
            }
        }
    }
}

// Add listener so if the content changes we add links to the new content
document.addEventListener('DOMNodeInserted', function(ev){ addLinks(ev.originalTarget); }, true);

// Add links to titles like the artist name on the artist page.
var body = document.body;
var div = document.getElementById('catalogueHead');
if (body.className && div) {
    if (body.className.match(/\br\-artist\b/)) {
        // artist page
        var h1 = div.getElementsByTagName('h1')[0];
        h1.appendChild(createLink('http://www.zicmama.com/index.php?last=0&CORE_OP=amazon_action&SearchIndex=Music&Action=Search&ItemPage=1&letters=all&Keywords='+ encodeURIComponent(h1.textContent)), '');
    } else if (body.className.match(/\br\-album\b/)) {
        // album page
        var h1 = div.getElementsByTagName('h1')[0];
        var a = createLink('http://www.zicmama.com/index.php?last=1&CORE_OP=amazon_action&SearchIndex=Music&Action=Search&ItemPage=1&letters=all&Keywords='+ encodeURIComponent(h1.firstChild.textContent) +'+'+ encodeURIComponent(h1.lastChild.textContent), '');
        h1.appendChild(a);
        div.previousSibling.previousSibling.getElementsByTagName('h1')[0].appendChild(a.cloneNode(true))
    } else if (body.className.match(/\br\-track\b/)) {
        // track page: Not Working h1 is poluted!
        var h1 = div.getElementsByTagName('h1')[0];
        var a = createLink('http://www.zicmama.com/index.php?last=2&CORE_OP=s_action&search=&minsize=2&maxsize=20&keywords='+ encodeURIComponent(h1.firstChild.textContent) +'+'+ encodeURIComponent(h1.lastChild.textContent.substring(3)), '');
        h1.appendChild(a);
        div.previousSibling.previousSibling.getElementsByTagName('h1')[0].appendChild(a.cloneNode(true))
    }
}

// Find links and add Zicmama links to them
addLinks(body);

//////////////////
//Auto Updater///
////////////////
//Auto Script Updater from http://userscripts.org/scripts/review/38017
//Thanks to the Author sizzlemctwizzle
// Copyleft Michael Medley <medleymind@gmail.com>, All Wrongs Reserved

scr_meta=<><![CDATA[ // Make sure to copy this line right above the script metadata
// ==UserScript==
// @name           Another Auto Update Script
// @namespace      sizzlemctwizzle
// @description    Include the code in your user scripts so they check for updates automatically
// @version        1.1.9
// @include        *
// ==/UserScript==
]]></>.toString(); // Make sure to copy this line right below metadata

var AnotherAutoUpdater = {
 // Config values, change these to match your script
 id: '40058', // Script id on Userscripts.org
 days: 2, // Days to wait between update checks

 // Don't edit after this line, unless you know what you're doing ;-)
 name: /\/\/\s*@name\s+(.*)\s*\n/i.exec(scr_meta)[1],
 version: /\/\/\s*@version\s+(.*)\s*\n/i.exec(scr_meta)[1].replace(/\./g, ''),
 time: new Date().getTime(),
 call: function(response) {
    GM_xmlhttpRequest({
      method: 'GET',
	  url: 'https://userscripts.org/scripts/source/'+this.id+'.meta.js',
	  onload: function(xpr) {AnotherAutoUpdater.compare(xpr,response);}
      });
  },
 compare: function(xpr,response) {
    this.xversion=/\/\/\s*@version\s+(.*)\s*\n/i.exec(xpr.responseText);
    this.xname=/\/\/\s*@name\s+(.*)\s*\n/i.exec(xpr.responseText);
    if ( (this.xversion) && (this.xname[1] == this.name) ) {
      this.xversion = this.xversion[1].replace(/\./g, '');
      this.xname = this.xname[1];
    } else {
      if ( (xpr.responseText.match("the page you requested doesn't exist")) || (this.xname[1] != this.name) ) 
	GM_setValue('updated_'+this.id, 'off');
      return false;
    }
    if ( (+this.xversion > +this.version) && (confirm('A new version of the '+this.xname+' user script is available. Do you want to update?')) ) {
      GM_setValue('updated_'+this.id, this.time+'');
      top.location.href = 'https://userscripts.org/scripts/source/'+this.id+'.user.js';
    } else if ( (this.xversion) && (+this.xversion > +this.version) ) {
      if(confirm('Do you want to turn off auto updating for this script?')) {
	GM_setValue('updated_'+this.id, 'off');
	GM_registerMenuCommand("Auto Update "+this.name, function(){GM_setValue('updated_'+this.id, new Date().getTime()+''); AnotherAutoUpdater.call(true);});
	alert('Automatic updates can be re-enabled for this script from the User Script Commands submenu.');
      } else {
	GM_setValue('updated_'+this.id, this.time+'');
      }
    } else {
      if(response) alert('No updates available for '+this.name);
      GM_setValue('updated_'+this.id, this.time+'');
    }
  },
  check: function() {
    if (GM_getValue('updated_'+this.id, 0) == "off")
      GM_registerMenuCommand("Enable "+this.name+" updates", function(){GM_setValue('updated_'+this.id, new Date().getTime()+'');AnotherAutoUpdater.call(true)});
    else {
      if (+this.time > (+GM_getValue('updated_'+this.id, 0) + 1000*60*60*24*this.days)) {
        GM_setValue('updated_'+this.id, this.time+'');
        this.call();
      }
      GM_registerMenuCommand("Check "+this.name+" for updates", function(){GM_setValue('updated_'+this.id, new Date().getTime()+'');AnotherAutoUpdater.call(true)});
    }
  }
};
if (self.location == top.location && typeof GM_xmlhttpRequest != 'undefined') AnotherAutoUpdater.check();

// As an alternative to including the code in your script you can now use an @require
// @require instructions here: http://userscripts.org/topics/34594
// I'm lazy and failed to keep the compressed version up-to-date so if you really want it bug me