User:Senvaikis/global.js

var wpTb1 var btnArt var btnAlb var request = false; var _timer; var btnBusyImage var btnArtImage var btnAlbImage

var wpSumm var btnLang var oreg var gLangSet var gConf var resultSpan var gLang = ''; var summary=''; var request = false; var selText=''; var artistName var Header var Footer var PageStr

function init{ if (_timer) clearInterval(_timer); var tlbr=window.document.getElementById("toolbar"); if (wgNamespaceNumber==6){ btnBusyImage = "http://images3.wikia.nocookie.net/__cb20100126184912/lyricwiki/images/7/76/LW_btn_busy.gif" btnArtImage = "http://images1.wikia.nocookie.net/__cb20101206073518/lyricwiki/images/d/d8/Lw_btn_user.png"; btnAlbImage ="http://images3.wikia.nocookie.net/__cb20101206073617/lyricwiki/images/d/d1/Lw_btn_disc.png" btnAlb=window.document.getElementById("mw-editbutton-headline"); var objImage = new Image; objImage.src=btnBusyImage; btnAlb.src = btnAlbImage; btnAlb.title = "Set AlbumCover"; btnAlb.onclick=CheckAlbum1; btnArt=window.document.getElementById("mw-editbutton-image"); btnArt.src = btnArtImage; btnArt.title = "Set ArtistPhoto"; btnArt.style.opacity=0.8; btnArt.style.filter = "alpha(opacity=80)" //alert(btnArt);not null btnArt.onclick=CheckArtist; request = new XMLHttpRequest; //alert(request); object created, not null //but this line throws an error (unspecified): //request.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); }	else if (wgNamespaceNumber==0){ var btnForm=window.document.getElementById("mw-editbutton-headline"); btnForm.src = "http://images1.wikia.nocookie.net/lyricwiki/images/6/6b/Lw_Button_Format.png"; btnForm.title = "Format page"; btnForm.onclick=FormatPage; btnLang=window.document.getElementById("mw-editbutton-image"); btnLang.src = "http://images4.wikia.nocookie.net/lyricwiki/images/1/19/Lw_btn_lang.png"; btnLang.title = "Check lyrics language"; btnLang.style.opacity=0.8; btnLang.style.filter = "alpha(opacity=80)" btnLang.onclick=CheckLang; resultSpan=document.createElement("span"); resultSpan.style.cssText = "display:none;border:1px silver solid;background-color:#FFFFEA;font-size: 10pt;color: #004080;margin-bottom:15px;paddig-left:5px;padding-right:5px;"; tlbr.appendChild(resultSpan); wpTb1.onmouseup = mouseUpHandler; request = new XMLHttpRequest; oreg =/(\{\{Song\s*\|[^}]*}})?[\s\S]*?([\n\r]*[\n\r]*([\s\S]*?)[\n\r]*<\/lyric[s]?>[\n\r]*)[\s\S]*?(\{\{SongFooter[\s\S]*?\}\})/; gLangSet={'af':'Afrikaans','sq':'Albanian','am':'Amharic','ar':'Arabic','hy':'Armenian','az':'Azerbaijani','eu':'Basque','be':'Belarusian','bn':'Bengali','bh':'Bihari','bg':'Bulgarian','my':'Burmese','ca':'Catalan','chr' :'Cherokee','zh':'Chinese','zh-CN':'Chinese Simplified','zh-TW':'Chinese Traditional','hr':'Croatian','cs':'Czech','da':'Danish','dv':'Dhivehi','nl':'Dutch','en':'English','eo':'Esperanto','et':'Estonian','fil' :'Filipino','fi':'Finnish','fr':'French','gd':'Gaelic','gl':'Galician','ka':'Georgian','de':'German','el':'Greek','gn':'Guarani','gu':'Gujarati','iw':'Hebrew','hi':'Hindi','hu':'Hungarian','is':'Icelandic','id':'Indonesian','iu':'Inuktitut','ga':'Irish','it':'Italian','ja':'Japanese','kn':'Kannada','kk':'Kazakh','km':'Khmer','ko':'Korean','ku':'Kurdish','ky':'Kyrgyz','lo':'Laothian','lv':'Latvian','lt':'Lithuanian','mk':'Macedonian','ms':'Malay','ml':'Malayalam','mt':'Maltese','mr':'Marathi','mn':'Mongolian','ne':'Nepali','no':'Norwegian','or':'Oriya','ps':'Pashto','fa':'Persian','pl':'Polish','pt':'Portuguese','pa':'Punjabi','ro':'Romanian','ru':'Russian','sa':'Sanskrit','sr':'Serbian','sd':'Sindhi','si':'Sinhalese','sk':'Slovak','sl':'Slovene','es':'Spanish','sw':'Swahili','sv':'Swedish','tg':'Tajik','ta':'Tamil','tl':'Tagalog','te':'Telugu','th':'Thai','bo':'Tibetan','tr':'Turkish','uk':'Ukrainian','ur':'Urdu','uz':'Uzbek','ug':'Uighur','vi':'Vietnamese','cy':'Welsh',''  : 'UNKNOWN'}; } }

//********Song page edit stuff********* function mouseUpHandler{ selText=GetSelectedText; var opac = (selText =='') ? 80 : 100; setBtnOpacity(opac); if (selText =='') btnLang.style.filter = "alpha(opacity=80)"; else btnLang.style.filter = "alpha(opacity=100)"; resultSpan.style.display="none"; }

function setBtnOpacity(opac) { if (isIE) btnLang.style.filter = "alpha(opacity=" + opac +")"; else btnLang.style.opacity = opac/100; }

function FormatPage { summary=''; PageStr = wpTb1.value var m =PageStr.match(oreg); if (m) { Header=m[1]; var Lyrics =m[2]; Footer =m[4]; var newLyrics="\n\n \n" + m[3] + "\n<\/lyrics>\n\n"; newLyrics=newLyrics.replace(/\n +/g,"\n"); //LeftTrim newLyrics=newLyrics.replace(/(\r?\n){2,}/g,"\n\n"); //Remove extra empty lines newLyrics=newLyrics.replace(/\n(\S)/g, function($0,$1){return '\n' + $1.toUpperCase;}); //Sentence-UpperCase if (newLyrics.match(/[’´`‘„“”«»]/g)) { newLyrics=newLyrics.replace(/[’´`‘]/g,"'").replace(/[„“”«»]/g,'"'); //Apostrophes   }    if (Lyrics != newLyrics) {      summary = "formatted; ";      PageStr=PageStr.replace(Lyrics,newLyrics);    }    var m =Footer.match(/\|\s*([^= ]+?)\s*= */g);    if (m) {      var maxLen=0;      for(i=0; i 0) {      wpSumm.value = summary;      wpTb1.value = PageStr;    }  } }

function CheckAlbum { if (Header.length>0) { var m = Header.match(/{{Song\s*\|\s*(([^}|]*?) \((\d{4})\))?\s*\|[^}]*}}/); if (m) { var AlbumTitle= m[2]; if (AlbumTitle) { m = Footer.match(/\|\s*album\s*=\s*([^}|\r\n]*)[\r\n]*/); if (m) { if ((m[1]=='')||(m[1] == AlbumTitle)) { var tfoot=Footer.replace(m[0],''); PageStr = PageStr.replace(Footer,tfoot); summary+="-sf.album; "; wpTb1.scrollTop = wpTb1.scrollHeight; }       }       }     }   } }

function AlignPar(parStr, len) { return('|' + parStr.rightPad(' ',len) + '= '); }

String.prototype.rightPad = function(pChr, len) { var str = this; while (str.length < len) str = str + pChr; return str; }

function CheckLang { if (resultSpan.style.display == "none") GetLang else UpdateLang; }

function parseGresp { if (request.readyState == 4) { if ((request.status == 200)||(request.status == 0)) { var m = request.responseText.match("\"language\":\"([-a-z]+)\",.*?\"confidence\":([0-9-.]+)},"); if (m) { gLang=gLangSet[m[1]]; gConf=m[2]; resultSpan.innerHTML = gLang + " (Gc: " + Math.round(m[2]*100)/100 + "); "; } else resultSpan.innerHTML = "Google check failed."; resultSpan.style.display = "inline"; } } }

function UpdateLang { var detRez= (isIE) ? resultSpan.innerText : resultSpan.textContent; resultSpan.style.display = "none"; if (detRez != 'Google check failed.') { var m=wpTb1.value.match(/{{SongFooter[\s\S]*?((\|\s*language\s*=) *([^|} \n\r]*))[\s\S]*?}}/); if (m) { var Footer =m[0]; var tfoot var currLang = m[3]; if (currLang != gLang) { tfoot = Footer.replace(m[1], m[2] + " " + gLang); if (currLang == '') wpSumm.value += "+lang: " + resultSpan.innerText; else wpSumm.value += "lang: " + currLang + "->" + resultSpan.innerText; } else return(false); }   else { tfoot = Footer.replace("SongFooter", "SongFooter\n|language   = " + gLang); wpSumm.value += "+lang: " + resultSpan.innerText; }   wpTb1.value=wpTb1.value.replace(Footer,tfoot); wpTb1.scrollTop = wpTb1.scrollHeight; } }

