Jump to content

User:Dispenser/disambig.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
 * 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);