Skip to content

Instantly share code, notes, and snippets.

@yingted
Created November 18, 2013 20:48
Show Gist options
  • Save yingted/7535027 to your computer and use it in GitHub Desktop.
Save yingted/7535027 to your computer and use it in GitHub Desktop.
lyrics for Astral Radio player
// ==UserScript==
// @name lyrics for Astral Radio player
// @description Check http://proxy.amri.ca/ or https://en.wikipedia.org/wiki/Bell_Media_Radio for a list
// @namespace localhost
// @include http://player.chumfm.com/
// @version 0.1
// @grant GM_xmlhttpRequest
// ==/UserScript==
addEventListener("load",function(){
var lyricsDiv=document.createElement("div"),container=document.getElementById("pageContainer");
lyricsDiv.style.position="absolute";
lyricsDiv.style.width="200px";
lyricsDiv.style.height="300px";
lyricsDiv.style.backgroundColor="rgba(255,255,255,.8)";
lyricsDiv.style.zIndex=100;
lyricsDiv.style.padding=".5em";
lyricsDiv.style.paddingLeft="1.5em";
lyricsDiv.style.textIndent="-1em";
//lyricsDiv.style.fontSize="150%";
lyricsDiv.style.overflowY="auto";
lyricsDiv.innerHTML="<i>loading…</i>";
container.insertBefore(lyricsDiv,container.firstChild);
var curUrl=null;
unsafeWindow.livePlayer.createNpSongString=(function(wrapped){
return function(){
var ret=wrapped.apply(this,arguments);
if(this.metadata){
//console.log("metadata",this.metadata);
var song=this.metadata.nowplaying.currentTrack,url="http://lyrics.wikia.com/api.php?artist="+encodeURIComponent(song.artistName)+"&song="+encodeURIComponent(song.trackName)+"&fmt=realjson";
//console.log("url",url);
if(url==curUrl)
return;
curUrl=url;
function updateLyrics(lyrics){
if(url!=curUrl)
return;
if(lyrics==null)
lyricsDiv.innerHTML="<i>could not find lyrics</i>";
else if(typeof lyrics=="string")
lyrics=document.createTextNode(lyrics);
console.log("lyrics",lyrics);
lyricsDiv.innerHTML="";
lyricsDiv.appendChild(lyrics);
}
setTimeout(function(){
try{
GM_xmlhttpRequest({
url:url,
onerror:console.error,
onload:function(data){
//console.log("got data");
//console.log("data",data);
if(url!=curUrl)
return;
//console.log("good url");
var obj=JSON.parse(data.responseText);
//console.log("parsed",obj);
if(/&amp;action=edit$/.test(obj.url)){
updateLyrics(null);
return;
}
updateLyrics(obj.lyrics);
//console.log("full lyrics",obj.url);
GM_xmlhttpRequest({
url:obj.url,
onerror:console.error,
onload:function(data){
//console.log("data2",data);
if(url!=curUrl)
return;
//console.log("good url2");
try{
var html=data.responseText.match(/^<div class='lyricbox'>.*<\/div>(.*)<!-- $/m)[1];
console.log("html",html);
var div=document.createElement("div"),df=document.createDocumentFragment();
div.innerHTML=html;
for(var i=0,a=div.childNodes;i<a.length;++i)
switch(a[i].nodeType){
case Node.ELEMENT_NODE:
switch(a[i].nodeName){
case"BR":
if(df.childNodes.length){
var prev=df.childNodes[df.childNodes.length-1];
if(prev.nodeType==Node.ELEMENT_NODE&&prev.nodeName=="BR")
continue;
}
df.appendChild(a[i]);
continue;
case"B":
case"I":
case"U":
case"STRONG":
case"EM":
case"SPAN":
var elt=document.createElement(a[i].nodeName);
elt.textContent=a[i].textContent;
//console.log("elt",elt);
df.appendChild(elt);
default:
continue;
}
case Node.TEXT_NODE:
//console.log("text",a[i]);
var p=document.createElement("p");
p.appendChild(a[i]);
df.appendChild(p);
df.appendChild(document.createElement("br"));
break;
}
console.log("df",df);
updateLyrics(df);
}catch(e){
console.error(e);
}
},
});
},
});
}catch(e){
console.log("caught error");
console.error(e);
}
});
}
return ret;
};
})(unsafeWindow.livePlayer.createNpSongString);
},false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment