Google Search Various Range

Add more time ranges on Google search.

目前為 2017-07-08 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Google Search Various Range
// @namespace   knoa.jp
// @description Add more time ranges on Google search.
// @description Google検索の期間指定の選択肢を増やします。
// @include     https://www.google.*/search?*
// @version     1
// @grant       none
// ==/UserScript==

(function(){
	const SCRIPTNAME = 'GoogleSearchVariousRange';
  console.time(SCRIPTNAME);
  const langs = {
    'en': 0,
    'ja': 1,
  };
  const ranges= {
    qdr_h: {
      h:  ['Past hour',      '1 時間以内'],
      h2: ['Past 2 hours',   '2 時間以内'],
      h12:['Past 12 hours', '12 時間以内'],
    },
    qdr_d: {
      d:  ['Past day',       '1 日以内'],
      d2: ['Past 2 days',    '2 日以内'],
      d3: ['Past 3 days',    '3 日以内'],
    },
    qdr_w: {
      w:  ['Past week',      '1 週間以内'],
      w2: ['Past 2 weeks',   '2 週間以内'],
    },
    qdr_m: {
      m:  ['Past month',     '1 か月以内'],
      m2: ['Past 2 months',  '2 か月以内'],
      m6: ['Past 6 months',  '6 か月以内'],
    },
    qdr_y: {
      y:  ['Past year',      '1 年以内'],
      y2: ['Past 2 years',   '2 年以内'],
      y5: ['Past 5 years',   '5 年以内'],
    },
  };
  window.addEventListener('load', setTimeout.bind(null, function(){
    /* Rebuild Ranges */
    let lang = document.documentElement.lang;
    let lindex = (lang in langs) ? langs[lang] : langs[lang.split('-')[0]] || 0;
    let lis = document.querySelectorAll('#hdtb li[id^="qdr_"]');
    let tpl = document.querySelector('#hdtb li[id^="qdr_"] a[href*="tbs=qdr:"]');
    for(let i=1; lis[i]; i++){
      if(ranges[lis[i].id]){
        lis[i].innerHTML = '';
        for(let range in ranges[lis[i].id]){
          let node = tpl.cloneNode(true);
          node.href = node.href.replace(/(tbs=qdr:)[a-z]/, '$1' + range);
          node.textContent = ranges[lis[i].id][range][lindex];
          lis[i].appendChild(node);
        }
      }else{
        lis[i].style.display = 'none';
      }
    }
    /* Selected Checkmark */
    let sel = document.querySelector('#hdtb li[id^="qdr_"].hdtbSel');
    if(sel && sel.id !== 'qdr_'){
      let qdr = sel.id.split('_')[1];
      let a = document.querySelector('#hdtb li[id^="qdr_"] a[href*="tbs=qdr:' + qdr + '&"]');
      if(a) a.classList.add('hdtbSel');
      sel.classList.remove('hdtbSel');
    }
  }, 100));
  (function(css){
    let head = document.querySelector('head');
    let style = document.createElement('style');
    style.type = 'text/css';
    style.textContent = css;
    head.appendChild(style);
  })(`
    #hdtb li[id^="qdr_"].hdtbItm a{
     display: inline-block !important;
     width: 80px !important;
     padding-right: 20px !important;
    }
  `);
  const log = console.log.bind(null, SCRIPTNAME);
  console.timeEnd(SCRIPTNAME);
})();