// ==UserScript==
// @name Google Rankage BETA
// @author mitzevo <http://mitzevo.dcmembers.com/>
// @description Google Rankage, a GM script for adding numbers next to google search results, making it easy to see their positions (saves counting ;)).
// @include http://google.*/*
// @include http://www.google.*/*
// ==/UserScript==
// todo: filter out news and shopping results on first page.. making the first page appear to have 11 or 12 results..
// todo: improve chinese listings beyond the 100.. probably improve/optimize/compact the code at a later stage or sum thin..
// todo: on google.cn some pages are not showing the numbers, have to debug that.
// todo: when logged into google account, results don't show numbers.. debug.
(function() {
var lang; //Display numbers in English or Chinese ('en' or 'zh')?:
lang = 'en';
var langhash = new Array(); //unicodes for english and chinese numbers
langhash['en'] = new Array(); langhash['zh'] = new Array();
langhash['en'][0] = "\u0030"; langhash['zh'][0] = "\uLING";
langhash['en'][1] = "\u0031"; langhash['zh'][1] = "\u4e00";
langhash['en'][2] = "\u0032"; langhash['zh'][2] = "\u4e8c";
langhash['en'][3] = "\u0033"; langhash['zh'][3] = "\u4e09";
langhash['en'][4] = "\u0034"; langhash['zh'][4] = "\u56db";
langhash['en'][5] = "\u0035"; langhash['zh'][5] = "\u4e94";
langhash['en'][6] = "\u0036"; langhash['zh'][6] = "\u516d";
langhash['en'][7] = "\u0037"; langhash['zh'][7] = "\u4e03";
langhash['en'][8] = "\u0038"; langhash['zh'][8] = "\u516b";
langhash['en'][9] = "\u0039"; langhash['zh'][9] = "\u4e5d";
langhash['en'][10] = "\u0039"; langhash['zh'][10] = "\u5341";
//langhash['en'][11] = "\u0031\u0030"; langhash['zh'][11] = "\u5341\u4e00"; //.. we will generate the sequence later instead of hardcoding.
var start = window.location.href.match("start=([0-9]+)"); //get the start param value from the loaded/loading/gs'd page..
start = (start == null)? 0 : start[1]; //alert("start = "+start); // ^if it doesn't exist, it's a fresh page/first page listing.
var page = start/10 + 1; //page = (pos == 0)? 1 : (pos/10 + 1); alert("page = "+page);
var pos = start; //pos = start;
//find out which <ol> contains the organic results by matching the footprint.. (easily found when comparing the html source of a few G queries)
var ol = document.getElementsByTagName("ol")[0]; // let's try the first one..
ol = (ol.innerHTML.substr(0,8) == '<!--m-->') ? ol : document.getElementsByTagName("ol")[1]; //does the first match the footprint? if not, it's the second one we're after
ol = (ol.innerHTML.substr(0,8) == '<!--m-->') ? ol : document.getElementsByTagName("ol")[2]; //does the second match the footprint? if not, it's the thirc one we're after
ol = (ol.innerHTML.substr(0,8) == '<!--m-->') ? ol : document.getElementsByTagName("ol")[3]; //does the third match the footprint? if not, it's the fourth one we're after
var lis = ol.getElementsByTagName('li');
for(var i = 0, li; li = lis[i]; i++){
if(li.className=='g'){
var ones, tens, position;
pos++;
if(pos < 10){
position = langhash[lang][pos];
}else if(pos < 100){
position = String(pos);
tens = langhash[lang][position.substr(0,1)]; ones = langhash[lang][position.substr(1,1)];
if(lang == 'en'){
position = tens+ones
}else{
if(pos > 9 && pos < 20)
position = (pos != 10)? langhash['zh'][10]+ones : langhash['zh'][10];
else if(pos > 19 && pos < 100)
position = (pos != 20 && pos != 30 && pos != 40 && pos != 50 && pos != 60 && pos != 70 && pos != 80 && pos != 90)? tens+langhash['zh'][10]+ones : tens+langhash['zh'][10];
else
position = tens+langhash['zh'][10]+ones;
}
}
li.innerHTML = '<span style="color: black; background-color: yellow; font-family: tahoma; font-size: 11px; font-weight: bold; border: none; padding: 1px 3px 2px 3px;"><a title=\''+position+'\'>'+position+'</a></span> '+li.innerHTML;
}
}
})();