Greasy Fork is available in English.

SearchSpecificSite

Adds a button to search Reddit posts with Google

  1. // ==UserScript==
  2. // @name SearchSpecificSite
  3. // @version 0.1
  4. // @description Adds a button to search Reddit posts with Google
  5. // @author Mario O.M.
  6. // @match *://*.google.com/search*
  7. // @match *://*.google.ca/search*
  8. // @run-at document-end
  9. // @license MIT
  10. // @namespace https://greasyfork.org/users/836868
  11. // ==/UserScript==
  12.  
  13. // If the UI version is new
  14. const newUI = true;
  15.  
  16. const queryRegex = /q=[^&]+/g;
  17. const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g;
  18.  
  19. const url_YT = '+site%3Ayoutube.com';
  20. const textNode_YT = '|YT';
  21.  
  22. const url_reddit = '+site%3Areddit.com';
  23. const textNode_reddit = '|Reddit';
  24.  
  25. const url_ELI5 = '+site%3Areddit.com%2Fr%2Fexplainlikeimfive';
  26. const textNode_ELI5 = '|rELI5';
  27.  
  28. const url_engL = '+site%3Areddit.com%2Fr%2FEnglishLearning';
  29. const textNode_engL = '|rEngL';
  30.  
  31. const url_wordref = '+site%3Aforum.wordreference.com';
  32. const textNode_wordref = '|Wordref';
  33.  
  34. const url_hi = '+site%3Ahinative.com';
  35. const textNode_hi = '|Hi';
  36.  
  37. const url_bbc = '+site%3Abbc.com';
  38. const textNode_bbc = '|BBC';
  39.  
  40. const url_cnn = '+site%3Acnn.com';
  41. const textNode_cnn = '|CNN';
  42.  
  43. const url_npr = '+site%3Anpr.org';
  44. const textNode_npr = '|NPR';
  45.  
  46. const url_nyt = '+site%3Anytimes.com';
  47. const textNode_nyt = '|nyt';
  48.  
  49. const url_WP = '+site%3Awashingtonpost.com';
  50. const textNode_WP = '|WP';
  51.  
  52. const url_WSJ = '+site%3Awsj.com';
  53. const textNode_WSJ = '|WSJ';
  54.  
  55.  
  56. const isImageSearch = /[?&]tbm=isch/.test(location.search);
  57.  
  58. (function () {
  59. // Creating the element
  60. let el = document.createElement('div');
  61. el.className = 'hdtb-mitem';
  62.  
  63. const link_YT = document.createElement('a');
  64. const link_reddit = document.createElement('a');
  65. const link_ELI5 = document.createElement('a');
  66. const link_engL = document.createElement('a');
  67. const link_wordref = document.createElement('a');
  68. const link_hi = document.createElement('a');
  69. const link_bbc = document.createElement('a');
  70. const link_cnn = document.createElement('a');
  71. const link_npr = document.createElement('a');
  72. const link_nyt = document.createElement('a');
  73. const link_WP = document.createElement('a');
  74. const link_WSJ = document.createElement('a');
  75.  
  76. // Hyperlink to add 'site:reddit.com' to the query
  77. link_YT.appendChild(document.createTextNode(textNode_YT));
  78. link_YT.href = window.location.href.replace(queryRegex, (match) => {
  79. // Replaces the existing `site` flags
  80. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_YT) : match + url_YT ;
  81. });
  82.  
  83. link_reddit.appendChild(document.createTextNode(textNode_reddit ));
  84. link_reddit.href = window.location.href.replace(queryRegex, (match) => {
  85. // Replaces the existing `site` flags
  86. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_reddit ) : match + url_reddit ;
  87. });
  88.  
  89. link_ELI5.appendChild(document.createTextNode(textNode_ELI5));
  90. link_ELI5.href = window.location.href.replace(queryRegex, (match) => {
  91. // Replaces the existing `site` flags
  92. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_ELI5) : match + url_ELI5;
  93. });
  94.  
  95. link_engL.appendChild(document.createTextNode(textNode_engL));
  96. link_engL.href = window.location.href.replace(queryRegex, (match) => {
  97. // Replaces the existing `site` flags
  98. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_engL) : match + url_engL;
  99. });
  100.  
  101. link_wordref.appendChild(document.createTextNode(textNode_wordref ));
  102. link_wordref.href = window.location.href.replace(queryRegex, (match) => {
  103. // Replaces the existing `site` flags
  104. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_wordref ) : match + url_wordref ;
  105. });
  106.  
  107. link_hi.appendChild(document.createTextNode(textNode_hi));
  108. link_hi.href = window.location.href.replace(queryRegex, (match) => {
  109. // Replaces the existing `site` flags
  110. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_hi ) : match + url_hi ;
  111. });
  112.  
  113. link_bbc.appendChild(document.createTextNode(textNode_bbc));
  114. link_bbc.href = window.location.href.replace(queryRegex, (match) => {
  115. // Replaces the existing `site` flags
  116. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_bbc) : match + url_bbc;
  117. });
  118.  
  119. link_cnn.appendChild(document.createTextNode(textNode_cnn));
  120. link_cnn.href = window.location.href.replace(queryRegex, (match) => {
  121. // Replaces the existing `site` flags
  122. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_cnn) : match + url_cnn;
  123. });
  124.  
  125. link_npr.appendChild(document.createTextNode(textNode_npr));
  126. link_npr.href = window.location.href.replace(queryRegex, (match) => {
  127. // Replaces the existing `site` flags
  128. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_npr) : match + url_npr;
  129. });
  130.  
  131. link_nyt.appendChild(document.createTextNode(textNode_nyt));
  132. link_nyt.href = window.location.href.replace(queryRegex, (match) => {
  133. // Replaces the existing `site` flags
  134. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_nyt) : match + url_nyt;
  135. });
  136.  
  137. link_WP.appendChild(document.createTextNode(textNode_WP));
  138. link_WP.href = window.location.href.replace(queryRegex, (match) => {
  139. // Replaces the existing `site` flags
  140. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_WP) : match + url_WP;
  141. });
  142.  
  143. link_WSJ.appendChild(document.createTextNode(textNode_WSJ));
  144. link_WSJ.href = window.location.href.replace(queryRegex, (match) => {
  145. // Replaces the existing `site` flags
  146. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, url_WSJ) : match + url_WSJ;
  147. });
  148.  
  149.  
  150. if (isImageSearch) {
  151. //link.classList.add('NZmxZe');
  152. //el = link;
  153. } else {
  154. el.appendChild(link_YT );
  155. el.appendChild(link_reddit);
  156. el.appendChild(link_ELI5 );
  157. el.appendChild(link_engL);
  158. el.appendChild(link_wordref );
  159. el.appendChild(link_hi);
  160. el.appendChild(link_bbc );
  161. el.appendChild(link_cnn);
  162. el.appendChild(link_npr );
  163. el.appendChild(link_nyt);
  164. el.appendChild(link_WP );
  165. el.appendChild(link_WSJ);
  166. }
  167.  
  168. // Inserting the element into Google search
  169. /*
  170. if (newUI) {
  171. const toolsBtn = document.querySelector(isImageSearch ? '.ssfWCe' : '.xhjkHe');
  172. toolsBtn.parentNode.appendChild(el, toolsBtn);
  173. } else {
  174. //const toolsBtn = document.querySelector(isImageSearch ? '.ssfWCe' : '.IC1Ck');
  175. //toolsBtn.parentNode.insertBefore(el, toolsBtn);
  176. }
  177. */
  178. // Inserting the element into Google search
  179. //const toolsBtn1 = document.querySelector(isImageSearch ? '.ssfWCe' : '.xhjkHe');
  180. const toolsBtn1 = document.querySelector(isImageSearch ? '.ssfWCe' : '#tools_1');
  181. if (toolsBtn1!=null){
  182. toolsBtn1.parentNode.appendChild(el, toolsBtn1);
  183. }
  184.  
  185. /*
  186. const toolsBtn2 = document.querySelector(isImageSearch ? '.ssfWCe' : '.IC1Ck');
  187. if (toolsBtn2!=null){
  188. toolsBtn2.parentNode.insertBefore(el, toolsBtn2);
  189. }
  190. */
  191. })();