User:Dispenser/disambig.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Dispenser/disambig. |
/*
* disambig.js
* Finds pages which are disambiguation pages using the category method
* Does not work well in internet Explorer at the moment
*/
var pages = new Array()
function disambigAjax(){
links = document.getElementById(document.getElementById('content')?'content':'mw_content').getElementsByTagName('a')
for(i=0; (a=links[i]); i++){
// BUG: mw-redirect no present for redirect [[WP:VP]] when going to that page
if(a.href && a.title && a.href.indexOf(wgServer+'/wiki/')==0)
if(pages.join("\n").indexOf('\n'+a.title+'\n')==-1 && !a.title.match("Categor|Special:") && !a.className.match(/extiw|internal|new/)){
pages[pages.length]= a.title
a.style.backgroundColor="#eee"
}
}
disambigRequest(pages, 0)
}
function disambigRequest(pages, idx){
var xmlhttp = sajax_init_object();
// Argument length is the limiting factor. Support for POST requests isn't as complete as GET. So we break up our request.
params = "action=query&format=json&redirects&indexpageids&prop=categories&titles="+pages.slice(idx, idx+100).join("|")
if(xmlhttp){
xmlhttp.open("GET", wgServer+wgScriptPath + "/api.php?" + params, true);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4) {
disambigParse(xmlhttp.responseText);
// Its asynchronous so its possible for two scripts to run at the same time
idx+=100;
if(idx<pages.length)
disambigRequest(pages, idx)
else
document.getElementById('contentSub').appendChild(document.createTextNode('Disambiguation checking done.'))
}
};
xmlhttp.send(null);
}
}
function disambigParse(sText){
if(!sText)return
links = document.getElementById( document.getElementById('content') ? 'content' : 'mw_content' ).getElementsByTagName('a')
r = eval("("+sText+")")
if(r.error){
alert(r.error)
return
}
for(i=0; (pageid=r.query.pageids[i]); i++){
page=r.query.pages[pageid]
redirects = []
if(r.query.redirects)
for(j=0; (redir=r.query.redirects[j]); j++)
if(redir.to==page.title)
redirects[redirects.length] = redir.from
if(page.categories)
for(j=0; (cat=page.categories[j]); j++)
if(cat.title.match("isambiguation|mbiguous")){
page.disambig=true
break;
}
for(j=0; (a=links[j]); j++)
if(a.title==page.title)
a.style.backgroundColor=(page.disambig?"#D8BFD8":"#CFEEB2")
else if(a.className.match("mw-redirect"))
for(k=0; (rt=redirects[k]); k++){
if(a.title==rt){
a.style.backgroundColor=(page.disambig?"#D8BFD8":"#FFFF00")
a.title+='\n redirects to '+page.title
break
}
}
}
}
var dlf=new Function("addPortletLink('p-tb', 'javascript:disambigAjax()', 'Highlight ambiguous links', 't-dablinks');")
if(doneOnloadHook)dlf(); /* if imported dynamically */
else addOnloadHook(dlf);