Google Search Extra Buttons

Add buttons (last day, last week, PDF search etc.) to results of search page of Google

当前为 2015-01-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google Search Extra Buttons
  3. // @name:ru GoogleSearchExtraButtons
  4. // @description Add buttons (last day, last week, PDF search etc.) to results of search page of Google
  5. // @description:ru Добавляет 5 кнопок вариантов поиска рядом с кнопкой поиска Google на странице результатов
  6. // @version 1.2.1
  7. // @namespace spmbt.github.com
  8. // @include http://www.google.*/*
  9. // @include https://www.google.*/*
  10. // ==/UserScript==
  11.  
  12. (function(lang){
  13.  
  14. var $x = function(el, h){if(h) for(var i in h) el[i] = h[i]; return el;} //===extend===
  15. ,$pd = function(ev){ev.preventDefault();}
  16. ,$e = function(g){ //===создать или использовать имеющийся элемент===
  17. //g={el|clone,IF+ifA,q|[q,el],cl,ht,cs,at,atRemove,on,apT}
  18. if(typeof g.IF =='function')
  19. g.IF = g.IF.apply(g, g.ifA ||[]);
  20. g.el = g.IF && g.IF.attributes && g.IF || g.el || g.clone ||'DIV';
  21. var o = g.o = g.clone && g.clone.cloneNode(!0)
  22. || (typeof g.el =='string' ? document.createElement(g.el) : g.el);
  23. if(o && (g.IF===undefined || g.IF || !g.IF && typeof g.IF =='object')
  24. && (!g.q || g.q && (g.dQ = g.q instanceof Array ? dQ(g.q[0], g.q[1]) : dQ(g.q)) ) ){ //выполнять, если существует; g.dQ - результат селектора для функций IF,f
  25. if(g.cl)
  26. o.className = g.cl;
  27. if(g.cs)
  28. $x(o.style, g.cs);
  29. if(g.ht || g.at){
  30. var at = g.at ||{}; if(g.ht) at.innerHTML = g.ht;}
  31. if(at)
  32. for(var i in at){
  33. if(i=='innerHTML') o[i] = at[i];
  34. else o.setAttribute(i, at[i]);}
  35. if(g.atRemove)
  36. for(var i in g.atRemove)
  37. o.removeAttribute(g.atRemove[i]);
  38. if(g.on)
  39. for(var i in g.on) if(g.on[i])
  40. o.addEventListener(i, g.on[i],!1);
  41. g.apT && g.apT.appendChild(o); //ставится по ориентации, если новый
  42. }
  43. return o;
  44. };
  45. /**
  46. * check occurrence of third-party event with growing interval
  47. * @constructor
  48. * @param{Number} t start period of check
  49. * @param{Number} i number of checks
  50. * @param{Number} m multiplier of period increment
  51. * @param{Function} check event condition
  52. * @param{Function} occur event handler
  53. */
  54. var Tout = function(h){
  55. var th = this;
  56. (function(){
  57. if((h.dat = h.check() )) //wait of positive result, then occurense
  58. h.occur();
  59. else if(h.i-- >0) //next slower step
  60. th.ww = setTimeout(arguments.callee, (h.t *= h.m) );
  61. })();
  62. },
  63. lNull = lang ==null, //если lang == null или undefined - no hints
  64. lang = lang =='ru'|| !(lang && lang.length && lang.length >1); //hints in ru or en lang
  65. new Tout({t:120, i:8, m: 1.6
  66. ,check: function(){
  67. return document && document.getElementsByName("q") && document.getElementsByName("q")[0];
  68. }
  69. ,occur: function(){
  70. var inputSearch = this.dat
  71. ,buttSearch = document.getElementsByName("btnG") && document.getElementsByName("btnG")[0]
  72. ,buttS =[
  73. ['PDF','filetype:pdf',lang?'поиск по документам PDF':lNull?'':'search in PDF files']
  74. //,['S','site:yoursite.com',lang?'write:yoursite.com':lNull?'':'search in yoursite.com'] //write your site name and uncomment
  75. ,['1D','&tbs=qdr:d',lang?'за последние сутки':lNull?'':'last day']
  76. ,['7D','&tbs=qdr:w',lang?'за последнюю неделю':lNull?'':'last week']
  77. ,['1M','&tbs=qdr:m',lang?'за последний месяц':lNull?'':'last month']
  78. ,['1Y','&tbs=qdr:y',lang?'за последний год':lNull?'':'last year']
  79. ], j =0;
  80. buttSearch.parentNode.style.position ='relative';
  81. if(buttSearch && top == self)
  82. for(var i in buttS)
  83. var bI = buttS[i]
  84. ,butt2 = $e({clone: buttSearch
  85. ,atRemove:['id','name']
  86. ,at:{value: bI[0]
  87. ,innerHTML: bI[0]
  88. ,title: bI[2]}
  89. ,cs:{position:'absolute', top:'33px', left: (-82 + 60*j++) +'px', opacity: 0.64, lineHeight:'14px', height:'16px'
  90. ,border:'1px solid rgba(0, 0, 0, 0)', backgroundColor:'#4485f5', color:'#fff'}
  91. ,on:{click: (function(bI){return bI[0] =='PDF'|| /^site:/.test(bI[1]) ? function(ev){
  92. inputSearch.value +=' '+ bI[1];
  93. }:function(ev){
  94. location.href ='/search?q='+ encodeURIComponent(inputSearch.value) + bI[1];
  95. $pd(ev);
  96. }})(bI) }
  97. ,apT: buttSearch.parentNode
  98. });
  99. $e({el:document.querySelector('.gb_Ib > .gb_e'),cs:{height:'47px'}} );
  100. }
  101. });
  102.  
  103. })('en'); //remove for no hints; write 'en' for English hints, 'ru' for Russian
  104.