Reddit search on Google

Adds a button to search Reddit posts with Google

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

  1. // ==UserScript==
  2. // @name Reddit search on Google
  3. // @version 1.0
  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. const queryRegex = /q=(.*?)([^&]+)/g;
  13. const redditUrl = "+site%3Areddit.com";
  14.  
  15. (function() {
  16. var el = document.createElement('div');
  17. el.className = 'hdtb-mitem';
  18. var link = document.createElement('a');
  19. link.appendChild(document.createTextNode('Reddit'));
  20.  
  21. link.href = window.location.href.replace(queryRegex, (match,p1,p2) => {
  22. return p2.lastIndexOf(redditUrl)==-1 ? ['q=', p2, redditUrl].join('') : ['q=', p2].join(''); // Only adds the url once
  23. });
  24. el.appendChild(link);
  25.  
  26. var toolsBtn = document.getElementById('hdtb-tls');
  27. toolsBtn.parentNode.insertBefore(el, toolsBtn.nextSibling);
  28. })();