MDN Search Filtering

Restores search topic filtering to MDN; for much better search results!

  1. // ==UserScript==
  2. // @name MDN Search Filtering
  3. // @namespace Violentmonkey Scripts
  4. // @match https://developer.mozilla.org/*
  5. // @grant none
  6. // @version 1.5
  7. // @author John Bussjaeger
  8. // @description Restores search topic filtering to MDN; for much better search results!
  9. // @homepageURL https://github.com/bussdriver/MDN-enhancer
  10. // ==/UserScript==
  11.  
  12. var filters= ["all","api","addons","css","canvas","firefox","firefox-os","games","html","http","js","marketplace","mathml","mobile","apps","svg","webdev","standards","webext","webgl","docs","xpcom","xul"];
  13. var localized= {//map to filters + url + url_title
  14. 'en':["All"//special case
  15. ,"APIs and DOM","Add-ons & Extensions","CSS","Canvas","Firefox","Firefox OS","Games","HTML","HTTP","JavaScript","Marketplace","MathML","Mobile","Open Web Apps","SVG","Web Development","Web Standards","WebExtensions","WebGL","Writing Documentation","XPCOM","XUL"
  16. ,"Search Topics","http://kb.mozillazine.org/Using_keyword_searches","Keyword Searching"]
  17. };
  18. localized= localized[ navigator.language ] || localized[ navigator.language.split('-')[0] ] || localized.en;
  19.  
  20. //existing document elements:
  21. var form= document.getElementById('nav-main-search') || document.querySelector('form.search-form');
  22. var input= document.getElementById('main-q');
  23.  
  24. //CSS
  25. var style= getComputedStyle(form);
  26. var x= document.createElement('style');
  27. document.head.appendChild(x);
  28. var s= '.search-results-filters {position: absolute;left:calc( '+ ( style.borderBottomLeftRadius ? style.borderBottomLeftRadius : style.paddingLeft +' + '+ style.borderLeftWidth +' + '+ style.borderLeftWidth ) +' );top: calc( '+ style.height +' - '+ style.borderBottomWidth +' );'
  29. +`position: absolute;border:inherit;border-bottom-left-radius: inherit;border-bottom-right-radius: inherit;background-color: #fff;padding: 8px;border-top-width:0;z-index:9;margin:0;}
  30. .search-results-filters label {display:block;padding-bottom:10px;}
  31. .page-header {z-index:9;}
  32. @media all and (min-width:47.9385em) and (max-width: 74.9375em) {
  33. .page-header .main-nav {grid-row:1/1;grid-column:2/3;}
  34. .page-header .header-search {grid-row:2/2;grid-column:2/3;}
  35. }
  36. @media all and (max-width:47.9375em) {
  37. .search-results-filters{top:calc(1.6em + 22px);border:thin solid #000;}
  38. }`;
  39. if( style.borderTopStyle === 'none' ){
  40. var y= getComputedStyle(input);
  41. s+= '\n.search-results-filters {border:'+
  42. y.borderTopStyle + ' '+ y.borderTopWidth +' '+ y.borderTopColor
  43. +';}';
  44. }
  45. x.innerHTML= s;
  46. var fset= document.createElement('fieldset');
  47. fset.className= "search-results-filters";
  48. fset.style.display= "none";
  49. var legend= document.createElement('b');
  50. legend.appendChild(document.createTextNode( localized[localized.length-3] ));// "search topics"
  51. fset.appendChild(legend);
  52.  
  53. var checklist= [];
  54. var all;
  55. for(var item,check,i=0;i<filters.length;++i){
  56. x= filters[i];
  57. item= document.createElement('label');
  58. check= document.createElement('input');
  59. check.type= "checkbox";
  60. check.name= "topic";
  61. check.value= x;
  62. if( (location+'').indexOf('topic='+x) >=0 ){
  63. check.checked= true;
  64. }
  65. check.addEventListener('click',function(e){
  66. e.target.focus();//click should focus so submit can work
  67. },false);
  68. if(x==='all'){//"all" special case
  69. all= check;
  70. check.name='';
  71. if( (location+'').indexOf('topic=') === -1 ){
  72. check.checked= true;
  73. }
  74. check.addEventListener('change',function(e){
  75. if( e.target.checked ){
  76. for(var i=checklist.length;--i>=0;){
  77. checklist[i].checked=false;
  78. }
  79. }
  80. },false);
  81. }else{
  82. checklist.push(check);
  83. check.addEventListener('change',function(e){
  84. if( e.target.checked ){
  85. all.checked= false;
  86. }
  87. },false);
  88. }
  89. item.appendChild(check);
  90. item.appendChild(document.createTextNode(" "+ localized[i]));
  91. fset.appendChild(item);
  92. }
  93.  
  94. item = document.createElement('hr');
  95. fset.appendChild(item);
  96.  
  97. item = document.createElement('a');
  98. item.innerText= localized[localized.length-1];// "keyword searching"
  99. item.href= localized[localized.length-2];// url help on keyword searching
  100. fset.appendChild(item);
  101.  
  102. form.appendChild(fset);
  103. form.addEventListener('mouseover',function(){fset.style.display="block"},false);
  104. form.addEventListener('mouseout',function(){fset.style.display="none"},false);
  105.  
  106. input.setAttribute('autocomplete','off');
  107. input.addEventListener('focus',function(){fset.style.display="block"},false);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址