Google Search Various Ranges

Add more time ranges on Google search.

Fra 15.07.2017. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Google Search Various Ranges
// @namespace   knoa.jp
// @description:en Add more time ranges on Google search.
// @description:ja Google検索の期間指定の選択肢を増やします。
// @include     https://www.google.*/search?*
// @version     1
// @grant       none
// @description Add more time ranges on Google search.
// ==/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 年以内'],
    },
  };
  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);
})();