// ==UserScript==
// @name Google限制时间搜索
// @namespace Yamamaya
// @description You can select time option and language.
// @include http://www.google.*/
// @include http://www.google.*/search*
// @include http://www.google.*/webhp?*
// @version 1.07
// ==/UserScript==

(function(){
var xpath = function(query){
return document.evaluate(
query,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null);
};

var xpathSingle = function(query){
return document.evaluate(
query,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null).singleNodeValue;
};

var GoogleTimeLanguage = (function (){
var allItem = xpath('.//input[@name="btnG"]');
for (var i = 0; i < allItem.snapshotLength; i++) {
thisItem = allItem.snapshotItem(i);

var time = document.createElement("select");
time.setAttribute("name","as_qdr");
time.setAttribute("style","margin: 0px 5px 0px 3px");
time.innerHTML = "<option selected='' value='all'>时间范围</option>"
+ "<option value='d'>24小时以内</option>"
+ "<option value='w'>一周以内</option><option value='m'>一月以内</option>"
+ "<option value='y'>一年以内</option>";
thisItem.parentNode.insertBefore(time,thisItem);

var lang = document.createElement("select");
lang.setAttribute("style","margin: 0px 5px 0px 2px");
lang.innerHTML = "<option>语言选择</option>"
+ "<div><input type='radio' id='lr' value='lang_zh-CN' name='lr'/><label for='lr'>简体中文</label></div>"
// + "<div><input type='radio' id='lr' value='lang_zh-TW' name='lr'/><label for='lr'>繁体中文</label></div>"
// + "<div><input type='radio' id='lr' value='lang_en' name='lr'/><label for='lr'>英语</label></div>"
// + "<div><input type='radio' id='lr' value='lang_es' name='lr'/><label for='lr'>西班牙语</label></div>"
// + "<div><input type='radio' id='lr' value='lang_fr' name='lr'/><label for='lr'>法语</label></div>"
// + "<div><input type='radio' id='lr' value='lang_it' name='lr'/><label for='lr'>意大利语</label></div>"
// + "<div><input type='radio' id='lr' value='lang_de' name='lr'/><label for='lr'>德语</label></div>"
// + "<div><input type='radio' id='lr' value='lang_ko' name='lr'/><label for='lr'>朝鲜语</label></div>"
// + "<div><input type='radio' id='lr' value='lang_ja' name='lr'/><label for='lr'>日语</label></div>"
+ "<div><input type='radio' value='' name='lr' id='all'/><label for='all'>所有语言</label></div>";
thisItem.parentNode.insertBefore(lang,thisItem);
}
})();

var removeElements = (function (){
var allItem = xpath('//select[@name="as_qdr"][2]');
for (var i = 0; i < allItem.snapshotLength; i++) {
thisItem = allItem.snapshotItem(i);
thisItem.parentNode.removeChild(thisItem);
}
})();

var Gsubmit = function(){
var gform = xpathSingle(".//form[@name='f'] | .//form[@name='gs']")
gform.submit();
};

var GoogleOnClick = (function (){
var path = ['//node()[@value="all"]',
'//node()[@value="d"]',
'//node()[@value="w"]',
'//node()[@value="m"]',
'//node()[@value="y"]',
'//node()[@name="lr"]',
].join(" | ");
var allItem = xpath(path);
for (var i = 0; i < allItem.snapshotLength; i++) {
thisItem = allItem.snapshotItem(i);
thisItem.addEventListener('click', Gsubmit, false);
}
})();

})();