newLISP-Documentation-Search

Provide keyword search interface in newLISP documentation

  1. // ==UserScript==
  2. // @name newLISP-Documentation-Search
  3. // @namespace https://github.com/kosh04/userscript
  4. // @version 20141001
  5. // @description Provide keyword search interface in newLISP documentation
  6. // @grant GM_addStyle
  7. // @grant GM_getResourceText
  8. // @match http://www.newlisp.org/*newlisp_manual.html
  9. // @match http://newlisp.nfshost.com/*newlisp_manual.html
  10. // @require http://code.jquery.com/jquery-2.1.1.js
  11. // @require http://code.jquery.com/ui/1.11.1/jquery-ui.js
  12. // @resource jquery-ui.css http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css
  13. // @author KOBAYASHI Shigeru (kosh)
  14. // @license Public domain
  15. // ==/UserScript==
  16.  
  17. GM_addStyle([
  18. GM_getResourceText("jquery-ui.css"),
  19. ".search-control {",
  20. " position: fixed;",
  21. " top: 10px;",
  22. " right: 20px;",
  23. "}",
  24. ".ui-autocomplete {",
  25. " max-height: 100px;",
  26. " overflow-x: hidden;",
  27. " overflow-y: auto;",
  28. "}",
  29. ].join("\n"));
  30.  
  31. $("body").prepend([
  32. "<div class='search-control ui-widget' role='search'>",
  33. " <label for='keyword' style='display:none;'>Search:</label>",
  34. " <input id='keyword' type='search' placeholder='Search keyword' autofocus>",
  35. "</div>",
  36. ].join("\n"));
  37.  
  38. function create_keywords() {
  39. var keywords = [],
  40. grep = function(obj, key, value) {
  41. return $.grep(obj, function(e) { return e[key] === value; });
  42. };
  43.  
  44. $("a[href]").each(function(i, a) {
  45. var hash = a.hash.slice(1), // #hash -> hash
  46. href = a.href;
  47. if (hash.length == 0 || grep(keywords, "href", href).length > 0) {
  48. return;
  49. }
  50. keywords.push({ "label": hash, "href": href });
  51. });
  52.  
  53. return keywords;
  54. }
  55.  
  56. $("#keyword").autocomplete({
  57. source: create_keywords(),
  58. select: function(event, ui) {
  59. window.location.replace(ui.item.href);
  60. }
  61. });