Reddit Search Button Next to Google Search

Adds a Reddit search button next to Google search button

  1. // ==UserScript==
  2. // @name Reddit Search Button Next to Google Search
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Adds a Reddit search button next to Google search button
  6. // @author You
  7. // @match https://www.google.com/*
  8. // @match https://google.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function addCustomStyles() {
  16. const styleSheet = document.createElement("style");
  17. styleSheet.textContent = `
  18. .reddit-search-btn {
  19. background: linear-gradient(to bottom, #ff4500, #dc3545);
  20. border: none;
  21. color: white;
  22. padding: 8px 16px;
  23. border-radius: 20px;
  24. font-family: Arial, sans-serif;
  25. font-weight: bold;
  26. cursor: pointer;
  27. transition: all 0.3s ease;
  28. box-shadow: 0 2px 4px rgba(0,0,0,0.2);
  29. text-transform: uppercase;
  30. font-size: 14px;
  31. letter-spacing: 0.5px;
  32. height: 36px;
  33. display: inline-flex;
  34. align-items: center;
  35. margin-left: 10px;
  36. vertical-align: middle;
  37. }
  38.  
  39. .reddit-search-btn:hover {
  40. background: linear-gradient(to bottom, #ff5610, #e84555);
  41. box-shadow: 0 4px 8px rgba(0,0,0,0.3);
  42. }
  43.  
  44. .reddit-search-btn:active {
  45. box-shadow: 0 1px 2px rgba(0,0,0,0.2);
  46. }
  47.  
  48. .reddit-search-btn::before {
  49. content: "🔍 ";
  50. margin-right: 4px;
  51. }
  52. `;
  53. document.head.appendChild(styleSheet);
  54. }
  55.  
  56. function createRedditButton() {
  57. // Find the Google search buttons container
  58. const buttonsContainer = document.querySelector('div.FPdoLc, div.aajZCb');
  59. if (!buttonsContainer) return;
  60.  
  61. // Check if button already exists
  62. if (buttonsContainer.querySelector('.reddit-search-btn')) return;
  63.  
  64. // Create the Reddit button
  65. const redditButton = document.createElement('button');
  66. redditButton.textContent = 'Search Reddit';
  67. redditButton.className = 'reddit-search-btn';
  68. redditButton.type = 'button';
  69.  
  70. // Add click event listener
  71. redditButton.addEventListener('click', function(e) {
  72. e.preventDefault();
  73. // Get text from Google search input
  74. const searchInput = document.querySelector('textarea.gLFyf, input.gLFyf');
  75. if (searchInput && searchInput.value.trim()) {
  76. const redditSearchUrl = `https://www.google.com/search?q=site:reddit.com ${encodeURIComponent(searchInput.value)}`;
  77. window.open(redditSearchUrl, '_blank');
  78. }
  79. });
  80.  
  81. // Add the button to the container
  82. const googleSearchButton = buttonsContainer.querySelector('input[value="Google Search"], button[aria-label="Google Search"]');
  83. if (googleSearchButton) {
  84. googleSearchButton.parentElement.appendChild(redditButton);
  85. } else {
  86. buttonsContainer.appendChild(redditButton);
  87. }
  88. }
  89.  
  90. function init() {
  91. addCustomStyles();
  92.  
  93. // Initial creation with a delay to ensure page is loaded
  94. setTimeout(createRedditButton, 1000);
  95.  
  96. // Watch for dynamic changes
  97. const observer = new MutationObserver(function() {
  98. createRedditButton();
  99. });
  100.  
  101. observer.observe(document.body, {
  102. childList: true,
  103. subtree: true
  104. });
  105. }
  106.  
  107. // Run initialization
  108. if (document.readyState === 'loading') {
  109. document.addEventListener('DOMContentLoaded', init);
  110. } else {
  111. init();
  112. }
  113. })();