There are 6 previous versions of this script.
// ==UserScript==
// @name Spotify + MusicBrainz
// @description Insert Spotify links on MusicBrainz website
// @version 2009-07-16_01
// @author Aurelien Mino <aurelien.mino@gmail.com>, based on the userscript Spotify + Last.fm (http://emil.hesslow.se/spotify/spotify__lastfm.user.js)
// @namespace http://userscripts.org/users/40229
// @include http://*musicbrainz.org/artist/*
// @include http://*musicbrainz.org/show/artist/*
// @include http://*musicbrainz.org/release-group/*
// @include http://*musicbrainz.org/show/release-group/*
// @include http://*musicbrainz.org/release/*
// @include http://*musicbrainz.org/show/release/*
// @include http://*musicbrainz.org/album/*
// @include http://*musicbrainz.org/show/album/*
// @include http://*musicbrainz.org/label/*
// @include http://*musicbrainz.org/show/label/*
// @include http://*musicbrainz.org/track/*
// @include http://*musicbrainz.org/show/track/*
// ==/UserScript==
(function () {
// Script Update Checker
// -- http://userscripts.org/scripts/show/20145
var version_scriptNum = 40229; // Change this to the number given to the script by userscripts.org (check the address bar)
var version_timestamp = 1247778408351; // Used to differentiate one version of the script from an older one. Use the Date.getTime() function to get a value for this.
try {
function updateCheck(forced) {if((forced)||(parseInt(GM_getValue("lastUpdate", "0")) + 86400000 <= (new Date().getTime()))) {try {GM_xmlhttpRequest({method: "GET",url: "http://userscripts.org/scripts/review/" + version_scriptNum + "?" + new Date().getTime(),headers: {'Cache-Control': 'no-cache'},onload: function(xhrResponse) {GM_setValue("lastUpdate", new Date().getTime() + ""); var rt = xhrResponse.responseText.replace(/ ?/gm, " ").replace(/<li>/gm, "\n").replace(/<[^>]*>/gm, ""); var scriptName = (/@name\s*(.*?)\s*$/m.exec(rt))[1]; GM_setValue("targetScriptName", scriptName); if (parseInt(/version_timestamp\s*=\s*([0-9]+)/.exec(rt)[1]) > version_timestamp) {if (confirm("There is an update available for the Greasemonkey script \"" + scriptName + ".\"\nWould you like to go to the install page now?")) {GM_openInTab("http://userscripts.org/scripts/show/" + version_scriptNum);}} else if (forced) {alert("No update is available for \"" + scriptName + ".\"");}}});} catch (err) {if (forced) {alert("An error occurred while checking for updates:\n" + err);}}}} GM_registerMenuCommand(GM_getValue("targetScriptName", "???") + " - Manual Update Check", function() {updateCheck(true);}); updateCheck(false);
} catch(e) {}
// Remove whitespace in the beginning and end
function trim(str) {
return str.replace(/^\s+/, '').replace(/\s+$/, '');
}
// Creates a link element
function createLink(link) {
var a = document.createElement('a');
a.href = link;
a.title = 'Listen in Spotify'
a.setAttribute('spotifyLink', true);
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';
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('spotifyLinksAdded'))
return;
topElem.setAttribute('spotifyLinksAdded', true);
// This is a last.fm url that we want to rewrite
var re = /^http:\/\/musicbrainz\.org\/(show\/|)(artist|release-group|release|track)\/(.*\.html|\?.*id=\d+)$/i;
var elems = topElem.getElementsByTagName('a');
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
// Ignore empty links
if (!elem.href || trim(elem.textContent) == '' || elem.textContent.substring(4,0) == 'http')
continue;
//~ // Check if the link matches
if (m = re.exec(elem.href)) {
// Hackish, exclude some invalid links
if( elem.textContent == 'Releases'
|| elem.textContent == 'Various Artists' )
continue;
// Create the spotify url
var q;
var type = m[2];
if (type == 'artist') {
q = ['artist%3a%22'+ encodeURIComponent(trim(elem.textContent)) +'%22'];
} else if (type == 'release') {
q = ['album%3a%22'+ encodeURIComponent(trim(elem.textContent)) +'%22'];
} else if (type == 'release-group') {
q = ['album%3a%22'+ encodeURIComponent(trim(elem.textContent)) +'%22'];
} else if (type == 'track') {
q = ['track%3a%22'+ encodeURIComponent(trim(elem.textContent)) +'%22'];
}
var a = createLink('spotify:search:'+ q.join('%20'));
// Insert the link after the found link
// Check if it already have a spotify url
if (!elem.nextSibling || !elem.nextSibling.hasAttribute || !elem.nextSibling.hasAttribute('spotifyLink')) {
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);
// Find links and add spotify links to them
var body = document.body;
addLinks(body);
})();