function GetSelectedText { return (isIE) ? document.selection.createRange.text : wpTb1.value.substring(wpTb1.selectionStart, wpTb1.selectionEnd); }

function GetLang { if (selText=='') { var m = wpTb1.value.match(/([\s\S]+?)<\/lyric[s]?>/); selText=(m) ? m[1] : ''; }  if (selText=='') { alert("No lyrics!"); return; } selText=selText.replace(new RegExp(/\n|\r\|\s*/g)," "); resultSpan.innerHTML="Requesting Google api..."; resultSpan.style.display="inline"; var urlstr = "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=" + encodeURI(selText); request.open("GET", urlstr, true); request.onreadystatechange = parseGresp; request.send(null); return false; }

//********Image page edit stuff*********

function CheckAlbum1 { if (btnAlb.src == btnAlbImage) { btnAlb.src=btnBusyImage; GetAlbum; } } function CheckArtist { if (btnArt.src == btnArtImage) { btnArt.src=btnBusyImage; GetArtist; } } function GetAlbum { var urlstr = "/api.php?action=query&generator=imageusage&prop=categories&clcategories=Category:Album&format=xml&giutitle=" + encodeURIComponent(wgPageName).replace('.','%2E'); request.open("GET", urlstr, true); request.onreadystatechange = parseAlbum; request.send(null); return false; } function parseAlbum { if (request.readyState == 4) { if (request.status == 200) { var oreg =/:]+?):([^>]+?) \((\d+)\))"><categories/; var m= oreg.exec(request.responseText); if (m) { wpTb1.value=""; } else wpTb1.value=""; btnAlb.src = btnAlbImage; } } } function GetArtist { var urlstr = "http://lyrics.wikia.com/api.php?action=query&generator=imageusage&prop=categories&clcategories=Category:Artist&format=xml&giutitle=" + encodeURIComponent(wgPageName).replace('.','%2E'); request.open("GET", urlstr, true); request.onreadystatechange = parseArtist; request.send(null); return false; } function parseArtist { if (request.readyState == 4) { //alert(request.status); // Found a reason- 403 in IE! (that's definitelly related to '.' in title   if (request.status == 200) {      var oreg =/]+?)"><categories/;      var m =oreg.exec(request.responseText);      var art=(m) ? m[1] : '';      wpTb1.value="";      btnArt.src = btnArtImage;     }  } }

