Greasy Fork - Search scripts on other sites (Added more sites)

Add search option to search on Userscripts.org, OpenUserJS.org, MonkeyGuts.com (Code Remodified) and Google(Beta;Work in progress;still functional)

当前为 2015-05-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @id greasyfork-search-other-sites@hackscomicon
  3. // @name Greasy Fork - Search scripts on other sites (Added more sites)
  4. // @namespace https://github.com/LouCypher/userscripts
  5. // @description Add search option to search on Userscripts.org, OpenUserJS.org, MonkeyGuts.com (Code Remodified) and Google(Beta;Work in progress;still functional)
  6. // @version 3.0
  7. // @author HACKSCOMICON Credits:LouCypher
  8. // @license MIT License
  9. // @contributionURL http://loucypher.github.io/userscripts/donate.html?Greasy+Fork+-+Search+other+sites
  10. // @homepageURL https://greasyfork.org/en/scripts/9630
  11. // @supportURL https://greasyfork.org/en/scripts/9630/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. { name: "Google(Beta)", url: "https://www.google.ro/webhp#q=userscript%20" }
  53. ];
  54.  
  55. function onsubmit(aEvent) {
  56. var searchURL;
  57. var query = aEvent.target.q.value;
  58. var site = $("#search-other-sites");
  59. switch (parseInt(site.value)) {
  60. case 4: searchURL = sites[3].url; break;
  61. case 3: searchURL = sites[2].url; break;
  62. case 2: searchURL = sites[1].url; break;
  63. case 1: searchURL = sites[0].url; break;
  64. default: searchURL = null;
  65. }
  66. if (searchURL) {
  67. aEvent.preventDefault();
  68. location.assign(searchURL + encodeURIComponent(query));
  69. }
  70. }
  71.  
  72. function onchange(aEvent) {
  73. var input = $("#script-search").q;
  74. switch (parseInt(aEvent.target.value)) {
  75. case 4: input.placeholder = "Search " + sites[3].name; break;
  76. case 3: input.placeholder = "Search " + sites[2].name; break;
  77. case 2: input.placeholder = "Search " + sites[1].name; break;
  78. case 1: input.placeholder = "Search " + sites[0].name; break;
  79. default: input.placeholder = "Search";
  80. }
  81. $("#script-search input[type='submit']").title = input.placeholder;
  82. }
  83.  
  84. var form = $("#script-search");
  85. if (form) {
  86. addStyle("#search-other-sites{width:19px;direction:rtl}" +
  87. "#link-other-sites li{line-height:1.5em}");
  88.  
  89. var select = form.insertBefore(createElement("select"), form.lastChild);
  90. select.id = "search-other-sites";
  91. select.title = "Search other sites";
  92. select.innerHTML = '<option value="0">Greasy Fork</option>'
  93. + '<option value="1">' + sites[0].name + '</option>'
  94. + '<option value="2">' + sites[1].name + '</option>'
  95. + '<option value="3">' + sites[2].name + '</option>'
  96. + '<option value="4">' + sites[3].name + '</option>';
  97.  
  98. select.addEventListener("change", onchange);
  99. form.addEventListener("submit", onsubmit);
  100. }
  101.  
  102. if (location.pathname === "/scripts/search") {
  103. var xpath = "//p[@id='script-list-sort']/following-sibling::p[text()='No scripts found.']";
  104. var p = document.evaluate(xpath, document, null, 9, null).singleNodeValue;
  105. if (p) {
  106. var query = $("#script-search").q.value;
  107. p.appendChild(createElement("br"));
  108. p.appendChild(createText("Search '" + query + "' on other sites:"));
  109. var ul = document.body.insertBefore(createElement("ul"), p.nextSibling);
  110. ul.id = "link-other-sites";
  111. var li;
  112. sites.forEach(function(site) {
  113. li = ul.appendChild(createElement("li"));
  114. li.appendChild(createLink(site.url + encodeURIComponent(query), site.name));
  115. });
  116. }
  117. }