// SEO Info 1.1
// Written by Mathieu Kooiman < mathieu@scriptorama.nl >
// http://www.scriptorama.nl/
//
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// ==UserScript==
// @name          SEO Information 1.1
// @namespace     http://scriptorama.nl/software/
// @description   Shows the pages Title, Meta Description, Meta Keywords and H1 tags. Also counts the words available on the page.
// @include       *
// ==/UserScript==

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function sorGetH1Tags()
{
	var headings = document.getElementsByTagName('h1');
	if (!headings.length)
	{
		return "No H1 tags found";
	}
	
	var result = '<ul style="text-align: left; margin-top: 0;">';

	for (var x = 0; x <  headings.length; x++ ) {
		result += "<li>" + headings[x].innerHTML + "</li>";
	}
	
	result += "</ul>";
	
	return result;
}

function sorCountWords()
{
	var xpathResult = document.evaluate( 
		"//.[local-name() != 'SCRIPT' and local-name() != 'STYLE' and local-name() != 'TITLE']/text()", 
		document.body, 
		null, 
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
		null 
	);
	
	var wordCount = 0;
	var words;
	
	for (var i = 0; i < xpathResult.snapshotLength; i++)
	{
		var textNode = trim(xpathResult.snapshotItem(i).nodeValue);
		
		if (textNode.length > 0)
		{
			words = textNode.split(/\s/);

			/* Only count elements that have 2 or more "word characters" */
			for (var z in words)
			{
				if (words[z].match(/\w{2,}/))
					wordCount++;	
			}
		}
	}

	return wordCount;
}

function sorGetMetaTag(tag)
{
	var metaTags = document.getElementsByTagName('meta');

	for (var j in metaTags)
	{
		if (metaTags[j].name == tag)
			return metaTags[j].content;
	}
	
	return "Meta tag '" + tag + "' not available";
}

function sorGetTitle()
{
	return document.title ? document.title : "No title specified";
}

function sorAddRow(title, value)
{
	return '<tr><td valign="top" style="font-weight: bold; text-align: left;">' + title + '</td><td style="text-align: left;">' + value + '</td></tr>';
}

function getBackground()
{
	/* php -r "echo base64_encode( file_get_contents( '/Users/michiel/px.png' ) );" > bla.txt */
	return 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAPSURBVHjaYrZoPDoTIMAABJoCG7wbAQ4AAAAASUVORK5CYII=';	
}

function sorSeoCheck(e)
{
	e.preventDefault();

	var div = document.getElementById('sor-seo-div');
	
	if (div)
	{
		document.body.removeChild(div);
		return false;
	}
	
	var div = document.createElement('div');
	div.id = "sor-seo-overlay";
	div.setAttribute(
		'style',
		'background: url(data:image/png;base64,' + getBackground() + ');' +
		'position: absolute; left: 0; top: 0; width: 100%; height: 100%; z-index: 99998;'
	);
		
	document.body.insertBefore(div, document.body.firstChild);
	
	var seoDiv = document.createElement('div');
	seoDiv.id = 'sor-seo-div';
	
	seoDiv.setAttribute (
		'style',
		'position: absolute; border: 2px solid black; padding: 15px; background-color: #fff; left: 25%; top: 10%; z-index: 99999; width: 500px;'
	);
	
	var HTML = '<h1 style="font-size: 15pt; margin-top: 0;">SEO info</h1>';
	
	HTML += 
		'<table border="1">' +
		sorAddRow('Title:', sorGetTitle() ) +
		sorAddRow('Meta Description', sorGetMetaTag('description')) +
		sorAddRow('Meta Keywords', sorGetMetaTag('keywords')) +
		sorAddRow('H1 elements:', sorGetH1Tags()) +
		sorAddRow('Number of words on page:', sorCountWords()) +
		'</table>';
		
	HTML += '<small>This greasemonkey user script was brought to you by: <a href="http://www.scriptorama.nl">Scriptorama.nl</a></small>';
	
	var a = document.createElement('a');
	a.href="#";
	a.addEventListener(
		'click', 
		function() { 
			var d1 = document.getElementById('sor-seo-overlay');
			var d2 = document.getElementById('sor-seo-div');
			
			document.body.removeChild(d1);
			document.body.removeChild(d2);
		}, true 
	);
	a.appendChild(document.createTextNode('Close'));
	a.style.display = "block";
	a.style.textAlign = "center";
	seoDiv.innerHTML = HTML;
	seoDiv.appendChild(document.createElement('p').appendChild(a) );
		
	document.body.insertBefore(seoDiv, document.body.firstChild);
	return false;
}
var a = document.createElement('a');
a.addEventListener('click', sorSeoCheck, true);
a.style.color = "#fff";
a.href = "#";
a.appendChild ( document.createTextNode('SEO'));

var seoNav = document.createElement('div');
seoNav.id = 'sor-seo-nav';
seoNav.setAttribute (
	'style',
	'position: absolute; top: 0px; right: 0px; background-color: #000; color: #fff; width: 50px;'
);

window.setTimeout(
	function() { document.body.removeChild(document.getElementById('sor-seo-nav')); },
	10000
);

seoNav.appendChild ( a )
document.body.insertBefore(seoNav, document.body.firstChild);
