Greasy Fork - Search other sites

Add search option to search on Userscripts.org and *cough* OpenUserJS.org.

目前为 2014-04-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @id greasyfork-search-other-sites@loucypher
  3. // @name Greasy Fork - Search other sites
  4. // @namespace https://github.com/LouCypher/userscripts
  5. // @description Add search option to search on Userscripts.org and *cough* OpenUserJS.org.
  6. // @version 1.0
  7. // @author LouCypher
  8. // @license MIT License
  9. // @contributionURL http://loucypher.github.io/userscripts/donate.html?Greasy+Fork+-+Search+other+sites
  10. // @homepageURL https://greasyfork.org/scripts/259
  11. // @supportURL https://greasyfork.org/scripts/259/feedback
  12. // @resource LICENSE https://raw.github.com/LouCypher/userscripts/master/greasyfork/search-other-sites/LICENSE.txt
  13. // @resource CHANGELOG https://raw.github.com/LouCypher/userscripts/master/greasyfork/search-other-sites/CHANGELOG.txt
  14. // @include https://greasyfork.org/*
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. function $(aSelector, aNode) {
  19. return (aNode || document).querySelector(aSelector);
  20. }
  21.  
  22. function createElement(aTagName) {
  23. return document.createElement(aTagName);
  24. }
  25.  
  26. function createText(aText) {
  27. return document.createTextNode(aText);
  28. }
  29.  
  30. function createLink(aURL, aText, aName) {
  31. var link = createElement("a");
  32. aURL && (link.href = aURL);
  33. aText && (link.textContent = aText);
  34. aName && (link.name = aName);
  35. return link;
  36. }
  37.  
  38. function addStyle(aCSS) {
  39. var style = createElement("style");
  40. style.type = "text/css";
  41. style.textContent = aCSS;
  42. if (document.head)
  43. document.head.appendChild(style);
  44. else
  45. document.documentElement.appendChild(style);
  46. return style;
  47. }
  48.  
  49. var sites = [
  50. { text: "Userscripts.org", url: "http://userscripts.org/scripts/search?q=" },
  51. { text: "OpenUserJS.org", url: "https://openuserjs.org/search/" }
  52. ];
  53.  
  54. function onsubmit(aEvent) {
  55. var searchURL;
  56. var query = aEvent.target.q.value;
  57. var site = $("#search-other-sites");
  58. switch (parseInt(site.value)) {
  59. case 2: searchURL = sites[1].url; break;
  60. case 1: searchURL = sites[0].url; break;
  61. default: searchURL = null;
  62. }
  63. if (searchURL) {
  64. aEvent.preventDefault();
  65. location.assign(searchURL + encodeURIComponent(query));
  66. }
  67. }
  68.  
  69. var form = $("#script-search");
  70. if (form) {
  71. addStyle("#search-other-sites{width:19px;direction:rtl}" +
  72. "#link-other-sites li{line-height:1.5em}");
  73.  
  74. var select = form.insertBefore(createElement("select"), form.lastChild);
  75. select.id = "search-other-sites";
  76. select.title = "Search other sites";
  77. select.innerHTML = '<option value="0">Greasy Fork</option>'
  78. + '<option value="1">' + sites[0].text + '</option>'
  79. + '<option value="2">' + sites[1].text + '</option>';
  80.  
  81. form.addEventListener("submit", onsubmit);
  82. }
  83.  
  84. if (location.pathname === "/scripts/search") {
  85. var xpath = "//p[@id='script-list-sort']/following-sibling::p[text()='No scripts found.']";
  86. var p = document.evaluate(xpath, document, null, 9, null).singleNodeValue;
  87. if (p) {
  88. var query = $("#script-search").q.value;
  89. p.appendChild(createElement("br"));
  90. p.appendChild(createText("Search '" + query + "' on other sites:"));
  91. var ul = document.body.insertBefore(createElement("ul"), p.nextSibling);
  92. ul.id = "link-other-sites";
  93. var li;
  94. sites.forEach(function(site) {
  95. li = ul.appendChild(createElement("li"));
  96. li.appendChild(createLink(site.url + encodeURIComponent(query), site.text));
  97. });
  98. }
  99. }