Google Search Extra Buttons

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

Verze ze dne 17. 06. 2015. Zobrazit nejnovější verzi.

  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 4.2015.6.17
  7. // @namespace spmbt.github.com
  8. // @include http://www.google.*/*
  9. // @include https://www.google.*/*
  10. // ==/UserScript==
  11.  
  12. (function(lang, yourSite){
  13.  
  14. if(!(yourSite instanceof Array)) yourSite = [yourSite];
  15. var $x = function(el, h){if(h) for(var i in h) el[i] = h[i]; return el;} //===extend===
  16. ,$pd = function(ev){ev.preventDefault();}
  17. ,d = document
  18. ,$e = function(g){ //===создать или использовать имеющийся элемент===
  19. //g={el|clone,IF+ifA,q|[q,el],cl,ht,cs,at,atRemove,on,apT}
  20. if(typeof g.IF =='function')
  21. g.IF = g.IF.apply(g, g.ifA ||[]);
  22. g.el = g.IF && g.IF.attributes && g.IF || g.el || g.clone ||'DIV';
  23. var o = g.o = g.clone && g.clone.cloneNode && g.clone.cloneNode(!0)
  24. || (typeof g.el =='string' ? d.createElement(g.el) : g.el);
  25. if(o && (g.IF===undefined || g.IF || !g.IF && typeof g.IF =='object')
  26. && (!g.q || g.q && (g.dQ = g.q instanceof Array ? dQ(g.q[0], g.q[1]) : dQ(g.q)) ) ){ //выполнять, если существует; g.dQ - результат селектора для функций IF,f
  27. if(g.cl)
  28. o.className = g.cl;
  29. if(g.cs)
  30. $x(o.style, g.cs);
  31. if(g.ht || g.at){
  32. var at = g.at ||{}; if(g.ht) at.innerHTML = g.ht;}
  33. if(at)
  34. for(var i in at){
  35. if(i=='innerHTML') o[i] = at[i];
  36. else o.setAttribute(i, at[i]);}
  37. if(g.atRemove)
  38. for(var i in g.atRemove)
  39. o.removeAttribute(g.atRemove[i]);
  40. if(g.on)
  41. for(var i in g.on) if(g.on[i])
  42. o.addEventListener(i, g.on[i],!1);
  43. g.ap && o.appendChild(g.ap);
  44. g.apT && g.apT.appendChild(o);
  45. }
  46. return o;
  47. },
  48. addRules = function(css){
  49. var heads = d.getElementsByTagName('head')
  50. ,node = d.createElement('style');
  51. heads.length && heads[0].appendChild(node);
  52. node.appendChild(d.createTextNode(css));
  53. };
  54. /**
  55. * check occurrence of third-party event with growing interval
  56. * @constructor
  57. * @param{Number} t start period of check
  58. * @param{Number} i number of checks
  59. * @param{Number} m multiplier of period increment
  60. * @param{Function} check event condition
  61. * @param{Function} occur event handler
  62. */
  63. var Tout = function(h){
  64. var th = this;
  65. (function(){
  66. if((h.dat = h.check() )) //wait of positive result, then occurense
  67. h.occur();
  68. else if(h.i-- >0) //next slower step
  69. th.ww = setTimeout(arguments.callee, (h.t *= h.m) );
  70. })();
  71. },
  72. lNull = lang ==null; //if lang == null|undefined, then no hints
  73. lang = lang =='ru'|| !(lang && lang.length && lang.length >1); //hints in ru or en lang
  74. addRules('.siteList:hover button{display: block}' //TODO no effect
  75. +'.siteList{height:auto; text-align:center}'
  76. +'.gb_Ib >.gb_e{height:47px}.gb_Fb{z-index:1087}.tsf-p{z-index:203}');
  77.  
  78. new Tout({t:120, i:8, m: 1.6
  79. ,check: function(){
  80. return d && d.getElementsByName("q") && d.getElementsByName("q")[0];
  81. },
  82. occur: function(){
  83. var inputSearch = this.dat
  84. ,buttSearch = d.getElementsByName("btnG") && d.getElementsByName("btnG")[0]
  85. ,buttS ={
  86. PDF:['filetype:pdf',lang?'поиск по документам PDF':lNull?'':'search in PDF files']
  87. ,site:['site:'+ yourSite[0],(lang ?'искать по ':lNull?'':'search in ')+ yourSite[0]] //write your site name and uncomment
  88. ,'1D':['&tbs=qdr:d',lang?'за последние сутки':lNull?'':'last day']
  89. ,'7D':['&tbs=qdr:w',lang?'за последнюю неделю':lNull?'':'last week']
  90. ,'1M':['&tbs=qdr:m',lang?'за последний месяц':lNull?'':'last month']
  91. ,'1Y':['&tbs=qdr:y',lang?'за последний год':lNull?'':'last year']
  92. }, j = 0, ww =0
  93. ,buttCss = {opacity: 0.64, lineHeight:'14px', width:'55px', height:'16px', fontSize:'14px', border:'1px solid transparent', backgroundColor:'#4485f5', color:'#fff'};
  94. !yourSite && delete buttS.site;
  95. buttSearch.parentNode.style.position ='relative';
  96. if(buttSearch && top == self)
  97. for(var i in buttS)
  98. var bI = buttS[i]
  99. ,butt2 = $e({clone: i=='site'? $e({cl:'siteList',cs:{cursor:'default'} }) : buttSearch
  100. ,atRemove:['id','name']
  101. ,at:{value: i
  102. ,innerHTML: i
  103. ,title: bI[1]}
  104. ,cs: $x({position:'absolute', top:'33px', left: (-82 + 60*j++) +'px'}, buttCss)
  105. ,on:{click: (function(bI,i){return i=='PDF'|| /^site:/.test(bI[0]) ? function(ev){
  106. inputSearch.value = inputSearch.value.replace(/ site\:[\w.]+$/i,'').replace(' filetype:pdf','') +' '+ (i=='PDF'? bI[0] :'site:'+ ev.target.getAttribute('site'));
  107. if(ev.target.className=='siteList') this.form.click();
  108. }:function(ev){
  109. location.href ='/search?q='+ encodeURIComponent(inputSearch.value) + bI[0];
  110. $pd(ev);
  111. }})(bI,i),
  112. mouseover: i=='site'? function(ev){clearTimeout(ww); ev.target.querySelector('.list').style.display ='block'}:'',
  113. mouseout: i=='site'? function(ev){var t = ev.target; clearTimeout(ww); ww = setTimeout(function(){t.parentNode.parentNode.querySelector('.list').style.display ='none'},450);}:''
  114. }
  115. ,apT: buttSearch.parentNode
  116. });
  117. var siteList = $e({cl:'list',cs:{display:'none'}, apT: buttSearch.parentNode.querySelector('.siteList')});
  118. if(siteList){
  119. for(var i in yourSite) if(i !=0)
  120. var sI = yourSite[i]
  121. ,butt3 = $e({clone:buttSearch
  122. ,atRemove:['id','name']
  123. ,at:{value: sI
  124. ,site: sI
  125. ,title: (lang ?'искать по ':lNull?'':'search in ')+ sI
  126. ,innerHTML: sI}
  127. ,cs: $x($x({display:'block'}, buttCss),{width:'auto', height:'18px', margin:'2px 0 -1px -16px', padding:0, fontWeight:'normal', opacity:1})
  128. ,apT: siteList
  129. });
  130. siteList.style.height ='auto'; siteList.style.textAlign ='center';}
  131. $e({el:d.querySelector('.gb_Ib > .gb_e'),cs:{height:'47px'}} );
  132. $e({el:d.querySelector('.gb_Fb'),cs:{zIndex:'1087'}} );
  133. $e({el:d.querySelector('.tsf-p'),cs:{zIndex:'203'}} );
  134. }
  135. });
  136.  
  137. })('en' //write '' to remove hints; 'en' for English hints, 'ru' for Russian
  138. ,['slashdot.org','engadget.com','techcrunch.com','habrahabr.ru','geektimes.ru','smashingmagazine.com','maketecheasier.com']); //write your favorite sites