Thursday, January 26, 2006

Re: Another greasemonkey script....

A few minor changes... Instead of an alert, a message is displayed at the top left of the screen for 5 seconds. In the code, the literal "constants" are moved outside of the for loop. The check for presence of both reference and code is moved inside the loop.


// ==UserScript==
// @name Google Analytics Detector
// @namespace http://codeword.blogspot.com
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==

var URL = "http://www.google-analytics.com/urchin.js";
var TRACKER = "urchinTracker()";

var scripts = document.getElementsByTagName( "script" );

var refPresent = false, codePresent = false, log = false;

for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];

// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;

if ( ref != null ) {
refPresent = ( ref.search( URL ) != -1 );

if( log )
GM_log( "Tested ref: " + ref + " Result: " + refPresent );
}
}

// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;

if ( code != null ) {
codePresent = ( code.search( TRACKER ) != -1 );

if( log )
GM_log( "Tested code: " + code + " Result: " + codePresent );
}
}

if ( refPresent && codePresent ) {
var logo = document.createElement("div");
logo.id = "logo";
logo.innerHTML = '<div style="position: absolute; left: 0px; top: 0px;' +
'border-bottom: 1px solid #000000; margin-bottom: 5px; ' +
'font-size: small; background-color: #000000; z-index: 100;' +
'color: #ffffff; width:200px; opacity: .75;"><p style="margin: 2px 0 1px 0;"> ' +
'<b>Google Analytics enabled</b>' +
'</p></div>';

document.body.insertBefore( logo, document.body.firstChild );

window.setTimeout(
function() {
var logo = document.getElementById( "logo" );
if ( logo ) {
logo.parentNode.removeChild( logo );
}
}
, 5000 );
return;
}
}

3 comments:

Informador said...

Doesnt work for all analytics codes, for example, not with one pointing to:
https://ssl.google-analytics.com/urchin.js

Tomek said...

To force it to work it with HTTPS just create another script (copy-paste) and in the new one which is based on the original script just replace var URL = "http://www.google-analytics.com/urchin.js"; for var URL = "https://ssl.google-analytics.com/urchin.js";

Tested and works great ;)

Tomek said...

Can you please create a script to detect the new google analytics code?

document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); etc etc