-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
111 lines (91 loc) · 4.04 KB
/
Copy pathbackground.js
File metadata and controls
111 lines (91 loc) · 4.04 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
// background.js
const STORAGE_KEY = "claude_conversations";
const PENDING_INJECT_KEY = "pending_context_inject";
const GEMINI_API_KEY = ""; // keep as is
const GEMINI_API_URL =
"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent";
// ── Gemini API call ─────────────────────────────────────────────
async function callGemini(systemPrompt, userPrompt) {
if (!GEMINI_API_KEY) {
throw new Error("No Gemini API key");
}
const res = await fetch(`${GEMINI_API_URL}?key=${GEMINI_API_KEY}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
systemInstruction: { parts: [{ text: systemPrompt }] },
contents: [{ role: "user", parts: [{ text: userPrompt }] }],
generationConfig: { temperature: 0.1, maxOutputTokens: 1024 },
}),
});
if (!res.ok) {
const errText = await res.text();
throw new Error(errText);
}
const data = await res.json();
return data?.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || null;
}
// ── AI target URLs ──────────────────────────────────────────────
const AI_URLS = {
claude: "https://claude.ai/new",
gemini: "https://gemini.google.com/app",
chatgpt: "https://chatgpt.com/",
deepseek: "https://chat.deepseek.com/",
};
// ── Message listener ────────────────────────────────────────────
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.action) {
// ── Compress a single message via Gemini ──────────────────────
case "compressMessage": {
const { type, content } = message;
const systemPrompt =
type === "assistant"
? "Compress assistant message (technical, lossless)."
: "Compress user message (preserve intent).";
callGemini(systemPrompt, content)
.then((compressed) => {
sendResponse({ ok: true, compressed: compressed || content });
})
.catch((err) => {
sendResponse({ ok: false, compressed: null, error: err.message });
});
return true;
}
// ── Scrape active tab (any supported AI site) ─────────────────
case "scrapeActiveTab": {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
if (!tab?.url) { sendResponse({ ok: false, error: "No active tab" }); return; }
const supported = ["claude.ai", "gemini.google.com", "chatgpt.com", "chat.deepseek.com"];
const isSupported = supported.some(h => tab.url.includes(h));
if (!isSupported) { sendResponse({ ok: false, error: "Unsupported site" }); return; }
chrome.tabs.sendMessage(tab.id, { action: "scrapeNow" }, (response) => {
if (chrome.runtime.lastError) {
sendResponse({ ok: false, error: chrome.runtime.lastError.message });
return;
}
sendResponse({ ok: true, response });
});
});
return true;
}
// ── Open target AI with context injected ─────────────────────
case "openAIWithContext": {
const { target, context } = message;
if (!AI_URLS[target]) {
sendResponse({ ok: false, error: "Unknown AI target" });
return true;
}
// Store the pending context so the injector content script can pick it up
// Using chrome.storage.local (not .session) for reliable cross-context access in MV3
chrome.storage.local.set({ [PENDING_INJECT_KEY]: { target, context, ts: Date.now() } }, () => {
chrome.tabs.create({ url: AI_URLS[target] }, (tab) => {
sendResponse({ ok: true, tabId: tab.id });
});
});
return true;
}
default:
break;
}
});