// Invisible Web user script
// version 1.2
// 2007-10-16
// Ryan Weir & David Cancel
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Invisible Web", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Invisible Web
// @namespace     invisibleWeb
// @description   Script to show you which invisible/visible trackers are being used on the current page.
// @include       *
// ==/UserScript==

// this retrieves the html source for the current page
var x = document.getElementsByTagName("html");

// parallel arrays which contain the source code to search for, and the string the display once it has been found, and a flag for whether the search string was present
var findCode = new Array();
var codeFound = new Array();
var flagFound = new Array();
var favIco = new Array();

// the code to look for. This will made into a user-modifiable menu
findCode[0] = 'http://pub.lookery.com/js/';
codeFound[0] = 'Lookery';
flagFound[0] = false;
favIco[0] = 'http://lookery.com/favicon.ico';

findCode[1] = 'www.google-analytics.com/urchin.js" type="text/javascript"';
codeFound[1] = 'Google Analytics';
flagFound[1] = false;
favIco[1] = 'http://www.google.com/favicon.ico';

findCode[2] = 'http://pub.mybloglog.com/comm2.php?mblID=';
codeFound[2] = 'MyBlogLog Badge';
flagFound[2] = false;
favIco[2] = 'http://www.mybloglog.com/favicon.ico';

findCode[3] = '.quantserve.com/quant.js">';
codeFound[3] = 'Quantcast';
flagFound[3] = false;
favIco[3] = 'http://www.quantcast.com/favicon.ico';

findCode[4] = 'indextools.js">';
codeFound[4] = 'IndexTools';
flagFound[4] = false;
favIco[4] = 'http://www.indextools.com/favicon.ico';

findCode[5] = '.sitemeter.com/js/counter.js';
codeFound[5] = 'SiteMeter';
flagFound[5] = false;
favIco[5] = 'http://www.sitemeter.com/favicon.ico';

findCode[6] = '/www.lijit.com/informers/wijits';
codeFound[6] = 'Lijit';
flagFound[6] = false;
favIco[6] = 'http://www.lijit.com/favicon.ico';

findCode[7] = '/hbx.js';
codeFound[7] = 'WebsideStory / Visual Sciences';
flagFound[7] = false;
favIco[7] = 'http://davidcancel.com/wp-content/uploads/visualsciences-favicon.ico';

findCode[8] = 'track.mybloglog.com/js/jsserv.php?mblID=';
codeFound[8] = 'MyBlogLog Tracking Script';
flagFound[8] = false;
favIco[8] = 'http://www.mybloglog.com/favicon.ico';

findCode[9] = 'http://cetrk.com/pages/scripts';
codeFound[9] = 'Crazy Egg';
flagFound[9] = false;
favIco[9] = 'http://www.crazyegg.com/favicon.ico';

findCode[10] = 'http://shots.snap.com/snap_shots.js';
codeFound[10] = 'Snap.com';
flagFound[10] = false;
favIco[10] = 'http://www.snap.com/favicon.ico';

findCode[11] = '/s_code.js';
codeFound[11] = 'Omniture';
flagFound[11] = false;
favIco[11] = 'http://www.omniture.com/favicon.ico';

findCode[12] = '/s_code_remote.js';
codeFound[12] = 'Omniture';
flagFound[12] = false;
favIco[12] = 'http://www.omniture.com/favicon.ico';

findCode[13] = 'statcounter.com/counter/counter.js';
codeFound[13] = 'Statcounter';
flagFound[13] = false;
favIco[13] = 'http://www.statcounter.com/favicon.ico';

//this sets the found flag in each element to false initially
for (var i = 0; i < findCode.length; i++) {
	flagFound[i] = false;
}

// this stores the number of search strings that were found in the html source
var numFound = 0;

// stores whether or not any strings were found
var anyFound = false;

// performs the search for the desired search strings
for (var i = 0; i < findCode.length; i++) {
	if (x[0].innerHTML.toUpperCase().indexOf(findCode[i].toUpperCase()) != -1) {
		flagFound[i] = true;
		numFound++;
		anyFound = true;
	}
}

if (anyFound == true) {
	//add this function to allow closing of the div message

	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.innerHTML = '{	var d = document.getElementsByTagName("body"); 		var olddiv = document.getElementById(divNum); 		d.removeChild(olddiv);	}';
	headID.appendChild(newScript);

	// output any of the strings found by adding a div element to this site
	var ni = document.getElementsByTagName("body");
	var newdiv = document.createElement('div');
	var divIdName = 'invisibleWebDivElement';
	newdiv.setAttribute('id',divIdName);
	newdiv.setAttribute('style','visibility:visible;background-color:#DDD;color:#FFFFFF;position:absolute;top:60px;left:20px;padding:20px;');
	for (var i = 0; i < findCode.length; i++) {
		if (flagFound[i] == true) {
			newdiv.innerHTML = newdiv.innerHTML + '<img src="'+favIco[i]+'" title="'+codeFound[i]+'" style="margin-bottom:15px;" /><br />';
		}
	}
	//this line used for debugging
	ni[0].appendChild(newdiv);
	
	// close the div element
	document.addEventListener('click', function(event) {
	    // event.target is the element that was clicked
	    if (event.target == newdiv) {
	    	newdiv = document.getElementById("invisibleWebDivElement");
	    	newdiv.setAttribute('style','visibility:hidden;background-color:#DDD;color:#FFFFFF;position:absolute;top:60px;left:20px;padding:20px; z-index:999;');
	    }
	}, true);
}
