|
| 1 | +// ==UserScript== |
| 2 | +// @name ChatGPT Shortcut for Google Search |
| 3 | +// @namespace http://tampermonkey.net/ |
| 4 | +// @version 1.0.0 |
| 5 | +// @description Add a ChatGPT button inside the Google search bar container |
| 6 | +// @author jamubc |
| 7 | +// @match https://www.google.com/search* |
| 8 | +// @grant none |
| 9 | +// ==/UserScript== |
| 10 | + |
| 11 | +(function() { |
| 12 | + 'use strict'; |
| 13 | + |
| 14 | + // Check if there’s a search query in the URL |
| 15 | + const urlParams = new URLSearchParams(window.location.search); |
| 16 | + const query = urlParams.get('q'); |
| 17 | + |
| 18 | + if (query) { |
| 19 | + // Locate the main search bar container with class 'RNNXgb' |
| 20 | + const searchBarContainer = document.querySelector('.RNNXgb'); |
| 21 | + |
| 22 | + if (searchBarContainer) { |
| 23 | + // Create a ChatGPT button |
| 24 | + const button = document.createElement('button'); |
| 25 | + button.textContent = "Ask ChatGPT"; |
| 26 | + button.style.marginLeft = '10px'; // Space between other elements |
| 27 | + button.style.padding = '6px 10px'; |
| 28 | + button.style.backgroundColor = '#1f1f1f'; |
| 29 | + button.style.color = 'white'; |
| 30 | + button.style.border = 'none'; |
| 31 | + button.style.borderRadius = '4px'; |
| 32 | + button.style.cursor = 'pointer'; |
| 33 | + button.style.fontSize = '12px'; |
| 34 | + button.style.display = 'flex'; |
| 35 | + button.style.alignItems = 'center'; |
| 36 | + |
| 37 | + // Add functionality to the button |
| 38 | + button.addEventListener('click', () => { |
| 39 | + const chatGPTUrl = `https://chat.openai.com/?q=${encodeURIComponent(query)}`; |
| 40 | + window.open(chatGPTUrl, '_blank'); |
| 41 | + }); |
| 42 | + |
| 43 | + // Insert the button at the end of the search bar container |
| 44 | + const buttonContainer = searchBarContainer.querySelector('.BKRPef'); |
| 45 | + if (buttonContainer) { |
| 46 | + buttonContainer.appendChild(button); |
| 47 | + } else { |
| 48 | + console.error("Button container {} not found inside the search bar."); |
| 49 | + console.error("An error will occur when google updates their website."); |
| 50 | + } |
| 51 | + } else { |
| 52 | + console.error("Search bar container {} not found."); |
| 53 | + console.error("An error will occur when google updates their website."); |
| 54 | + } |
| 55 | + } |
| 56 | +})(); |
0 commit comments