// This is a greasemonkey script, for use with the Firefox extension Greasemonkey.
// More info: http://greasemonkey.mozdev.org/
//
// ==UserScript==
// @name         Google Cache
// @version      1.0.1
// @author       sunwan
// @e-mail       bws@cnnj.8866.org
// @description  Fix google cache to work in china
// @namespace    http://www.cnnj.8866.org/download/greasemonkey/
// @include      http://www.google.com/*
// @include      http://www.google.cn/*
// ==/UserScript==

var fixed = false;

document.addEventListener('click', catchEvent, true);

function catchEvent(e) {
	if ( /\/search\?q=cache:/.test(e.target.href) )	{
		e.preventDefault();					// disable click before fixed
	}
	if ( fixed ) {
		document.removeEventListener('click', catchEvent, true);
	}	
}

function checkPage()	{
    var allLinks = document.links;
    if (allLinks  != null)
      {
         for (i = 0; i <allLinks.length; ++i)
         {
            if (allLinks [i].href.indexOf ("/search?q=cache:") > 0)
            {
                allLinks [i].href = allLinks [i].href.replace ("/search?q=cache:", "/search?&q=cache:");
            }
         }
      }
}

checkPage();