GreasyFork Search

To search scripts using Google Search

当前为 2023-07-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GreasyFork Search
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3.1
  5. // @description To search scripts using Google Search
  6. // @author CY Fung
  7. // @match https://greasyfork.org/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. let input = document.querySelector('form input[name="q"]');
  17. if (!(input instanceof HTMLInputElement)) return;
  18. let form = input.closest('form');
  19. if (!(form instanceof HTMLFormElement)) return;
  20.  
  21.  
  22. let locales = [...document.querySelectorAll('select#language-selector-locale > option')].map(x => x.value)
  23.  
  24.  
  25. form.addEventListener('submit', function (evt) {
  26.  
  27. let form = evt.target;
  28. if (!(form instanceof HTMLFormElement)) return;
  29. let input = form.querySelector('input[name="q"]');
  30. if (!(input instanceof HTMLInputElement)) return;
  31.  
  32. let value = input.value;
  33. let oldValue = value;
  34. const lang = document.documentElement.lang || '';
  35.  
  36. let useLang = false;
  37.  
  38.  
  39. let u = 0;
  40. let isGoogleSearch = false;
  41.  
  42. let sites = [];
  43.  
  44. const split = value.split(/\s+/);
  45. let reformedSplit = [];
  46. for (const s of split) {
  47.  
  48. if (!isGoogleSearch && /^[a-z][a-z0-9_-]{2,}(\.[a-z][a-z0-9_-]{2,})*(\.[a-z-]{2,4})+$/.test(s)) {
  49. sites.push(s);
  50. } else if (u === 0 && s === 'g') {
  51. isGoogleSearch = true;
  52. } else if (locales.indexOf(s) >= 0 || s === lang) {
  53. useLang = s;
  54. } else {
  55. reformedSplit.push(s);
  56. }
  57. u++;
  58. }
  59.  
  60. value = reformedSplit.join(' ')
  61.  
  62.  
  63. if (isGoogleSearch && value) {
  64. let q = value.replace('g ', '');
  65.  
  66. let m = "-inurl%3A%22%2Fusers%2F%22+-inurl%3A%22%2Fdiscussions%22-inurl%3A%22%2Fstats%22+-inurl%3A%22%2Ffeedback%22+-inurl%3A%22%2Fcode%22+-inurl%3A%22q%3D%22+-inurl%3A%22%2Fby-site%2F%22+inurl%3A%22%2Fscripts%2F%22+site%3Agreasyfork.org";
  67.  
  68.  
  69.  
  70. let lr = useLang ? `&lr=lang_${useLang}` : '';
  71. evt.preventDefault();
  72. location.href = `https://www.google.com/search?q=${encodeURIComponent(q)}+${m}${lr}`
  73.  
  74. } else if (value) {
  75.  
  76. if (!form.querySelector('input[name="site"]')) {
  77. input.parentNode.insertBefore(Object.assign(document.createElement('input'), {
  78. type: 'hidden',
  79. name: 'site',
  80. value: ''
  81. }
  82. ), input.nextSibiling);
  83. }
  84. const site = form.querySelector('input[name="site"]');
  85.  
  86.  
  87. value = value.replace(/\s+/g, ' ');
  88. if (sites.length === 1) {
  89. site.value = sites[0];
  90. }
  91. else site.value == '';
  92.  
  93. if (form.getAttribute('action') === `/${lang}/scripts` && useLang && useLang !== lang) {
  94.  
  95. form.setAttribute('action', `/${useLang}/scripts`)
  96. }
  97.  
  98. if (site.value === '') site.remove();
  99.  
  100.  
  101. if (value !== oldValue) input.value = value;
  102.  
  103.  
  104. } else {
  105. evt.preventDefault();
  106. }
  107.  
  108. })
  109.  
  110. // Your code here...
  111. })();