-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpopup.js
More file actions
203 lines (167 loc) · 6.08 KB
/
Copy pathpopup.js
File metadata and controls
203 lines (167 loc) · 6.08 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
const form = document.getElementById("reply-form");
const threadInput = document.getElementById("thread");
const roleSelect = document.getElementById("role");
const toneSelect = document.getElementById("tone");
const modeSelect = document.getElementById("mode");
const newMessageInput = document.getElementById("new-message");
const newMessageLabel = document.getElementById("new-message-label");
const repliesContainer = document.getElementById("replies-container");
const statusDiv = document.getElementById("status");
const generateBtn = document.getElementById("generate-btn");
// Show/hide new message textarea based on mode
modeSelect.addEventListener("change", () => {
if (modeSelect.value === "analyze" || modeSelect.value === "both") {
newMessageInput.classList.remove("hidden");
newMessageLabel.classList.remove("hidden");
newMessageInput.required = true;
} else {
newMessageInput.classList.add("hidden");
newMessageLabel.classList.add("hidden");
newMessageInput.required = false;
newMessageInput.value = "";
}
});
// Copy button event delegation (added once)
repliesContainer.addEventListener("click", async (e) => {
if (e.target.classList.contains("copy-btn")) {
const btn = e.target;
const replyCard = btn.closest(".reply-card");
const replyText = replyCard.innerText.replace("Copy 📋", "").trim();
try {
await navigator.clipboard.writeText(replyText);
btn.textContent = "Copied! 📋";
setTimeout(() => {
btn.textContent = "Copy 📋";
}, 2000);
} catch {
btn.textContent = "Failed to copy";
setTimeout(() => {
btn.textContent = "Copy 📋";
}, 2000);
}
}
});
function autoResizeTextarea(textarea) {
textarea.style.height = "auto"; // reset height
textarea.style.height = textarea.scrollHeight + "px"; // set height based on scrollHeight
}
// For #thread textarea
threadInput.addEventListener("input", () => autoResizeTextarea(threadInput));
// For #new-message textarea
newMessageInput.addEventListener("input", () => autoResizeTextarea(newMessageInput))
autoResizeTextarea(threadInput);
autoResizeTextarea(newMessageInput);
// Form submission
form.addEventListener("submit", async (e) => {
e.preventDefault();
const geminiApiKey = await new Promise((resolve) => {
chrome.storage.local.get("geminiApiKey", (result) => {
resolve(result.geminiApiKey || "");
});
});
repliesContainer.innerHTML = "";
statusDiv.textContent = "Generating replies... ⏳";
generateBtn.disabled = true;
const payload = {
thread: threadInput.value.trim(),
role: roleSelect.value,
tone: toneSelect.value,
mode: modeSelect.value,
api_key: geminiApiKey
};
if (modeSelect.value === "analyze" || modeSelect.value === "both") {
payload.new_message = newMessageInput.value.trim();
}
if (!geminiApiKey) {
statusDiv.textContent = "❌ Gemini API key is missing. Please set it in settings.";
return;
}
try {
const response = await fetch("https://threadwise-ai-backend.onrender.com/generate-replies", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!response.ok) {
statusDiv.textContent = `❌ Failed to generate replies. Server responded with status ${response.status}.`;
generateBtn.disabled = false;
return;
}
const data = await response.json();
let replies = [];
try {
if (typeof data.reply === "string") {
replies = JSON.parse(data.reply);
} else {
replies = data.reply;
}
} catch {
replies = [data.reply];
}
if (!replies.length) {
statusDiv.textContent = "No replies generated.";
generateBtn.disabled = false;
return;
}
statusDiv.textContent = `✅ Generated ${replies.length} reply${replies.length > 1 ? "ies" : "y"}:`;
repliesContainer.innerHTML = replies
.map(
(r) => `
<div class="reply-card" tabindex="0" role="region" aria-label="Generated reply">
${r.replace(/\n/g, "<br>")}
<button class="copy-btn" aria-label="Copy reply to clipboard">Copy 📋</button>
</div>`
)
.join("");
} catch (error) {
if (error instanceof TypeError && error.message.includes("Failed to fetch")) {
statusDiv.textContent = "❌ Invalid API Key || Turn on the Internet";
} else {
statusDiv.textContent = `❌ Unexpected error: ${error.message}`;
}
}
generateBtn.disabled = false;
});
const mainTabBtn = document.getElementById("generate-tab-btn");
const settingsTabBtn = document.getElementById("settings-tab-btn");
const mainTab = document.getElementById("generate-tab");
const settingsTab = document.getElementById("settings-tab");
if (mainTabBtn && settingsTabBtn && mainTab && settingsTab) {
function switchTab(to) {
if (to === "main") {
mainTab.classList.remove("hidden");
settingsTab.classList.add("hidden");
mainTabBtn.classList.add("active");
settingsTabBtn.classList.remove("active");
} else {
mainTab.classList.add("hidden");
settingsTab.classList.remove("hidden");
settingsTabBtn.classList.add("active");
mainTabBtn.classList.remove("active");
}
}
mainTabBtn.addEventListener("click", () => switchTab("main"));
settingsTabBtn.addEventListener("click", () => switchTab("settings"));
}
const saveBtn = document.getElementById("save-api-key");
const apiInput = document.getElementById("gemini-api-key");
const apiStatus = document.getElementById("api-status");
saveBtn.addEventListener("click", () => {
const key = apiInput.value.trim();
if (!key || key.length < 15) {
apiStatus.textContent = "❌ Please enter a valid API key (min 15 characters) .";
apiStatus.classList.add("error");
return;
}
chrome.storage.local.set({ geminiApiKey: key }, () => {
apiStatus.textContent = "✅ API key saved!";
apiStatus.classList.remove("error");
});
});
chrome.storage.local.get("geminiApiKey", (result) => {
if (result.geminiApiKey) {
apiInput.value = result.geminiApiKey;
}
});