-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
415 lines (326 loc) · 11.9 KB
/
Copy pathcontent.js
File metadata and controls
415 lines (326 loc) · 11.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// AtlasChat content script
// v1.10: Integrated structured PME responses, enforced Guard Mode behavior, and improved response handling
// NOTE: Guard-mode wrapper branding updated in background.js (no 'PME' in user-facing output).
//
// Responsibilities:
// - Only activate on ChatGPT domains
// - Capture Enter (no Shift) at CAPTURE phase to govern sends
// - In Guard mode: wrap user text with the Guard prompt wrapper (handled by background.js)
// - In Direct mode: send exactly what user typed (handled by background.js)
// Safety: If anything fails, we fall back and do not break the page.
const ATLAS_ENABLED_KEY = "atlasChatEnabled";
const ATLAS_MODE_KEY = "atlasChatMode";
let atlasChatEnabled = false;
let atlasChatMode = "guard";
// Runtime state (must be releasable / idempotent)
let promptEl = null;
let isProcessing = false;
let skipNextIntercept = false;
let promptWatchTimer = null;
let promptWatchAttempts = 0;
let keyListenerAttached = false;
let routeWatcherInstalled = false;
// Hard timeout (ms) for background processing; prevents "stuck in Guard/markdown lock"
const REWRITE_TIMEOUT_MS = 2500;
(function bootstrap() {
const host = window.location.host || "";
const isChatGPTHost = host.includes("chat.openai.com") || host.includes("chatgpt.com");
if (!isChatGPTHost) return;
console.log("[AtlasChat] v1.10 content LOADED on:", window.location.href);
// SPA route watcher (ChatGPT navigates without full reload)
installRouteWatcher();
// Load initial state, then init
chrome.storage.local.get([ATLAS_ENABLED_KEY, ATLAS_MODE_KEY], (result) => {
atlasChatEnabled = Boolean(result[ATLAS_ENABLED_KEY]);
atlasChatMode = result[ATLAS_MODE_KEY] === "direct" ? "direct" : "guard";
initOrRelease("bootstrap");
});
// React to state changes
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== "local") return;
const enabledChanged = Boolean(changes[ATLAS_ENABLED_KEY]);
const modeChanged = Boolean(changes[ATLAS_MODE_KEY]);
if (enabledChanged) atlasChatEnabled = Boolean(changes[ATLAS_ENABLED_KEY].newValue);
if (modeChanged) atlasChatMode = changes[ATLAS_MODE_KEY].newValue === "direct" ? "direct" : "guard";
// Any meaningful change: hard release then re-init as needed.
initOrRelease("storage-change");
});
})();
function initOrRelease(reason) {
// Always start clean; prevents stacked listeners/observers or hung state across refreshes.
release(reason);
if (!atlasChatEnabled) {
console.log("[AtlasChat] Disabled. Released hooks. Reason:", reason);
return;
}
// Attach capture listener (fires before ChatGPT/ProseMirror handlers)
if (!keyListenerAttached) {
document.addEventListener("keydown", onDocumentKeyDownCapture, true);
keyListenerAttached = true;
}
startPromptWatcher();
console.log("[AC:INIT] Enabled. Mode:", atlasChatMode, "Reason:", reason);
}
function release(reason) {
// Idempotent: safe to call repeatedly.
try {
// Stop prompt watcher interval
if (promptWatchTimer) {
window.clearInterval(promptWatchTimer);
promptWatchTimer = null;
promptWatchAttempts = 0;
}
// Detach key listener
if (keyListenerAttached) {
document.removeEventListener("keydown", onDocumentKeyDownCapture, true);
keyListenerAttached = false;
}
// Reset runtime flags
promptEl = null;
isProcessing = false;
skipNextIntercept = false;
// Optional: you can log with reason for diagnostics
if (reason) console.log("[AC:RELEASE] complete. Reason:", reason);
} catch (e) {
console.warn("[AtlasChat] RELEASE error (non-fatal):", e);
}
}
function installRouteWatcher() {
if (routeWatcherInstalled) return;
routeWatcherInstalled = true;
const onRouteChange = () => {
// Route changes can replace the composer DOM node; release + re-init prevents ghost hooks.
initOrRelease("route-change");
};
// Patch history methods (best-effort; do not break page if it fails)
try {
const _pushState = history.pushState;
history.pushState = function (...args) {
const ret = _pushState.apply(this, args);
window.setTimeout(onRouteChange, 0);
return ret;
};
const _replaceState = history.replaceState;
history.replaceState = function (...args) {
const ret = _replaceState.apply(this, args);
window.setTimeout(onRouteChange, 0);
return ret;
};
} catch (e) {
console.warn("[AtlasChat] Could not patch history (non-fatal):", e);
}
window.addEventListener("popstate", () => window.setTimeout(onRouteChange, 0), false);
}
function startPromptWatcher() {
const intervalMs = 500;
const maxAttempts = 120; // 60 seconds
promptWatchAttempts = 0;
promptWatchTimer = window.setInterval(() => {
promptWatchAttempts += 1;
const el = findPromptElement();
if (el) {
promptEl = el;
window.clearInterval(promptWatchTimer);
promptWatchTimer = null;
console.log("[AtlasChat] Attached to prompt:", promptEl);
return;
}
if (promptWatchAttempts >= maxAttempts) {
window.clearInterval(promptWatchTimer);
promptWatchTimer = null;
console.warn("[AtlasChat] Could not locate prompt element after 60s.");
}
}, intervalMs);
}
function ensurePromptElement() {
// ChatGPT can replace the composer DOM node between sends.
// If our cached promptEl is disconnected, re-acquire it.
try {
if (!promptEl || !(promptEl.isConnected || document.contains(promptEl))) {
promptEl = findPromptElement();
}
} catch (_) {
promptEl = findPromptElement();
}
return promptEl;
}
function findPromptElement() {
// ChatGPT ProseMirror prompt
let el = document.querySelector("div#prompt-textarea[contenteditable='true']");
if (el) return el;
// Generic id fallback
el = document.querySelector("[id='prompt-textarea'][contenteditable='true']");
if (el) return el;
// Legacy textarea path
el = document.querySelector("textarea[data-testid='prompt-textarea']");
if (el) return el;
const allTextareas = document.querySelectorAll("textarea");
if (allTextareas.length > 0) return allTextareas[allTextareas.length - 1];
return null;
}
function isEventFromPrompt(event) {
if (!promptEl) return false;
const t = event.target;
if (!t) return false;
// Direct match
if (t === promptEl) return true;
// If the target is inside the prompt
if (promptEl.contains(t)) return true;
// If active element is the prompt (or inside it)
const ae = document.activeElement;
if (ae && (ae === promptEl || promptEl.contains(ae))) return true;
// If the target has a closest prompt ancestor
if (typeof t.closest === "function") {
const closest = t.closest("#prompt-textarea");
if (closest && closest === promptEl) return true;
}
return false;
}
async function onDocumentKeyDownCapture(event) {
// Only intercept Enter submit (no Shift)
if (event.key !== "Enter" || event.shiftKey) return;
// Ignore IME composition
if (event.isComposing) return;
if (skipNextIntercept) {
skipNextIntercept = false;
return;
}
if (!atlasChatEnabled) return;
try {
ensurePromptElement();
if (!promptEl) return;
if (!isEventFromPrompt(event)) return;
// Prevent ChatGPT from handling this Enter.
event.preventDefault();
event.stopPropagation();
if (typeof event.stopImmediatePropagation === "function") event.stopImmediatePropagation();
if (isProcessing) return;
const originalText = getPromptText(promptEl);
if (!originalText.trim()) return;
isProcessing = true;
console.log("[AtlasChat] Intercepted Enter. Mode:", atlasChatMode, "Text:", originalText);
// Hard timeout protection: never hang the UI in guarded mode.
const response = await withTimeout(
processWithAtlasChat(originalText, atlasChatMode),
REWRITE_TIMEOUT_MS
);
isProcessing = false;
if (!response || !response.ok) {
console.warn("[AC:PIPELINE_FAIL]", response && response.error);
// Non-blocking user signal (best-effort)
chrome.runtime.sendMessage({ type: "ATLASCHAT_PIPELINE_ERROR", error: response && response.error });
// Fail-safe: send original text (un-governed) instead of trapping the user.
setPromptText(promptEl, originalText);
window.setTimeout(() => {
skipNextIntercept = true;
triggerSend();
}, 25);
return;
}
const finalText = response.text || originalText;
console.log("[AtlasChat] Process OK. Final:", finalText);
setPromptText(promptEl, finalText);
// Let React/ProseMirror digest input before clicking send.
window.setTimeout(() => {
skipNextIntercept = true;
triggerSend();
}, 25);
} catch (err) {
isProcessing = false;
console.error("[AtlasChat] Intercept error:", err);
// Absolute fail-safe: release hooks so we never trap the page.
// (User can re-enable from popup if needed.)
initOrRelease("intercept-error");
}
}
function withTimeout(promise, ms) {
return new Promise((resolve) => {
let settled = false;
const t = window.setTimeout(() => {
if (settled) return;
settled = true;
resolve({ ok: false, error: "TIMEOUT" });
}, ms);
Promise.resolve(promise)
.then((v) => {
if (settled) return;
settled = true;
window.clearTimeout(t);
resolve(v);
})
.catch((e) => {
if (settled) return;
settled = true;
window.clearTimeout(t);
resolve({ ok: false, error: "EXCEPTION", detail: String(e && e.message ? e.message : e) });
});
});
}
function getPromptText(el) {
if (!el) return "";
if (el instanceof HTMLTextAreaElement) return el.value || "";
if (el.isContentEditable) return (el.innerText || el.textContent || "");
return el.value || el.textContent || "";
}
function setPromptText(el, text) {
if (!el) return;
// Textarea: set value + input event
if (el instanceof HTMLTextAreaElement) {
el.value = text;
el.dispatchEvent(new Event("input", { bubbles: true }));
return;
}
// ProseMirror / contenteditable: use execCommand insertText to update editor state.
if (el.isContentEditable) {
el.focus();
// Select all existing content inside the editor.
const sel = window.getSelection();
if (sel) sel.removeAllRanges();
const range = document.createRange();
range.selectNodeContents(el);
if (sel) sel.addRange(range);
// Replace with governed text.
document.execCommand("insertText", false, text);
// Fire input event (some builds still rely on it)
el.dispatchEvent(new Event("input", { bubbles: true }));
return;
}
// Fallback
if ("value" in el) el.value = text;
else el.textContent = text;
el.dispatchEvent(new Event("input", { bubbles: true }));
}
function processWithAtlasChat(text, modeOverride) {
return new Promise((resolve) => {
chrome.runtime.sendMessage(
{ type: "ATLASCHAT_PROCESS_PROMPT", text, mode: modeOverride },
(response) => {
if (chrome.runtime.lastError) {
console.error("[AtlasChat] runtime error:", chrome.runtime.lastError);
resolve({ ok: false, error: "RUNTIME_ERROR" });
return;
}
resolve(response);
}
);
});
}
function triggerSend() {
// ChatGPT can re-render the composer and buttons. Refresh references.
ensurePromptElement();
const sendButton =
document.querySelector("button[data-testid='send-button']") ||
document.querySelector("button[aria-label='Send message']") ||
document.querySelector("button[aria-label='Send']");
if (sendButton && !sendButton.disabled) {
sendButton.click();
return;
}
// Fallback: synthesize Enter on the prompt element
if (promptEl) {
const evtInit = { key: "Enter", code: "Enter", keyCode: 13, which: 13, bubbles: true };
promptEl.dispatchEvent(new KeyboardEvent("keydown", evtInit));
promptEl.dispatchEvent(new KeyboardEvent("keypress", evtInit));
promptEl.dispatchEvent(new KeyboardEvent("keyup", evtInit));
}
}