User:Anakin101/qualityanalysis.js

/* Note: This code is beta/experimental! */

if (wgNamespaceNumber == 0) { var button = document.createElement('button'); button.innerHTML = 'Citations'; button.style.background = '#fe8'; button.style.borderColor = '#fe8'; button.style.color = '#000'; button.style.fontSize = '8pt'; button.style.cssFloat = 'right'; button.style.margin = '1px'; button.style.marginRight = '75px'; button.addEventListener('click', function {		this.set = !this.set;		if (this.set) {			button.style.borderStyle = 'inset';			analyseRefs(getArticleBody);		} else {			button.style.borderStyle = 'outset';			resetColoring(getArticleBody);		}	}, false); var h1 = document.getElementsByTagName('h1')[0]; h1.insertBefore(button, h1.firstChild); }

function getArticleBody { var a = document.getElementById('bodyContent'); // monobook, others if (!a) a = document.getElementById('mw_contentholder'); // modern if (!a) a = document.getElementById('article'); // classic, others if (!a) alert('Cannot find article content. You may need to switch to Monobook or a standard skin in your preferences.'); return a; }

function analyseRefs(body) { var value = 0; for (var i = body.childNodes.length; i--;) { var p = body.childNodes[i]; if (p.tagName == 'UL' || p.tagName == 'OL') { if (p.className == 'references') continue; // refs var litems = p.getElementsByTagName('li'); // lists for (var j = litems.length; j--;) { value = analyseRefs2(litems[j], value); value *= 0.35; }			continue; }		if (p.tagName != 'P') continue; if (p.childNodes.length == 1 && p.firstChild.tagName == 'A') continue; // heading anchor if (p.childNodes.length == 1 && p.firstChild.tagName == 'BR') continue; if (p.className == 'catlinks') continue; // cats value *= 0.35; value = analyseRefs2(p, value); } }

function resetColoring(body) { var all = body.getElementsByTagName('*'); for (var i = all.length; i--;) { var e = all[i]; if (e.getAttribute('origBorder') !== null) e.style.border = e.getAttribute('origBorder'); if (e.getAttribute('origBackground') !== null) e.style.background = e.getAttribute('origBackground'); } }

function analyseRefs2(p, value) { p.setAttribute('origBorder', p.style.border); p.style.border = "1px solid #900"; for (var i = p.childNodes.length; i--;) { var node = p.childNodes[i]; if (node.className == 'reference') { value += getReferenceValue(node); } else if (String(node.className).match(/Template-Fact/)) { value -= 20; if (value < 0) value = 0; } else if (node.nodeType == 3) { value -= 0.15 * node.length; if (value < 0) value = 0; var span = document.createElement('span'); span.style.backgroundColor = getColorForRefValue(value); span.innerHTML = node.nodeValue; span.setAttribute('origBackground', 'none'); p.replaceChild(span, node); } else { value -= 0.15 * node.textContent.length; if (value < 0) value = 0; if (!node.getAttribute('origBackground')) node.setAttribute('origBackground', node.style.background); node.style.backgroundColor = getColorForRefValue(value); }	}	return value; }

function getColorForRefValue(value) { if (value > 100) return '#ffffff'; var g = Math.round(value / 100 * 10 + 245); var b = Math.round(value / 100 * 255); if (g > 255) g = 255; g = g.toString(16); if (g.length == 1) g = '0' + g;	if (b > 255) b = 255; b = b.toString(16); if (b.length == 1) b = '0' + b;	return '#ff' + g + b; }

function getReferenceValue(node) { if (node.tagName == 'SUP' && (matches = node.id.match(/^_ref-(.*?)_?(\d+)$/))) { var fn = document.getElementById('_note-' + (matches[1].length ? matches[1] : matches[2])); if (!fn) return 45; var x = 85; var uses = 0; var docloc = document.location.href; var fnc = fn.getElementsByTagName('*'); for (var i = fnc.length; i--;) { var cn = fnc[i]; if (cn.tagName == 'A' && cn.href.substr(0, docloc.length + 6) == docloc + '#_ref-') { uses++; continue; }			x += 3; }		if (uses > 3) x = Math.round(x / Math.sqrt(uses - 2)); return x;	} else { //return node.innerHTML; return 50; } }