User:Dispenser/Link checker/json.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
//<source lang="JavaScript">
/** JSON checklinks ************************************************************
 *  Description: Query the Toolserver to preform the link test on a given page
 *               and returns the results to be display at the top of the page.
 *  License:     Public domain
 *
 *  Installation: Copy the lines bellow into your monobook.js
 *   importScript('User:Dispenser/Link_checker/json.js'); // [[User:Dispenser/Link_checker/json.js]]
 * 
 *  Functions: 
 *   checklinksRequest(pagename, callback): Setup JSON query
 *   checklinksDone(checklinksObject): Print table at the top of the page
 *  Configuration:
 *   threshold:  The cutoff score for links to be displayed in the table
 *   checklinksColor: An array of color for each rank [0-6]
 */
var checklinksColor = ['#f7f7f7', '#CFEEB2', '#EEEEB2', '#B2CFEE', '#EED0B2', '#EEB2B2', '#9596E9'];
function checklinksRequest(title, callback){
	document.getElementById('contentSub').innerHTML += '<br />Querying Toolserver <img src="/skins-1.5/common/images/spinner.gif" />';
	var scriptElem = document.createElement('script');
	scriptElem.setAttribute('src', 'http://tools.wikimedia.de/~dispenser/cgi-bin/jsonchecklinks.py?callback=' + callback + '&page=' + title);
	scriptElem.setAttribute('type', 'text/javascript');
	document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
function checklinksDone(checklinks){
	if(!threshold)var threshold=0;
	linktable='<table class="wikitable" id="linktable"><tr><th></th><th>Link</th><th>Analysis</th></tr>';
	alinks = document.getElementsByTagName("a");
	for(var i=1; (link=checklinks.links[i-1]); i++) {
		if(link.rank >= threshold) {
			linktable += '<tr style="background:'+checklinksColor[link.rank]+';" class="dead-'+link.rank+'">'
			            +'  <td><a href="#weblink'+i+'">'+i+'</a></td>'
						+'  <td><a href="'+link.url+'" class="external">'+(link.title||link.url)+'</a></td>'
						+'  <td>'+(link.comment||link.reason)+'</td>'
						+'</tr>\n';
		}
		for(var j=0; (a=alinks[j]); j++){
			// a.title instead of a.href to avoid browser link normalization
			if(a.title == link.url && !a.id){
				a.style.backgroundColor=checklinksColor[link.rank];
				a.id="weblink"+i;
				break;
			}
		}
	}
	linktable += '</table>';
	document.getElementById('contentSub').innerHTML = linktable;
	if(ts_makeSortable)
		ts_makeSortable(document.getElementById('linktable'))
}
if(wgIsArticle) {
addOnloadHook(new Function("addPortletLink('p-tb', 'javascript:checklinksRequest(wgContentLanguage+\":\"+wgPageName, \"checklinksDone\");', 'External links', 't-checklinks', 'Check status of external links', '!');"));
}
// </source>