-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
703 lines (636 loc) · 28.3 KB
/
Copy pathcontent.js
File metadata and controls
703 lines (636 loc) · 28.3 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// content.js — DOM Scraper + Send-to-AI button for Claude.ai
// ── Code-block helpers ────────────────────────────────────────────────────────
function extractCodeBlocks(text) {
const blocks = [];
let idx = 0;
const stripped = text.replace(/```[\s\S]*?```/g, (match) => {
const placeholder = `__CODE_BLOCK_${idx++}__`;
blocks.push({ placeholder, code: match });
return placeholder;
});
return { stripped, blocks };
}
function restoreCodeBlocks(text, blocks) {
let result = text;
for (const { placeholder, code } of blocks) {
result = result.replace(placeholder, code);
}
return result;
}
// ── Single-message compressor ─────────────────────────────────────────────────
async function compressMessage(message) {
const { content, type } = message;
if (!content || content.length < 120) return { ...message, compressed: false };
const { stripped, blocks } = extractCodeBlocks(content);
const proseOnly = stripped.replace(/__CODE_BLOCK_\d+__/g, "").trim();
if (!proseOnly) return { ...message, compressed: false };
return new Promise((resolve) => {
chrome.runtime.sendMessage(
{ action: "compressMessage", type, content: stripped },
(response) => {
if (chrome.runtime.lastError || !response?.ok || !response?.compressed) {
resolve({ ...message, compressed: false });
return;
}
const compressedContent = restoreCodeBlocks(response.compressed, blocks);
resolve({ ...message, content: compressedContent, compressed: true });
}
);
});
}
// ── Conversation compressor ───────────────────────────────────────────────────
async function compressConversation(messages) {
const userMessages = messages.filter((m) => m.type === "user").slice(-5);
const assistantMessages = messages.filter((m) => m.type === "assistant").slice(-5);
const keptUserSet = new Set(userMessages.map((m) => m.timestamp + m.content.slice(0, 40)));
const keptAssistantSet = new Set(assistantMessages.map((m) => m.timestamp + m.content.slice(0, 40)));
const kept = messages.filter((m) => {
const key = m.timestamp + m.content.slice(0, 40);
return m.type === "user" ? keptUserSet.has(key) : keptAssistantSet.has(key);
});
return await Promise.all(kept.map((msg) => compressMessage(msg)));
}
// ── Capsule ───────────────────────────────────────────────────────────────────
const Capsule = {
build(messages, url) {
const title = this._inferTitle(messages, url);
return {
id: `conv_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`,
title, url, messages,
savedAt: new Date().toISOString(),
source: "claude",
version: 1,
};
},
_inferTitle(messages, url) {
const firstUser = messages.find((m) => m.type === "user");
if (firstUser && firstUser.content.length > 0) {
return firstUser.content.substring(0, 60).replace(/\n/g, " ").trim() +
(firstUser.content.length > 60 ? "…" : "");
}
try {
const u = new URL(url);
const parts = u.pathname.split("/").filter(Boolean);
return parts[parts.length - 1] || "Untitled Conversation";
} catch { return "Untitled Conversation"; }
},
};
// ── Storage ───────────────────────────────────────────────────────────────────
const STORAGE_KEY = "claude_conversations";
const MAX_CONVERSATIONS = 50;
function storageGetAll() {
return new Promise((resolve) => {
chrome.storage.local.get([STORAGE_KEY], (result) => {
if (chrome.runtime.lastError) { resolve([]); return; }
resolve(result[STORAGE_KEY] || []);
});
});
}
async function storageSave(conversation) {
const all = await storageGetAll();
const last = all[all.length - 1];
if (last && last.url === conversation.url && last.messages.length === conversation.messages.length) {
all[all.length - 1] = { ...conversation, id: last.id, savedAt: last.savedAt };
} else {
all.push(conversation);
}
const trimmed = all.slice(-MAX_CONVERSATIONS);
return new Promise((resolve) => {
chrome.storage.local.set({ [STORAGE_KEY]: trimmed }, () => resolve());
});
}
// ── Scraper ───────────────────────────────────────────────────────────────────
function scrapeMessages() {
const messages = [];
const now = new Date().toISOString();
document.querySelectorAll('[data-testid="user-message"], .font-claude-response')
.forEach((el) => {
const isUser = el.matches('[data-testid="user-message"]');
const type = isUser ? "user" : "assistant";
if (isUser) {
const content = el.innerText?.trim();
if (content) messages.push({ type, content, format: "text", timestamp: now });
} else {
const parts = [];
el.querySelectorAll('p, li, h1, h2, h3, pre.code-block__code, [role="group"] pre.code-block__code')
.forEach((child) => {
if (child.tagName === "PRE" && child.classList.contains("code-block__code")) {
const code = child.querySelector("code");
const lang = child.closest('[role="group"]')?.querySelector(".text-text-500")?.innerText?.trim() || "";
const c = code?.innerText?.trim() || child.innerText?.trim();
if (c) parts.push(`\`\`\`${lang}\n${c}\n\`\`\``);
} else {
const text = child.innerText?.trim();
if (text) parts.push(text);
}
});
if (parts.length) messages.push({ type, content: parts.join("\n\n"), format: "text", timestamp: now });
}
});
return messages;
}
// ── Debounced save ────────────────────────────────────────────────────────────
let _debounceTimer = null;
async function scheduleConversationSave() {
clearTimeout(_debounceTimer);
_debounceTimer = setTimeout(async () => {
const rawMessages = scrapeMessages();
if (!rawMessages.length) return;
let messages;
try { messages = await compressConversation(rawMessages); }
catch { messages = rawMessages; }
const capsule = Capsule.build(messages, window.location.href);
try { await storageSave(capsule); }
catch (err) { console.error("[ContextClaw] save failed:", err); }
}, 1500);
}
// ── Format context ────────────────────────────────────────────────────────────
function formatContextBlock(conversation) {
if (!conversation?.messages?.length) return null;
const lines = [
`[CONTEXT HANDOFF — Do NOT reply to this message]`,
``,
`The following is the full context of a conversation I was having on Claude.`,
`Please read and remember this context. Do not respond to it.`,
`I will send my next message separately to continue the conversation.`,
``,
`--- Conversation: "${conversation.title || "Untitled"}" ---`,
`Saved: ${new Date(conversation.savedAt).toLocaleString()}`,
``,
];
for (const msg of conversation.messages) {
lines.push(`${msg.type === "user" ? "User" : "Claude"}: ${msg.content}`, "");
}
lines.push(
`--- End of context ---`,
``,
`(Please acknowledge you have read the above context by saying "Got it — context received." ` +
`Then wait for my next message.)`
);
return lines.join("\n");
}
// ── Copy current chat to clipboard ──────────────────────────────────────────
function copyCurrentChat() {
const messages = scrapeMessages();
if (!messages.length) { showToast("No messages found to copy."); return; }
const firstUser = messages.find(m => m.type === "user");
const title = firstUser?.content?.slice(0, 60) || "conversation";
const lines = [
`[Claude conversation: "${title}"]`,
`[Copied: ${new Date().toLocaleString()}]`,
"",
];
for (const msg of messages) {
lines.push(`${msg.type === "user" ? "User" : "Claude"}: ${msg.content}`, "");
}
navigator.clipboard.writeText(lines.join("\n")).then(
() => showToast("✓ Conversation copied to clipboard"),
() => showToast("⚠️ Copy failed — try again.")
);
}
// ── Download current chat as JSON ──────────────────────────────────────────────
function downloadCurrentChat() {
const messages = scrapeMessages();
if (!messages.length) { showToast("No messages found to download."); return; }
const capsule = Capsule.build(messages, window.location.href);
const blob = new Blob([JSON.stringify(capsule, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `${capsule.title.replace(/[^\w\d]+/g, "_").slice(0, 50)}.json`;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
showToast("✓ Downloading JSON…");
}
// ── Send to AI ────────────────────────────────────────────────────────────────
async function sendToAI(target) {
const all = await storageGetAll();
if (!all.length) { showToast("⚠️ No saved conversations. Click Refresh in the popup first."); return; }
const conversation = all.slice().reverse().find(c => c.url === window.location.href) || all[all.length - 1];
const ctx = formatContextBlock(conversation);
if (!ctx) { showToast("No messages to send."); return; }
chrome.runtime.sendMessage({ action: "openAIWithContext", target, context: ctx }, (res) => {
const names = { claude: "Claude", gemini: "Gemini", chatgpt: "ChatGPT", deepseek: "DeepSeek" };
showToast(res?.ok ? `↗ Opening ${names[target]}…` : "Failed to open tab.");
});
}
// ── Toast ─────────────────────────────────────────────────────────────────────
function showToast(msg) {
document.getElementById("cc-toast")?.remove();
const t = document.createElement("div");
t.id = "cc-toast";
t.textContent = msg;
Object.assign(t.style, {
position: "fixed", bottom: "90px", left: "50%",
transform: "translateX(-50%) translateY(6px)",
background: "#18181b", color: "#fafafa",
fontFamily: "system-ui, sans-serif", fontSize: "13px", fontWeight: "500",
padding: "8px 16px", borderRadius: "999px",
border: "1px solid #3f3f46", boxShadow: "0 4px 20px rgba(0,0,0,0.4)",
zIndex: "2147483647", pointerEvents: "none", opacity: "0",
transition: "opacity 0.2s, transform 0.2s", whiteSpace: "nowrap",
});
document.body.appendChild(t);
requestAnimationFrame(() => { t.style.opacity = "1"; t.style.transform = "translateX(-50%) translateY(0)"; });
setTimeout(() => { t.style.opacity = "0"; setTimeout(() => t.remove(), 250); }, 2800);
}
// ═══════════════════════════════════════════════════════════════════════════════
// ── BUTTON INJECTION ──────────────────────────────────────────────────────────
// ═══════════════════════════════════════════════════════════════════════════════
const CC_BTN_ID = "cc-ask-ai-btn";
const CC_PANEL_ID = "cc-ask-ai-panel";
const AI_OPTIONS = [
{
id: "gemini", label: "Gemini",
color: "#4285F4", bg: "rgba(66,133,244,0.12)",
svg: `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12 2C9.5 7.5 6.5 9.5 2 12c4.5 2.5 7.5 4.5 10 10 2.5-5.5 5.5-7.5 10-10-4.5-2.5-7.5-4.5-10-10z" fill="#4285F4"/>
</svg>`,
},
{
id: "chatgpt", label: "ChatGPT",
color: "#10a37f", bg: "rgba(16,163,127,0.12)",
svg: `<svg width="14" height="14" viewBox="0 0 24 24" fill="#10a37f"><circle cx="12" cy="12" r="10"/><path d="M8 12h8M12 8v8" stroke="white" stroke-width="2" stroke-linecap="round"/></svg>`,
},
{
id: "deepseek", label: "DeepSeek",
color: "#1A6BFF", bg: "rgba(26,107,255,0.12)",
svg: `<svg width="14" height="14" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="#1A6BFF"/><circle cx="12" cy="12" r="5" fill="white"/><circle cx="12" cy="12" r="2.5" fill="#1A6BFF"/></svg>`,
},
];
function ensureStyles() {
if (document.getElementById("cc-styles")) return;
const s = document.createElement("style");
s.id = "cc-styles";
s.textContent = `
#cc-ask-ai-btn {
display: inline-flex !important;
align-items: center !important;
gap: 5px !important;
padding: 0 10px !important;
height: 32px !important;
background: transparent !important;
border: 1px solid hsl(var(--border-300) / 0.45) !important;
border-radius: 8px !important;
color: hsl(var(--text-400)) !important;
cursor: pointer !important;
font-family: inherit !important;
font-size: 12px !important;
font-weight: 600 !important;
transition: background 0.15s, border-color 0.15s, color 0.15s !important;
flex-shrink: 0 !important;
white-space: nowrap !important;
outline: none !important;
box-shadow: none !important;
}
#cc-ask-ai-btn:hover {
background: hsl(var(--bg-200)) !important;
border-color: hsl(var(--border-200)) !important;
color: hsl(var(--text-200)) !important;
}
#cc-ask-ai-btn.active {
background: hsl(var(--bg-200)) !important;
border-color: hsl(var(--border-200)) !important;
color: hsl(var(--text-100)) !important;
}
#cc-ask-ai-panel {
position: fixed !important;
background: hsl(var(--bg-000)) !important;
border: 1px solid hsl(var(--border-300) / 0.5) !important;
border-radius: 14px !important;
padding: 8px !important;
box-shadow: 0 8px 32px rgba(0,0,0,0.14), 0 0 0 1px rgba(0,0,0,0.03) !important;
z-index: 2147483647 !important;
display: none !important;
flex-direction: column !important;
gap: 3px !important;
min-width: 205px !important;
font-family: inherit !important;
}
#cc-ask-ai-panel.cc-open {
display: flex !important;
animation: cc-pop 0.16s cubic-bezier(0.16,1,0.3,1) !important;
}
@keyframes cc-pop {
from { opacity:0; transform:translateY(6px) scale(0.97); }
to { opacity:1; transform:translateY(0) scale(1); }
}
.cc-panel-hdr {
font-size: 10px !important;
font-weight: 700 !important;
color: hsl(var(--text-500)) !important;
text-transform: uppercase !important;
letter-spacing: 0.8px !important;
padding: 3px 8px 8px !important;
border-bottom: 1px solid hsl(var(--border-300) / 0.35) !important;
margin-bottom: 2px !important;
}
.cc-ai-opt {
display: flex !important;
align-items: center !important;
gap: 10px !important;
width: 100% !important;
padding: 8px 10px !important;
background: transparent !important;
border: none !important;
border-radius: 8px !important;
cursor: pointer !important;
font-family: inherit !important;
transition: background 0.12s, transform 0.1s !important;
text-align: left !important;
}
.cc-ai-opt:hover { background: hsl(var(--bg-100)) !important; transform: translateX(2px) !important; }
.cc-ai-opt:active { transform: scale(0.97) !important; }
.cc-ai-ico {
width: 28px !important; height: 28px !important;
border-radius: 8px !important;
display: flex !important; align-items: center !important; justify-content: center !important;
flex-shrink: 0 !important;
}
.cc-ai-lbl { font-size: 13px !important; font-weight: 600 !important; color: hsl(var(--text-100)) !important; display:block !important; }
.cc-ai-sub { font-size: 10px !important; color: hsl(var(--text-500)) !important; display:block !important; margin-top:1px !important; }
.cc-arr { margin-left: auto !important; color: hsl(var(--text-500)) !important; flex-shrink: 0 !important; }
.cc-divider { height: 1px !important; background: hsl(var(--border-300) / 0.35) !important; margin: 3px 0 !important; }
.cc-action-row {
display: flex !important;
gap: 4px !important;
padding: 2px !important;
}
.cc-action-btn {
flex: 1 !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
gap: 6px !important;
padding: 7px 10px !important;
background: transparent !important;
border: 1px solid hsl(var(--border-300) / 0.4) !important;
border-radius: 8px !important;
color: hsl(var(--text-400)) !important;
cursor: pointer !important;
font-family: inherit !important;
font-size: 11px !important;
font-weight: 500 !important;
transition: background 0.12s, color 0.12s, border-color 0.12s !important;
white-space: nowrap !important;
}
.cc-action-btn:hover {
background: hsl(var(--bg-100)) !important;
border-color: hsl(var(--border-200)) !important;
color: hsl(var(--text-200)) !important;
}
.cc-action-btn:active { opacity: 0.75 !important; }
`;
document.head.appendChild(s);
}
// ── Find exact injection point from real Claude DOM ───────────────────────────
// Claude toolbar structure:
// <div class="relative flex gap-2 w-full items-center"> ← toolbar row
// <div class="relative flex-1 flex items-center shrink ..."> ← left side
// <div> ← wraps + button
// <button aria-label="Add files, connectors, and more"> ← the + btn
// <div class="flex flex-row items-center min-w-0 gap-1"> ← chip slot (inject here)
// <div> ← model selector
// <div> ← voice btn
function findSlot() {
// 1. Anchor on the + button — most stable identifier
const plusBtn = document.querySelector('button[aria-label="Add files, connectors, and more"]');
if (!plusBtn) return null;
// 2. Its grandparent is the flex-1 container. Find the chip slot inside it.
const flexContainer = plusBtn.closest('.relative.flex-1') || plusBtn.parentElement?.parentElement;
if (!flexContainer) return null;
// 3. The chip slot is a flex row inside that container (after the + button wrapper)
const chipSlot = flexContainer.querySelector('.flex.flex-row.items-center.min-w-0.gap-1');
if (chipSlot) return chipSlot;
// 4. Fallback: insert right after the + button's wrapper div
const plusWrapper = plusBtn.parentElement;
return plusWrapper?.parentElement || null;
}
function buildPanel() {
const panel = document.createElement("div");
panel.id = CC_PANEL_ID;
const hdr = document.createElement("div");
hdr.className = "cc-panel-hdr";
hdr.textContent = "Send context to";
panel.appendChild(hdr);
for (const ai of AI_OPTIONS) {
const opt = document.createElement("button");
opt.className = "cc-ai-opt";
opt.type = "button";
opt.innerHTML = `
<span class="cc-ai-ico" style="background:${ai.bg}">${ai.svg}</span>
<span>
<span class="cc-ai-lbl">${ai.label}</span>
<span class="cc-ai-sub">Open & inject context</span>
</span>
<svg class="cc-arr" width="11" height="11" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
<line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/>
</svg>
`;
opt.addEventListener("mousedown", (e) => {
e.preventDefault(); // prevent input blur
closePanel();
sendToAI(ai.id);
});
panel.appendChild(opt);
}
// ── Copy + Download inline row ──────────────────────────────────────────
const divider = document.createElement("div");
divider.className = "cc-divider";
panel.appendChild(divider);
const actionRow = document.createElement("div");
actionRow.className = "cc-action-row";
const copyBtn = document.createElement("button");
copyBtn.className = "cc-action-btn";
copyBtn.type = "button";
copyBtn.title = "Copy conversation to clipboard";
copyBtn.innerHTML = `
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"/>
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>
</svg>
Copy
`;
copyBtn.addEventListener("mousedown", (e) => {
e.preventDefault();
closePanel();
copyCurrentChat();
});
const dlBtn = document.createElement("button");
dlBtn.className = "cc-action-btn";
dlBtn.type = "button";
dlBtn.title = "Download conversation as JSON";
dlBtn.innerHTML = `
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 3v12"/><path d="m7 10 5 5 5-5"/>
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
</svg>
Download
`;
dlBtn.addEventListener("mousedown", (e) => {
e.preventDefault();
closePanel();
downloadCurrentChat();
});
actionRow.appendChild(copyBtn);
actionRow.appendChild(dlBtn);
panel.appendChild(actionRow);
document.body.appendChild(panel);
return panel;
}
let _panel = null;
let _panelOpen = false;
function openPanel(btn) {
if (!_panel) _panel = buildPanel();
const r = btn.getBoundingClientRect();
const panelH = 185;
if (r.top > panelH + 10) {
_panel.style.bottom = `${window.innerHeight - r.top + 8}px`;
_panel.style.top = "auto";
} else {
_panel.style.top = `${r.bottom + 8}px`;
_panel.style.bottom = "auto";
}
_panel.style.left = `${Math.min(r.left, window.innerWidth - 215)}px`;
_panel.classList.add("cc-open");
btn.classList.add("active");
_panelOpen = true;
}
function closePanel() {
_panel?.classList.remove("cc-open");
document.getElementById(CC_BTN_ID)?.classList.remove("active");
_panelOpen = false;
}
function injectButton() {
if (document.getElementById(CC_BTN_ID)) return;
ensureStyles();
const slot = findSlot();
if (!slot) return;
const btn = document.createElement("button");
btn.id = CC_BTN_ID;
btn.type = "button";
btn.title = "Send conversation context to another AI";
btn.innerHTML = `
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
<line x1="22" y1="2" x2="11" y2="13"/>
<polygon points="22 2 15 22 11 13 2 9 22 2"/>
</svg>
Export
`;
btn.addEventListener("click", (e) => {
e.stopPropagation();
_panelOpen ? closePanel() : openPanel(btn);
});
document.addEventListener("click", (e) => {
if (_panelOpen && !_panel?.contains(e.target) && e.target !== btn) closePanel();
});
// Insert into chip slot
slot.appendChild(btn);
}
// ── Message listener ──────────────────────────────────────────────────────────
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.action === "scrapeNow") { scheduleConversationSave(); sendResponse({ ok: true }); }
if (message.action === "ping") { sendResponse({ ok: true, url: window.location.href }); }
return false;
});
// ── MutationObserver ──────────────────────────────────────────────────────────
let _observer = null;
function startObserver() {
if (_observer) return;
_observer = new MutationObserver((mutations) => {
if (mutations.some((m) => m.addedNodes.length > 0 || m.type === "characterData")) {
scheduleConversationSave();
if (!document.getElementById(CC_BTN_ID)) setTimeout(injectButton, 300);
}
});
_observer.observe(document.body, { childList: true, subtree: true, characterData: true });
}
// ── Init ──────────────────────────────────────────────────────────────────────
function isConversationPage() {
return (
/\/chat\/|\/c\/|\/conversation/.test(window.location.pathname) ||
/\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.test(window.location.pathname)
);
}
let _injectAttempts = 0;
function retryInject() {
if (document.getElementById(CC_BTN_ID)) return;
if (_injectAttempts++ > 30) return;
injectButton();
if (!document.getElementById(CC_BTN_ID)) setTimeout(retryInject, 400);
}
// ── Pending context injection (receive context from other AIs) ────────────────
function checkAndInjectPendingContext() {
try {
chrome.storage.session.get(["pending_context_inject"], (result) => {
const pending = result["pending_context_inject"];
if (!pending) return;
if (pending.target !== "claude" || Date.now() - pending.ts > 60000) return;
chrome.storage.session.remove(["pending_context_inject"]);
const context = pending.context;
let attempts = 0;
const maxAttempts = 20;
const poll = setInterval(() => {
attempts++;
const el = document.querySelector('div[data-testid="chat-input"][contenteditable="true"]');
if (el) {
clearInterval(poll);
el.focus();
document.execCommand("selectAll", false, null);
document.execCommand("delete", false, null);
document.execCommand("insertText", false, context);
el.dispatchEvent(new Event("input", { bubbles: true }));
// Show success banner
showToast("✓ Context injected — review and send!");
} else if (attempts >= maxAttempts) {
clearInterval(poll);
showToast("⚠️ Could not find Claude input field.");
}
}, 500);
});
} catch (e) {
// chrome.storage.session may not be available in all contexts — silently fail
}
}
function init() {
checkAndInjectPendingContext();
if (!isConversationPage()) {
let last = window.location.href;
const poll = setInterval(() => {
if (window.location.href !== last) {
last = window.location.href;
if (isConversationPage()) { clearInterval(poll); startObserver(); scheduleConversationSave(); _injectAttempts = 0; retryInject(); }
}
}, 800);
return;
}
startObserver();
scheduleConversationSave();
_injectAttempts = 0;
retryInject();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
// SPA nav
let _lastHref = window.location.href;
const _navObserver = new MutationObserver(() => {
if (window.location.href !== _lastHref) {
_lastHref = window.location.href;
if (_observer) { _observer.disconnect(); _observer = null; }
document.getElementById(CC_BTN_ID)?.remove();
document.getElementById(CC_PANEL_ID)?.remove();
_panel = null; _panelOpen = false;
init();
}
});
_navObserver.observe(document.documentElement, { childList: true, subtree: false });
window.__contextClaw = { scrapeNow: scheduleConversationSave, scrapeMessages };