Greasy Fork - Search other sites

Add search option to search on Userscripts.org and *cough* OpenUserJS.org. + Monkey Guts.com(Code remodified)

目前为 2015-05-02 提交的版本,查看 最新版本

  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. + Monkey Guts.com(Code remodified)
  6. // @version 2.0
  7. // @license MIT License
  8. // @author Remodifier: HACKSCOMICON Credits: LouCypher
  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. // @include https://greasyfork.org/*
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. function $(aSelector, aNode) {
  18. return (aNode || document).querySelector(aSelector);
  19. }
  20.  
  21. function createElement(aTagName) {
  22. return document.createElement(aTagName);
  23. }
  24.  
  25. function createText(aText) {
  26. return document.createTextNode(aText);
  27. }
  28.  
  29. function createLink(aURL, aText, aName) {
  30. var link = createElement("a");
  31. aURL && (link.href = aURL);
  32. aText && (link.textContent = aText);
  33. aName && (link.name = aName);
  34. return link;
  35. }
  36.  
  37. function addStyle(aCSS) {
  38. var style = createElement("style");
  39. style.type = "text/css";
  40. style.textContent = aCSS;
  41. if (document.head)
  42. document.head.appendChild(style);
  43. else
  44. document.documentElement.appendChild(style);
  45. return style;
  46. }
  47.  
  48. var sites = [
  49. { name: "Userscripts.org", url: "http://userscripts.org/scripts/search?q=" },
  50. { name: "OpenUserJS", url: "https://openuserjs.org/?q=" },
  51. { name: "MonkeyGuts", url: "https://monkeyguts.com/index.php?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 3: searchURL = sites[2].url; break;
  60. case 2: searchURL = sites[1].url; break;
  61. case 1: searchURL = sites[0].url; break;
  62. default: searchURL = null;
  63. }
  64. if (searchURL) {
  65. aEvent.preventDefault();
  66. location.assign(searchURL + encodeURIComponent(query));
  67. }
  68. }
  69.  
  70. function onchange(aEvent) {
  71. var input = $("#script-search").q;
  72. switch (parseInt(aEvent.target.value)) {
  73. case 3: input.placeholder = "Search " + sites[2].name; break;
  74. case 2: input.placeholder = "Search " + sites[1].name; break;
  75. case 1: input.placeholder = "Search " + sites[0].name; break;
  76. default: input.placeholder = "Search";
  77. }
  78. $("#script-search input[type='submit']").title = input.placeholder;
  79. }
  80.  
  81. var form = $("#script-search");
  82. if (form) {
  83. addStyle("#search-other-sites{width:19px;direction:rtl}" +
  84. "#link-other-sites li{line-height:1.5em}");
  85.  
  86. var select = form.insertBefore(createElement("select"), form.lastChild);
  87. select.id = "search-other-sites";
  88. select.title = "Search other sites";
  89. select.innerHTML = '<option value="0">Greasy Fork</option>'
  90. + '<option value="1">' + sites[0].name + '</option>'
  91. + '<option value="2">' + sites[1].name + '</option>'
  92. + '<option value="3">' + sites[2].name + '</option>';
  93.  
  94. select.addEventListener("change", onchange);
  95. form.addEventListener("submit", onsubmit);
  96. }
  97.  
  98. if (location.pathname === "/scripts/search") {
  99. var xpath = "//p[@id='script-list-sort']/following-sibling::p[text()='No scripts found.']";
  100. var p = document.evaluate(xpath, document, null, 9, null).singleNodeValue;
  101. if (p) {
  102. var query = $("#script-search").q.value;
  103. p.appendChild(createElement("br"));
  104. p.appendChild(createText("Search '" + query + "' on other sites:"));
  105. var ul = document.body.insertBefore(createElement("ul"), p.nextSibling);
  106. ul.id = "link-other-sites";
  107. var li;
  108. sites.forEach(function(site) {
  109. li = ul.appendChild(createElement("li"));
  110. li.appendChild(createLink(site.url + encodeURIComponent(query), site.name));
  111. });
  112. }
  113. }