Reddit search on Google

Adds a button to search Reddit posts with Google

当前为 2019-09-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit search on Google
  3. // @version 1.2
  4. // @description Adds a button to search Reddit posts with Google
  5. // @author Mario O.M.
  6. // @namespace https://github.com/marioortizmanero/reddit-search-on-google/
  7. // @include http*://www.google.*/search*
  8. // @include http*://google.*/search*
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. // Change this to false if you don't want an icon
  13. const useIcon = true;
  14. // Change this to true if you want to add the button to the right of the 'Tools' button
  15. const appendRight = false;
  16.  
  17.  
  18. const queryRegex = /q=[^&]+/g;
  19. const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g;
  20. const redditUrl = "+site%3Areddit.com";
  21. const redditIcon = '<svg foscusable="false" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"><path d="M24 11.779c0-1.459-1.192-2.645-2.657-2.645-.715 0-1.363.286-1.84.746-1.81-1.191-4.259-1.949-6.971-2.046l1.483-4.669 4.016.941-.006.058c0 1.193.975 2.163 2.174 2.163 1.198 0 2.172-.97 2.172-2.163s-.975-2.164-2.172-2.164c-.92 0-1.704.574-2.021 1.379l-4.329-1.015c-.189-.046-.381.063-.44.249l-1.654 5.207c-2.838.034-5.409.798-7.3 2.025-.474-.438-1.103-.712-1.799-.712-1.465 0-2.656 1.187-2.656 2.646 0 .97.533 1.811 1.317 2.271-.052.282-.086.567-.086.857 0 3.911 4.808 7.093 10.719 7.093s10.72-3.182 10.72-7.093c0-.274-.029-.544-.075-.81.832-.447 1.405-1.312 1.405-2.318zm-17.224 1.816c0-.868.71-1.575 1.582-1.575.872 0 1.581.707 1.581 1.575s-.709 1.574-1.581 1.574-1.582-.706-1.582-1.574zm9.061 4.669c-.797.793-2.048 1.179-3.824 1.179l-.013-.003-.013.003c-1.777 0-3.028-.386-3.824-1.179-.145-.144-.145-.379 0-.523.145-.145.381-.145.526 0 .65.647 1.729.961 3.298.961l.013.003.013-.003c1.569 0 2.648-.315 3.298-.962.145-.145.381-.144.526 0 .145.145.145.379 0 .524zm-.189-3.095c-.872 0-1.581-.706-1.581-1.574 0-.868.709-1.575 1.581-1.575s1.581.707 1.581 1.575-.709 1.574-1.581 1.574z"/></svg>';
  22.  
  23.  
  24. (function() {
  25. // Creating the element
  26. var el = document.createElement('div');
  27. el.className = 'hdtb-mitem';
  28. var link = document.createElement('a');
  29.  
  30. // Adding the svg icon
  31. if (useIcon) {
  32. var span = document.createElement('span');
  33. span.className = 'HF9Klc ZYMsjf';
  34. span.innerHTML += redditIcon;
  35. link.appendChild(span);
  36. }
  37.  
  38. // Hyperlink to add 'site:reddit.com' to the query
  39. link.appendChild(document.createTextNode('Reddit'));
  40. link.href = window.location.href.replace(queryRegex, (match) => {
  41. // Replaces the existing `site` flags
  42. return match.search(siteRegex) >= 0 ? match.replace(siteRegex, redditUrl) : match + redditUrl;
  43. });
  44. el.appendChild(link);
  45.  
  46. // Inserting the element into Google search
  47. if (appendRight) {
  48. var toolsBtn = document.getElementById('hdtb-tls');
  49. toolsBtn.parentNode.insertBefore(el, toolsBtn.nextSibling);
  50. } else {
  51. var button = document.getElementById('hdtb-msb-vis');
  52. button.appendChild(el);
  53. }
  54. })();