-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathgrokIntegration.js
More file actions
186 lines (159 loc) · 6.14 KB
/
Copy pathgrokIntegration.js
File metadata and controls
186 lines (159 loc) · 6.14 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2024-2026 nich (@nichxbt). Licensed under the Apache License, Version 2.0.
// scripts/grokIntegration.js
// Browser console script for interacting with Grok AI on X
// Paste in DevTools console on x.com/i/grok
// by nichxbt
(() => {
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
// =============================================
// CONFIGURATION
// =============================================
const CONFIG = {
prompt: 'Analyze the latest tech trends on X',
waitForResponse: true,
maxWaitMs: 30000,
};
// =============================================
const SELECTORS = {
input: '[data-testid="grokInput"], textarea[placeholder*="Ask"], textarea[placeholder*="ask"], [contenteditable="true"]',
send: '[data-testid="grokSendButton"], button[aria-label="Send"], button[data-testid*="send"]',
response: '[data-testid="grokResponse"], [data-testid="grokResponseText"]',
newChat: '[data-testid="grokNewChat"], a[href="/i/grok"]',
};
const findEl = (selectorString) => {
for (const sel of selectorString.split(',').map(s => s.trim())) {
const el = document.querySelector(sel);
if (el) return el;
}
return null;
};
const typeInto = async (el, text) => {
el.focus();
await sleep(200);
if (el.contentEditable === 'true') {
document.execCommand('selectAll', false, null);
document.execCommand('insertText', false, text);
} else {
const nativeSet = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value')?.set
|| Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set;
if (nativeSet) {
nativeSet.call(el, text);
el.dispatchEvent(new Event('input', { bubbles: true }));
el.dispatchEvent(new Event('change', { bubbles: true }));
} else {
el.value = text;
el.dispatchEvent(new Event('input', { bubbles: true }));
}
}
await sleep(300);
};
const waitForResponse = async (maxMs) => {
const startTime = Date.now();
let lastLength = 0;
let stableCount = 0;
console.log('⏳ Waiting for Grok response...');
while (Date.now() - startTime < maxMs) {
const responseEls = document.querySelectorAll(SELECTORS.response);
if (responseEls.length > 0) {
const lastEl = responseEls[responseEls.length - 1];
const currentLength = (lastEl.textContent || '').length;
if (currentLength > 0 && currentLength === lastLength) {
stableCount++;
if (stableCount >= 3) {
console.log('✅ Response received.');
return lastEl.textContent.trim();
}
} else {
stableCount = 0;
lastLength = currentLength;
}
}
await sleep(1000);
const elapsed = ((Date.now() - startTime) / 1000).toFixed(0);
if (parseInt(elapsed) % 5 === 0 && parseInt(elapsed) > 0) {
console.log(` ⏳ Still waiting... (${elapsed}s)`);
}
}
// Timeout — grab whatever is there
const responseEls = document.querySelectorAll(SELECTORS.response);
if (responseEls.length > 0) {
const text = responseEls[responseEls.length - 1].textContent?.trim();
if (text) {
console.log('⚠️ Timeout reached, but partial response captured.');
return text;
}
}
// Fallback: try markdown or grok-related elements
const fallback = document.querySelectorAll('[data-testid*="grok"], [class*="markdown"]');
if (fallback.length > 0) {
const text = fallback[fallback.length - 1].textContent?.trim();
if (text) return text;
}
return null;
};
const run = async () => {
console.log('🤖 Grok Integration — AI Assistant');
console.log('━'.repeat(50));
console.log(` Prompt: "${CONFIG.prompt.slice(0, 80)}${CONFIG.prompt.length > 80 ? '...' : ''}"`);
console.log(` Max wait: ${CONFIG.maxWaitMs / 1000}s`);
console.log('');
// Check page
if (!window.location.href.includes('/i/grok')) {
console.log('⚠️ Navigate to x.com/i/grok first.');
console.log('🔗 Redirecting...');
window.location.href = 'https://x.com/i/grok';
return;
}
await sleep(2000);
// Find input
const inputEl = findEl(SELECTORS.input);
if (!inputEl) {
console.error('❌ Grok input field not found.');
console.log(' Tried selectors:', SELECTORS.input);
console.log(' Make sure the Grok page is fully loaded.');
return;
}
console.log('✅ Found Grok input field.');
// Type prompt
console.log('📝 Typing prompt...');
await typeInto(inputEl, CONFIG.prompt);
await sleep(500);
// Find and click send
const sendBtn = findEl(SELECTORS.send);
if (!sendBtn) {
console.error('❌ Send button not found. Trying Enter key...');
inputEl.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
await sleep(500);
} else {
console.log('🚀 Sending prompt...');
sendBtn.click();
}
await sleep(1000);
// Wait for response
if (CONFIG.waitForResponse) {
const response = await waitForResponse(CONFIG.maxWaitMs);
if (response) {
console.log('\n━━━ 🤖 GROK RESPONSE ━━━');
console.log(response);
console.log('━'.repeat(50));
console.log(`\n📊 Response stats:`);
console.log(` Length: ${response.length} characters`);
console.log(` Words: ~${response.split(/\s+/).length}`);
console.log(` Time: ${new Date().toLocaleString()}`);
try {
await navigator.clipboard.writeText(response);
console.log('📋 Response copied to clipboard!');
} catch (e) {}
} else {
console.log('❌ No response received within timeout.');
console.log(` Try increasing CONFIG.maxWaitMs (currently ${CONFIG.maxWaitMs}ms).`);
console.log(' Or check if Grok is available in your region/account.');
}
} else {
console.log('⏭️ Not waiting for response (CONFIG.waitForResponse = false).');
console.log(' Check the Grok chat for the reply.');
}
console.log('\n✅ Grok integration complete.');
};
run();
})();