Created
November 25, 2010 02:40
-
-
Save BenWard/714812 to your computer and use it in GitHub Desktop.
A bookmarklet to move you from one music playback service to another. Can take you to MOG or to Rdio from pages on MOG, Rdio, Last.FM, Spotify, and some MySpace pages. Set `using: 'mog'` or `using: 'rdio'` as appropriate. Work in progress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PlayMusic = { | |
using: 'mog', | |
esc: function(s) { | |
return s.replace(' ', '-'); | |
}, | |
sources: { | |
'rdio': { | |
match: /rdio.com/, | |
handler: function(url) { | |
var artist='', album='', track=''; | |
var parts = url.split('/'); | |
for(var i=0; i < parts.length; i++) { | |
p=parts[i]; | |
switch(p) { | |
case 'artist': | |
i = i+1; | |
artist = parts[i]; | |
break; | |
case 'album': | |
i = i+1; | |
album = parts[i]; | |
break; | |
case 'track': | |
i = i+1; | |
track = parts[i]; | |
break; | |
default: | |
break; | |
} | |
} | |
return { artist: artist, album: album, track: track }; | |
} | |
}, | |
'spotify': { | |
match: /spotify\.com/, | |
handler: function() { | |
var track='', artist='', album='', | |
links = document.getElementsByTagName('a'); | |
for(var i=0;l=links[i];i++) { | |
if(l.href.match(/^spotify:track:/)) { | |
track = l.innerText; | |
} | |
else if(l.href.match(/open.spotify.com\/artist/)) { | |
artist = l.title; | |
} | |
else if(l.href.match(/open.spotify.com\/album/)) { | |
album = l.title; | |
} | |
} | |
return { artist: artist, album: album, track: track }; | |
} | |
}, | |
'mog': { | |
match: /mog\.com/, | |
handler: function(url) { | |
url = url.replace(/\-/g, ' '); | |
var parts = url.match(/(artists|albums|tracks)\/mn[0-9]+\/([a-z0-9\-]+)(\/([a-z0-9\-]+))?(\/([a-z0-9\-]+))?/i); | |
var rtn = {}; | |
if(parts[2]) { | |
rtn.artist = parts[2]; | |
} | |
if(parts[4]) { | |
rtn.album = parts[4]; | |
} | |
if(parts[6]) { | |
rtn.track = parts[6]; | |
} | |
return rtn; | |
} | |
}, | |
'lastfm': { | |
match: /(last\.fm|lastfm\.[a-z]{2,3})/, | |
handler: function(url) { | |
url = url.replace(/(\+|%20|_)/g, ' '); | |
var parts = url.match(/(music(\/\+noredirect)?)\/([a-z0-9\-]+)(\/([a-z0-9\-]+))?(\/([a-z0-9\-]+))?/i); | |
// Handle Last.FM track view outside of an album context. | |
if('_' === parts[5]) { | |
parts[5] = undefined; | |
} | |
var rtn = {}; | |
if(parts[3]) { | |
rtn.artist = parts[3]; | |
} | |
if(parts[5]) { | |
rtn.album = parts[5]; | |
} | |
if(parts[7]) { | |
rtn.track = parts[7]; | |
} | |
return rtn; | |
} | |
}, | |
'hypem': { | |
match: /hypem.com/, | |
handler: function(url) { | |
url = url.replace(/\+/g, ' '); | |
console.log(url); | |
if(match = url.match(/artist\/(a-z )/i)) { | |
return { artist: match[1], album: undefined, track: undefined }; | |
} | |
else if(match = url.match(/track\/[0-9]+\/(.+)/i)) { | |
split = match[1].split(' - ', 2); | |
console.log(split); | |
return { artist: split[0], track: split[1], album: undefined }; | |
} | |
else { | |
return undefined; | |
} | |
} | |
}, | |
'myspace': { | |
match: /myspace\.com/, | |
handler: function() { | |
artist = document.getElementsByClassName('userLink')[0].innerText; | |
return { artist: artist }; | |
} | |
} | |
}, | |
dest: { | |
'mog': { | |
redirect: function(t) { | |
var url = undefined; | |
if(t.artist) { | |
url = 'http://mog.com/music/'+t.artist.replace(/ /g,'-'); | |
if(t.album) { | |
url+='/'+t.album.replace(/ /g,'-'); | |
if(t.track) { | |
url+='/'+t.track.replace(/ /g,'-'); | |
} | |
} | |
} | |
return url; | |
} | |
}, | |
'rdio': { | |
redirect: function(t) { | |
var url = undefined; | |
if(t.artist) { | |
url = 'http://rdio.com/artist/'+t.artist; | |
if(t.album) { | |
url += '/album/'+t.album.replace(/ /g,'_'); | |
if(t.track) { | |
url += '/track/'+t.track.replace(/ /g,'_'); | |
} | |
} | |
} | |
return url; | |
} | |
} | |
}, | |
redirect:function() { | |
for(service in PlayMusic.sources) { | |
if(window.location.href.match(PlayMusic.sources[service].match)) { | |
if(PlayMusic.using === service) { | |
console.log("Can't redirect away; this site is configured as your prefered playback service!"); | |
return; | |
} | |
music_info = PlayMusic.sources[service].handler(window.location.href); | |
if(undefined === music_info) { | |
alert("Sorry, couldn't figure out a destination URL for this page."); | |
return; | |
} | |
otherplace = PlayMusic.dest[PlayMusic.using].redirect(music_info); | |
if(undefined === otherplace) { | |
alert("Sorry, couldn't figure out a destination URL for this page."); | |
return; | |
} | |
window.location.href = otherplace; | |
return; | |
} | |
} | |
alert("Sorry, couldn't translate this page into a music URL"); | |
return; | |
} | |
}; | |
PlayMusic.redirect(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:PlayMusic%20=%20{using:%20%27mog%27,esc:%20function(s)%20{return%20s.replace(%27%20%27,%20%27-%27);},sources:%20{%27rdio%27:%20{match:%20/rdio.com/,handler:%20function(url)%20{var%20artist=%27%27,%20album=%27%27,%20track=%27%27;var%20parts%20=%20url.split(%27/%27);for(var%20i=0;%20i%20<%20parts.length;%20i++)%20{p=parts[i];switch(p)%20{case%20%27artist%27:i%20=%20i+1;artist%20=%20parts[i];break;case%20%27album%27:i%20=%20i+1;album%20=%20parts[i];break;case%20%27track%27:i%20=%20i+1;track%20=%20parts[i];break;default:break;}}return%20{%20artist:%20artist,%20album:%20album,%20track:%20track%20};}},%27spotify%27:%20{match:%20/spotify\.com/,handler:%20function()%20{var%20track=%27%27,%20artist=%27%27,%20album=%27%27,links%20=%20document.getElementsByTagName(%27a%27);for(var%20i=0;l=links[i];i++)%20{if(l.href.match(/^spotify:track:/))%20{track%20=%20l.innerText;}else%20if(l.href.match(/open.spotify.com\/artist/))%20{artist%20=%20l.title;}else%20if(l.href.match(/open.spotify.com\/album/))%20{album%20=%20l.title;}}return%20{%20artist:%20artist,%20album:%20album,%20track:%20track%20};}},%27mog%27:%20{match:%20/mog\.com/,handler:%20function(url)%20{url%20=%20url.replace(/\-/g,%20%27%20%27);var%20parts%20=%20url.match(/(artists|albums|tracks)\/mn[0-9]+\/([a-z0-9\-]+)(\/([a-z0-9\-]+))?(\/([a-z0-9\-]+))?/i);var%20rtn%20=%20{};if(parts[2])%20{rtn.artist%20=%20parts[2];}if(parts[4])%20{rtn.album%20=%20parts[4];}if(parts[6])%20{rtn.track%20=%20parts[6];}return%20rtn;}},%27lastfm%27:%20{match:%20/(last\.fm|lastfm\.[a-z]{2,3})/,handler:%20function(url)%20{url%20=%20url.replace(/(\+|%20|_)/g,%20%27%20%27);var%20parts%20=%20url.match(/(music(\/\+noredirect)?)\/([a-z0-9\-]+)(\/([a-z0-9\-]+))?(\/([a-z0-9\-]+))?/i);if(%27_%27%20===%20parts[5])%20{parts[5]%20=%20undefined;}var%20rtn%20=%20{};if(parts[3])%20{rtn.artist%20=%20parts[3];}if(parts[5])%20{rtn.album%20=%20parts[5];}if(parts[7])%20{rtn.track%20=%20parts[7];}return%20rtn;}},%27hypem%27:%20{match:%20/hypem.com/,handler:%20function(url)%20{url%20=%20url.replace(/\+/g,%20%27%20%27);console.log(url);if(match%20=%20url.match(/artist\/(a-z%20)/i))%20{return%20{%20artist:%20match[1],%20album:%20undefined,%20track:%20undefined%20};}else%20if(match%20=%20url.match(/track\/[0-9]+\/(.+)/i))%20{split%20=%20match[1].split(%27%20-%20%27,%202);console.log(split);return%20{%20artist:%20split[0],%20track:%20split[1],%20album:%20undefined%20};}else%20{return%20undefined;}}},%27myspace%27:%20{match:%20/myspace\.com/,handler:%20function()%20{artist%20=%20document.getElementsByClassName(%27userLink%27)[0].innerText;return%20{%20artist:%20artist%20};}}},dest:%20{%27mog%27:%20{redirect:%20function(t)%20{var%20url%20=%20undefined;if(t.artist)%20{url%20=%20%27http://mog.com/music/%27+t.artist.replace(/%20/g,%27-%27);if(t.album)%20{url+=%27/%27+t.album.replace(/%20/g,%27-%27);if(t.track)%20{url+=%27/%27+t.track.replace(/%20/g,%27-%27);}}}return%20url;}},%27rdio%27:%20{redirect:%20function(t)%20{var%20url%20=%20undefined;if(t.artist)%20{url%20=%20%27http://rdio.com/artist/%27+t.artist;if(t.album)%20{url%20+=%20%27/album/%27+t.album.replace(/%20/g,%27_%27);if(t.track)%20{url%20+=%20%27/track/%27+t.track.replace(/%20/g,%27_%27);}}}return%20url;}}},redirect:function()%20{for(service%20in%20PlayMusic.sources)%20{if(window.location.href.match(PlayMusic.sources[service].match))%20{if(PlayMusic.using%20===%20service)%20{console.log(%22Can%27t%20redirect%20away;%20this%20site%20is%20configured%20as%20your%20prefered%20playback%20service!%22);return;}music_info%20=%20PlayMusic.sources[service].handler(window.location.href);if(undefined%20===%20music_info)%20{alert(%22Sorry,%20couldn%27t%20figure%20out%20a%20destination%20URL%20for%20this%20page.%22);return;}otherplace%20=%20PlayMusic.dest[PlayMusic.using].redirect(music_info);if(undefined%20===%20otherplace)%20{alert(%22Sorry,%20couldn%27t%20figure%20out%20a%20destination%20URL%20for%20this%20page.%22);return;}window.location.href%20=%20otherplace;return;}}alert(%22Sorry,%20couldn%27t%20translate%20this%20page%20into%20a%20music%20URL%22);return;}};PlayMusic.redirect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment