Google Search Various Ranges

Add more time ranges on Google search.

当前为 2017-09-28 提交的版本,查看 最新版本

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

(function(){
  const SCRIPTNAME = 'GoogleSearchVariousRanges';
  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 年以内'],
    },
  };
  const PERIODS = {
//    'Before 2000': ['', '12/31/1999'],
//    'After 2000':  ['1/1/2000', ''],
  };
  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*="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(/(qdr:)[a-z][0-9]*/, '$1' + range);
          node.textContent = RANGES[lis[i].id][range][lindex];
          lis[i].appendChild(node);
        }
      }else{
        lis[i].style.display = 'none';
      }
    }
    /* Add Custom Periods */
    let cdr = document.querySelector('#cdrlnk');
    for(let key in PERIODS){
      let node = tpl.cloneNode(true);
      node.href = node.href.replace(/(qdr:)[a-z][0-9]*/, `cdr:1,cd_min:${PERIODS[key][0]},cd_max:${PERIODS[key][1]}`);
      node.textContent = key;
      cdr.parentNode.appendChild(node);
    }
    /* Selected Checkmark */
    let sel = document.querySelector('#hdtb li[id*="dr_"].hdtbSel');
    if(!sel || sel.id === 'qdr_'/*Any time*/) return;
    let a, cdruri = location.href.match(/cdr:1,cd_min:[0-9\/]*,cd_max:[0-9\/]*/);
    if(cdruri){/*has period*/
      a = document.querySelector(`#hdtb li[id^="cdr_"] a[href*="${cdruri[0]}"]`);
    }else{
      let qdr = sel.id.split('_')[1];
      a = document.querySelector(`#hdtb li[id^="qdr_"] a[href*="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,
    #hdtb li[id^="cdr_"].hdtbItm a{
     display: inline-block !important;
     width: 80px !important;
     padding-right: 20px !important;
    }
  `);
  const log = console.log.bind(null, SCRIPTNAME);
  console.timeEnd(SCRIPTNAME);
})();