Google China
By Drew
—
Last update Jun 26, 2008
—
Installed
674 times.
// ==UserScript==
// @name Google China
// @namespace
// @description When the user queries Google, the script also queries Google China. The script inserts the number of results reported by Google China following the number of results reported by Google. Click the number of results reported by Google China to show the first Google China result.
// @include http://www.google.*/search*
// ==/UserScript==
var $A = Array.from = function(iterable) {
if (!iterable) return [];
if (iterable.toArray) {
return iterable.toArray();
}
else {
var results = [];
for (var i = 0; i < iterable.length; i++) {
results.push(iterable[i]);
}
return results;
}
}
Function.prototype.bind = function() {
var __method = this, args = $A(arguments), object = args.shift();
return function() {
return __method.apply(object, args.concat($A(arguments)));
}
}
function hasNode(xpath, contextDocument) {
if(contextDocument) {
return contextDocument.evaluate('count(' + xpath + ')', contextDocument, null, XPathResult.NUMBER_TYPE, null).numberValue != 0;
}
else {
return document.evaluate('count(' + xpath + ')', document, null, XPathResult.NUMBER_TYPE, null).numberValue != 0;
}
}
function stringFor(xpath, contextDocument) {
if(contextDocument) {
return contextDocument.evaluate(xpath, contextDocument, null, XPathResult.STRING_TYPE, null).stringValue;
}
else {
return document.evaluate(xpath, document, null, XPathResult.STRING_TYPE, null).stringValue;
}
}
function nodeFor(xpath, contextDocument) {
if(contextDocument) {
return contextDocument.evaluate(xpath, contextDocument, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
else {
return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
}
function nodeIteratorFor(xpath, contextDocument) {
if(contextDocument) {
return contextDocument.evaluate(xpath, contextDocument, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
}
else {
return document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
}
}
function nodeSnapshotFor(xpath, contextDocument) {
if(contextDocument) {
return contextDocument.evaluate(xpath, contextDocument, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}
else {
return document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}
}
function GreaseFrame() {
}
GreaseFrame.prototype.statusUnknown = 0;
GreaseFrame.prototype.statusPresent = 1;
GreaseFrame.prototype.statusAbsent = 2;
GreaseFrame.prototype.statusError = 3;
GreaseFrame.prototype.status = GreaseFrame.prototype.statusUnknown;
GreaseFrame.prototype.request = function(url, callback) {
this.callback = callback;
GM_xmlhttpRequest({
url: url,
method: 'GET',
onload: this.load.bind(this),
onerror: this.error.bind(this)
});
}
GreaseFrame.prototype.load = function(response) {
var iframe;
iframe = document.createElement('iframe');
iframe.style.position = 'absolute';
iframe.style.visibility = 'hidden';
iframe.style.width = '95%';
iframe.style.height = '0px';
if(this.iframe) {
document.body.replaceChild(iframe, this.iframe);
}
else {
document.body.appendChild(iframe);
}
this.iframe = iframe;
with(iframe) {
contentWindow.addEventListener('load', this.post.bind(this, iframe.contentDocument), false);
contentWindow.location.href = location.href;
contentDocument.open("text/html");
contentDocument.write(response.responseText.replace(/<script(.|\s)*?>(.|\s)*?<\/script>/gmi, ''));
contentDocument.close();
}
}
GreaseFrame.prototype.post = function(document) {
this.callback(this.parse(document));
}
GreaseFrame.prototype.error = function(response) {
this.status = this.statusError;
}
function GoogleChina() {
}
GoogleChina.prototype = new GreaseFrame();
GoogleChina.prototype.xpath = new Object();
GoogleChina.prototype.xpath['result'] = '/HTML[1]/BODY[1]/TABLE[2]/TBODY[1]/TR[1]/TD[2]/FONT[1]/B[1]/text()[1]';
GoogleChina.prototype.location = function() {
return location.protocol + '//www.google.cn' +
location.pathname +
location.search.replace(/([?&])(hl=[^&]*)/gi, '$1hl=zh-cn');
}
GoogleChina.prototype.get = function(key, document) {
return stringFor(this.xpath[key], document);
}
GoogleChina.prototype.build = function(callback) {
this.request(this.location(), callback);
}
GoogleChina.prototype.parse = function(document) {
this.result = this.get('result', document);
return this;
}
Google.prototype.xpath = new Object();
Google.prototype.xpath['context'] = '/HTML[1]/BODY[1]/TABLE[2]/TBODY[1]/TR[1]/TD[2]/FONT[1]';
function Google() {
if(window == window.top) {
addEventListener('load', this.initialize.bind(this), false);
}
}
Google.prototype.get = function(key) {
return stringFor(this.xpath[key]).replace(/^\s+|\s+$/g,"");
}
Google.prototype.nodeFor = function(key) {
return nodeFor(this.xpath[key]);
}
Google.prototype.initialize = function() {
new GoogleChina().build(this.render.bind(this));
}
Google.prototype.render = function(googleChina) {
var contextNode = this.nodeFor('context');
contextNode.innerHTML = contextNode.innerHTML.replace(/\(.*\)/g, '(About <b>' + googleChina.result + '</b> in Google China.)');
}
new Google();