//********Song page view stuff*********

function getLyricsBoxElement { node = document.getElementById("WikiaArticle"); var a = null; var re = new RegExp('\\blyricbox\\b'); var els = node.getElementsByTagName("div"); for(var i=0,j=els.length; i<j; i++) if(re.test(els[i].className)) { a=els[i]; break; }	return a; }

function LoadAudioIfExists{ if (wgCategories){ var cats= wgCategories.toString; if (cats.match(/Spotify\/Song/i)){ var m =window.document.body.innerHTML.match(/"https:\/\/play\.spotify\.com\/track\/(.+?)"/i); if (m) { var gpl = document.createElement("div"); var lb=getLyricsBoxElement; gpl.style.cssText="FLOAT: right; CLEAR: both; margin:0px;height:80px;"; gpl.innerHTML=' '; lb.parentNode.insertBefore(gpl,lb); }		}	} }

function CheckPage { if (wgAction=="edit"){ wpTb1 = window.document.getElementById("wpTextbox1"); wpSumm=document.getElementById("wpSummary"); if (isIE) _timer = setInterval(init, 300); else init; } else if ((wgAction=="view") && (wgNamespaceNumber==0)) LoadAudioIfExists; } var isIE = /*@cc_on!@*/false; //alert(isIE); if (isIE) window.attachEvent('onload', CheckPage); else window.addEventListener('load', CheckPage, false);