Greasemonkey/Tampermonkey: Add the date-range selector on Google Search.
// ==UserScript== | |
// @name Google Search Date Limiter | |
// @namespace http://0-oo.net/ | |
// @description Add the date-range selector on Google Search. | |
// @homepage http://0-oo.net/log/category/javascript/google-search-date-limiter/ | |
// @version 0.9.0 | |
// @include http*://www.google.tld/search* | |
// @include http*://www.google.tld/webhp* | |
// @include http*://www.google.tld/imghp | |
// @include http*://www.google.tld/ig* | |
// @include http*://www.google.tld/#* | |
// @include http*://www.google.tld/ | |
// @grant none | |
// ==/UserScript== | |
// | |
// ( The MIT License ) | |
// | |
setInterval(function() { | |
if (document.getElementsByTagName("SELECT").length) { //消えたらまた追加する | |
return; | |
} | |
var btn = document.getElementsByName("btnG")[0]; | |
if (!btn) { | |
return; | |
} | |
var btnDiv = document.createElement("div"); | |
btnDiv.style.marginTop = "-5px"; | |
btnDiv.style.textAlign = "right"; | |
document.getElementById("tsf").appendChild(btnDiv); // 検索フォームに追加 | |
//(あれば)指定された期間 | |
var selected = decodeURIComponent(location.search).match(/tbs=qdr:([a-z])([0-9]+)/) || []; | |
//数字のselect | |
var selNum = btnDiv.appendChild(document.createElement("select")); | |
selNum.appendChild(document.createElement("option")); | |
var opt = null; | |
for (var i = 1; i < 60;) { | |
opt = document.createElement("option"); | |
opt.appendChild(document.createTextNode(i)); | |
if (i == selected[2]) { | |
opt.selected = true; | |
} | |
selNum.appendChild(opt); | |
if (i < 10) { | |
i++; | |
} else { | |
i += 10; //10以上は10ずつ増加 | |
} | |
} | |
//単位のselect | |
var selUnit = btnDiv.appendChild(document.createElement("select")); | |
var units = {}, suffix = ""; | |
if (navigator.language == "ja") { | |
units = { s: "秒", n: "分", h: "時間", d: "日", w: "週間", m: "ヶ月", y: "年" }; | |
suffix = " 以内"; | |
} else { | |
units = { s: "second", n: "minute", h: "hour", d: "day", w: "week", m: "month", y: "year" }; | |
suffix = "(s)"; | |
} | |
for (var val in units) { | |
opt = document.createElement("option"); | |
opt.setAttribute("value", val); | |
opt.appendChild(document.createTextNode(units[val] + suffix)); | |
if (val == selected[1]) { | |
opt.selected = true; | |
} | |
selUnit.appendChild(opt); | |
} | |
if (!selected[1]) { | |
selUnit.selectedIndex = 5; //デフォルトは"月" | |
} | |
btn.addEventListener("click", function() { | |
var url = "/search?q="; | |
//検索ワード | |
url += encodeURIComponent(document.getElementsByName("q")[0].value); | |
if (selNum.selectedIndex) { | |
//期間 | |
var qdr = "qdr:" + selUnit.options[selUnit.selectedIndex].value; | |
qdr += selNum.options[selNum.selectedIndex].text; | |
url += "&tbs=" + qdr; | |
} | |
//(あれば)検索種類(画像検索等) | |
url += location.href.match(/&tbm=[a-z]+/) || ""; | |
//(あれば)表示言語 | |
url += location.href.match(/&hl=[-a-z]+/i) || ""; | |
//(あれば)言語での絞り込み | |
url += location.href.match(/&lr=lang_[-a-z]+/i) || ""; | |
// 処理が取られないように非同期で実行 | |
setTimeout(function(){ location.href = url; }, 1); | |
}, true); | |
}, 1); //レンダリング後のタイミングを見計らう |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment