Enhance your Gemini prompts with AI-powered improvements for better results
Size
10.5 KB
Version
1.0.1
Created
Oct 16, 2025
Updated
7 days ago
1// ==UserScript==
2// @name Gemini Prompt Enhancer
3// @description Enhance your Gemini prompts with AI-powered improvements for better results
4// @version 1.0.1
5// @match https://*.gemini.google.com/*
6// @icon https://www.gstatic.com/lamda/images/gemini_sparkle_aurora_33f86dc0c0257da337c63.svg
7// ==/UserScript==
8(function() {
9 'use strict';
10
11 console.log('Gemini Prompt Enhancer: Extension loaded');
12
13 // Debounce function to prevent multiple rapid calls
14 function debounce(func, wait) {
15 let timeout;
16 return function executedFunction(...args) {
17 const later = () => {
18 clearTimeout(timeout);
19 func(...args);
20 };
21 clearTimeout(timeout);
22 timeout = setTimeout(later, wait);
23 };
24 }
25
26 // Add custom styles for the enhance button
27 function addStyles() {
28 const styles = `
29 .prompt-enhancer-btn {
30 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
31 color: white;
32 border: none;
33 border-radius: 20px;
34 padding: 8px 16px;
35 font-size: 14px;
36 font-weight: 500;
37 cursor: pointer;
38 display: flex;
39 align-items: center;
40 gap: 6px;
41 transition: all 0.3s ease;
42 box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
43 margin-right: 8px;
44 }
45
46 .prompt-enhancer-btn:hover {
47 transform: translateY(-1px);
48 box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
49 }
50
51 .prompt-enhancer-btn:active {
52 transform: translateY(0);
53 }
54
55 .prompt-enhancer-btn:disabled {
56 opacity: 0.6;
57 cursor: not-allowed;
58 transform: none;
59 }
60
61 .prompt-enhancer-btn .spinner {
62 width: 14px;
63 height: 14px;
64 border: 2px solid rgba(255, 255, 255, 0.3);
65 border-top-color: white;
66 border-radius: 50%;
67 animation: spin 0.8s linear infinite;
68 }
69
70 @keyframes spin {
71 to { transform: rotate(360deg); }
72 }
73
74 .prompt-enhancer-container {
75 display: flex;
76 align-items: center;
77 }
78
79 .enhance-icon {
80 font-size: 16px;
81 }
82 `;
83
84 const styleElement = document.createElement('style');
85 styleElement.textContent = styles;
86 document.head.appendChild(styleElement);
87 console.log('Gemini Prompt Enhancer: Styles added');
88 }
89
90 // Get the current prompt text from the rich-textarea
91 function getCurrentPrompt() {
92 const richTextarea = document.querySelector('rich-textarea');
93 if (!richTextarea) {
94 console.error('Gemini Prompt Enhancer: rich-textarea not found');
95 return '';
96 }
97
98 const contentEditable = richTextarea.querySelector('[contenteditable="true"]');
99 if (!contentEditable) {
100 console.error('Gemini Prompt Enhancer: contenteditable element not found');
101 return '';
102 }
103
104 return contentEditable.textContent || '';
105 }
106
107 // Set the enhanced prompt back to the textarea
108 function setPrompt(text) {
109 const richTextarea = document.querySelector('rich-textarea');
110 if (!richTextarea) {
111 console.error('Gemini Prompt Enhancer: rich-textarea not found');
112 return false;
113 }
114
115 const contentEditable = richTextarea.querySelector('[contenteditable="true"]');
116 if (!contentEditable) {
117 console.error('Gemini Prompt Enhancer: contenteditable element not found');
118 return false;
119 }
120
121 // Set the text content
122 contentEditable.textContent = text;
123
124 // Trigger input event to notify the application
125 const inputEvent = new Event('input', { bubbles: true, cancelable: true });
126 contentEditable.dispatchEvent(inputEvent);
127
128 // Focus the element
129 contentEditable.focus();
130
131 console.log('Gemini Prompt Enhancer: Prompt updated successfully');
132 return true;
133 }
134
135 // Enhance the prompt using AI
136 async function enhancePrompt(originalPrompt) {
137 console.log('Gemini Prompt Enhancer: Enhancing prompt:', originalPrompt);
138
139 try {
140 const enhancedPrompt = await RM.aiCall(
141 `You are a prompt engineering expert. Enhance the following prompt to make it more effective for getting better results from an AI assistant like Gemini.
142
143Guidelines:
144- Make it more specific and clear
145- Add relevant context if needed
146- Structure it better for clarity
147- Keep the core intent intact
148- Don't make it overly long or complex
149- Return ONLY the enhanced prompt, nothing else
150
151Original prompt: "${originalPrompt}"`,
152 {
153 type: "json_schema",
154 json_schema: {
155 name: "enhanced_prompt",
156 schema: {
157 type: "object",
158 properties: {
159 enhancedPrompt: {
160 type: "string",
161 description: "The improved version of the prompt"
162 },
163 improvements: {
164 type: "array",
165 items: { type: "string" },
166 description: "List of improvements made"
167 }
168 },
169 required: ["enhancedPrompt"]
170 }
171 }
172 }
173 );
174
175 console.log('Gemini Prompt Enhancer: Enhancement complete', enhancedPrompt);
176 return enhancedPrompt.enhancedPrompt;
177 } catch (error) {
178 console.error('Gemini Prompt Enhancer: Error enhancing prompt:', error);
179 throw error;
180 }
181 }
182
183 // Create and add the enhance button
184 function createEnhanceButton() {
185 // Check if button already exists
186 if (document.querySelector('.prompt-enhancer-btn')) {
187 console.log('Gemini Prompt Enhancer: Button already exists');
188 return;
189 }
190
191 // Find the send button container
192 const sendButton = document.querySelector('button.send-button');
193 if (!sendButton) {
194 console.log('Gemini Prompt Enhancer: Send button not found, will retry');
195 return;
196 }
197
198 const buttonContainer = sendButton.parentElement;
199 if (!buttonContainer) {
200 console.error('Gemini Prompt Enhancer: Button container not found');
201 return;
202 }
203
204 // Create enhance button
205 const enhanceBtn = document.createElement('button');
206 enhanceBtn.className = 'prompt-enhancer-btn';
207 enhanceBtn.innerHTML = `
208 <span class="enhance-icon">✨</span>
209 <span class="btn-text">Enhance</span>
210 `;
211 enhanceBtn.title = 'Enhance your prompt with AI';
212
213 // Add click handler
214 enhanceBtn.addEventListener('click', async (e) => {
215 e.preventDefault();
216 e.stopPropagation();
217
218 const currentPrompt = getCurrentPrompt();
219 if (!currentPrompt || currentPrompt.trim().length === 0) {
220 alert('Please enter a prompt first!');
221 return;
222 }
223
224 // Disable button and show loading state
225 enhanceBtn.disabled = true;
226 const btnText = enhanceBtn.querySelector('.btn-text');
227 const originalText = btnText.textContent;
228 btnText.textContent = 'Enhancing...';
229 enhanceBtn.querySelector('.enhance-icon').innerHTML = '<div class="spinner"></div>';
230
231 try {
232 const enhanced = await enhancePrompt(currentPrompt);
233 setPrompt(enhanced);
234
235 // Show success feedback
236 btnText.textContent = 'Enhanced!';
237 setTimeout(() => {
238 btnText.textContent = originalText;
239 enhanceBtn.querySelector('.enhance-icon').textContent = '✨';
240 enhanceBtn.disabled = false;
241 }, 2000);
242 } catch (error) {
243 console.error('Gemini Prompt Enhancer: Enhancement failed:', error);
244 alert('Failed to enhance prompt. Please try again.');
245
246 // Reset button
247 btnText.textContent = originalText;
248 enhanceBtn.querySelector('.enhance-icon').textContent = '✨';
249 enhanceBtn.disabled = false;
250 }
251 });
252
253 // Insert the button before the send button
254 buttonContainer.insertBefore(enhanceBtn, sendButton);
255 console.log('Gemini Prompt Enhancer: Enhance button added successfully');
256 }
257
258 // Initialize the extension
259 function init() {
260 console.log('Gemini Prompt Enhancer: Initializing...');
261
262 addStyles();
263
264 // Wait for the page to be fully loaded
265 const checkAndAddButton = () => {
266 const sendButton = document.querySelector('button.send-button');
267 if (sendButton) {
268 createEnhanceButton();
269 } else {
270 console.log('Gemini Prompt Enhancer: Waiting for send button...');
271 setTimeout(checkAndAddButton, 1000);
272 }
273 };
274
275 // Start checking for the send button
276 if (document.readyState === 'loading') {
277 document.addEventListener('DOMContentLoaded', checkAndAddButton);
278 } else {
279 checkAndAddButton();
280 }
281
282 // Watch for DOM changes to re-add button if needed (e.g., after navigation)
283 const observer = new MutationObserver(debounce(() => {
284 const sendButton = document.querySelector('button.send-button');
285 const enhanceButton = document.querySelector('.prompt-enhancer-btn');
286
287 if (sendButton && !enhanceButton) {
288 console.log('Gemini Prompt Enhancer: Re-adding button after DOM change');
289 createEnhanceButton();
290 }
291 }, 500));
292
293 observer.observe(document.body, {
294 childList: true,
295 subtree: true
296 });
297
298 console.log('Gemini Prompt Enhancer: Initialization complete');
299 }
300
301 // Start the extension
302 init();
303})();