GreasyFork Search

To search scripts using Google Search

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

  1. // ==UserScript==
  2. // @name GreasyFork Search
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.4
  5. // @description To search scripts using Google Search
  6. // @author CY Fung
  7. // @match https://gf.qytechs.cn/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=gf.qytechs.cn
  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. document.head.appendChild(document.createElement('style')).textContent = `
  25.  
  26.  
  27. @keyframes rs1tmAnimation {
  28. 0% {
  29. background-position-x: 3px;
  30. }
  31. 100% {
  32. background-position-x: 4px;
  33. }
  34. }
  35.  
  36. form.rs1tm{
  37. position: fixed;
  38. top:-300px;
  39. left:-300px;
  40. width: 1px;
  41. height: 1px;
  42. contain: strict;
  43. display: flex;
  44. overflow: hidden;
  45. animation: rs1tmAnimation 1ms linear 1ms 1 normal forwards;
  46. }
  47.  
  48. `
  49. document.addEventListener('animationstart', (evt) => {
  50.  
  51. if (evt.animationName === 'rs1tmAnimation') {
  52. const target = evt.target;
  53. target && target.parentNode && target.remove();
  54. }
  55.  
  56. }, true);
  57.  
  58. form.addEventListener('submit', function (evt) {
  59.  
  60. let form = evt.target;
  61. if (!(form instanceof HTMLFormElement)) return;
  62. let input = form.querySelector('input[name="q"]');
  63. if (!(input instanceof HTMLInputElement)) return;
  64.  
  65. if (form.classList.contains('rs1tm')) return;
  66.  
  67. let value = input.value;
  68. const lang = document.documentElement.lang || '';
  69.  
  70. let useLang = false;
  71.  
  72.  
  73. let u = 0;
  74. let isGoogleSearch = false;
  75.  
  76. let sites = [];
  77.  
  78. const split = value.split(/\s+/);
  79. let forceLang = 'all';
  80. let reformedSplit = [];
  81. for (const s of split) {
  82.  
  83. if (!isGoogleSearch && /^[a-z][a-z0-9_-]{2,}(\.[a-z][a-z0-9_-]{2,})*(\.[a-z-]{2,4})+$/.test(s)) {
  84. sites.push(s);
  85. } else if (s === 'js') {
  86. forceLang = 'js'; reformedSplit.push(s);
  87. } else if (s === 'css') {
  88. forceLang = 'css'; reformedSplit.push(s);
  89. } else if (s === 'user.js') {
  90. forceLang = 'js';
  91. } else if (s === 'user.css') {
  92. forceLang = 'css';
  93. } else if (s === '"js"') {
  94. reformedSplit.push('js');
  95. } else if (s === '"css"') {
  96. reformedSplit.push('css');
  97. } else if (u === 0 && s === 'g') {
  98. isGoogleSearch = true;
  99. } else if (locales.indexOf(s) >= 0 || s === lang) {
  100. useLang = s;
  101. } else {
  102. reformedSplit.push(s);
  103. }
  104. u++;
  105. }
  106.  
  107. value = reformedSplit.join(' ')
  108.  
  109. let onlySite = '';
  110.  
  111. if (sites.length === 1 && sites[0]) {
  112. onlySite = sites[0];
  113. }
  114.  
  115.  
  116. if (isGoogleSearch && value) {
  117. let q = value.replace('g ', '');
  118.  
  119. 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%3Agf.qytechs.cn";
  120.  
  121.  
  122.  
  123. let lr = useLang ? `&lr=lang_${useLang}` : '';
  124. evt.preventDefault();
  125. evt.stopImmediatePropagation();
  126. evt.stopPropagation();
  127. location.href = `https://www.google.com/search?q=${encodeURIComponent(q)}+${m}${lr}`
  128.  
  129. } else if (value || onlySite) {
  130.  
  131.  
  132.  
  133. let newForm = document.createElement('form');
  134. newForm.className = 'rs1tm';
  135. const copyAttr = (x) => {
  136. let t = form.getAttribute(x);
  137. if (typeof t === 'string') newForm.setAttribute(x, t);
  138. }
  139. copyAttr('action');
  140. copyAttr('accept-charset');
  141. copyAttr('method');
  142. newForm.innerHTML = `<input name="q" type="hidden" value="" /><input name="site" type="hidden" /><input name="language" type="hidden" value="all" />`
  143.  
  144.  
  145. const nq = newForm.querySelector('input[name="q"]');
  146. const language = newForm.querySelector('input[name="language"]');
  147. const site = newForm.querySelector('input[name="site"]');
  148.  
  149. value = value.replace(/\s+/g, ' ');
  150. site.value = onlySite;
  151.  
  152. if (form.getAttribute('action') === `/${lang}/scripts` && useLang && useLang !== lang) {
  153. form.setAttribute('action', `/${useLang}/scripts`)
  154. }
  155.  
  156. if (site.value === '') site.remove();
  157.  
  158. nq.value = value;
  159.  
  160. language.value = forceLang;
  161.  
  162. if (language.value === '') language.remove();
  163.  
  164. evt.preventDefault();
  165. evt.stopImmediatePropagation();
  166. evt.stopPropagation();
  167.  
  168. form.parentNode.insertBefore(newForm, form);
  169. newForm.submit();
  170.  
  171.  
  172. } else {
  173. evt.preventDefault();
  174. evt.stopImmediatePropagation();
  175. evt.stopPropagation();
  176. }
  177.  
  178. })
  179.  
  180. // Your code here...
  181. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址