Repository navigation
Expand file tree
/
Copy pathchatgpt-search-engine.user.js
More file actions
168 lines (133 loc) · 4.76 KB
/
Copy pathchatgpt-search-engine.user.js
File metadata and controls
168 lines (133 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// ==UserScript==
// @name ChatGPT URL Search Injector & Auto-Submit
// @namespace http://tampermonkey.net/
// @version 1.3
// @description Allows searching ChatGPT directly via URL: https://chatgpt.com/?q=your+query
// @author Leonidas
// @match https://chatgpt.com/*
// @match https://chat.openai.com/*
// @run-at document-idle
// @grant none
// ==/UserScript==
// Browser search engine URL:
// https://chatgpt.com/?q=%s
(function () {
'use strict';
const STORAGE_KEY = 'chatgpt-url-search-prompt';
const params = new URLSearchParams(window.location.search);
const query = params.get('q') || params.get('prompt');
// Step 1: Save the query and redirect away from ChatGPT's native ?q= flow.
if (query) {
try {
sessionStorage.setItem(STORAGE_KEY, query);
} catch (error) {
console.warn('[ChatGPT URL Search] Could not save prompt:', error);
}
const cleanUrl =
window.location.origin +
window.location.pathname +
window.location.hash;
if (window.location.search !== '') {
window.location.replace(cleanUrl);
}
return;
}
// Step 2: After the clean redirect, retrieve and insert the saved prompt.
const savedQuery = sessionStorage.getItem(STORAGE_KEY);
if (!savedQuery) return;
// Remove it immediately so refreshing does not insert it again.
sessionStorage.removeItem(STORAGE_KEY);
let attempts = 0;
let injected = false;
const composerPoll = setInterval(() => {
attempts++;
if (attempts > 300) {
clearInterval(composerPoll);
console.warn('[ChatGPT URL Search] Composer did not appear.');
return;
}
const composer =
document.querySelector('#mobile-composer-prompt') ||
document.querySelector('textarea[name="prompt"]') ||
document.querySelector('#prompt-textarea') ||
document.querySelector('div[contenteditable="true"]');
if (!composer || injected) return;
injected = true;
clearInterval(composerPoll);
// Let ChatGPT finish initializing its composer/editor bindings.
setTimeout(() => {
insertPrompt(composer, savedQuery);
}, 300);
}, 100);
// Stop polling after 30 seconds.
setTimeout(() => {
clearInterval(composerPoll);
}, 30000);
function insertPrompt(composer, text) {
composer.focus();
if (composer.tagName === 'TEXTAREA') {
insertIntoTextarea(composer, text);
} else {
insertIntoEditable(composer, text);
}
// Give ChatGPT time to register the inserted draft before Enter.
setTimeout(() => {
pressEnter(composer);
}, 700);
}
function insertIntoTextarea(textarea, text) {
textarea.value = '';
textarea.dispatchEvent(new Event('input', {
bubbles: true,
composed: true
}));
textarea.focus();
const inserted = document.execCommand('insertText', false, text);
// Fallback if execCommand is unavailable or blocked.
if (!inserted || textarea.value !== text) {
const nativeSetter = Object.getOwnPropertyDescriptor(
window.HTMLTextAreaElement.prototype,
'value'
).set;
nativeSetter.call(textarea, text);
textarea.dispatchEvent(new InputEvent('input', {
bubbles: true,
composed: true,
inputType: 'insertText',
data: text
}));
}
}
function insertIntoEditable(editor, text) {
editor.textContent = '';
editor.dispatchEvent(new InputEvent('beforeinput', {
bubbles: true,
composed: true,
inputType: 'insertText',
data: text
}));
document.execCommand('insertText', false, text);
editor.dispatchEvent(new InputEvent('input', {
bubbles: true,
composed: true,
inputType: 'insertText',
data: text
}));
}
function pressEnter(composer) {
const enterOptions = {
key: 'Enter',
code: 'Enter',
keyCode: 13,
which: 13,
bubbles: true,
cancelable: true,
composed: true
};
// Send the normal Enter key sequence.
composer.dispatchEvent(new KeyboardEvent('keydown', enterOptions));
composer.dispatchEvent(new KeyboardEvent('keypress', enterOptions));
composer.dispatchEvent(new KeyboardEvent('keyup', enterOptions));
console.log('[ChatGPT URL Search] Enter sent.');
}
})();