-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidePanel.js
443 lines (383 loc) · 14.8 KB
/
sidePanel.js
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
document.addEventListener("DOMContentLoaded", function () {
const chatMessages = document.getElementById("chat-messages");
const userInput = document.getElementById("user-input");
const sendBtn = document.getElementById("send-btn");
const clearChatBtn = document.getElementById("clear-chat-btn");
const settingsBtn = document.getElementById("settings-btn");
const container = document.querySelector(".container");
// Api key section open/close
settingsBtn.addEventListener("click", function () {
container.classList.toggle("expanded");
if (container.classList.contains("expanded")) {
container.style.height = "300px";
} else {
container.style.height = "60px";
}
});
// Fetch chat history from local storage and display it
chrome.storage.local.get(["chatHistory"], function (result) {
const chatHistory = result.chatHistory || [];
if (chatHistory.length > 0) {
// display the chat history
displayMessages(chatHistory);
// scroll to bottom of chat-messages div
chatMessages.scrollTop = chatMessages.scrollHeight;
} else {
// show a image of the icon and a text with "how can I help you?"
displayAssistanInfo();
}
checkClearChatBtn();
});
// focus on the input field
userInput.focus();
// disable the send button by default
sendBtn.disabled = true;
// disable the send button if the input field is empty
userInput.addEventListener("keyup", function () {
const userMessage = userInput.value.trim();
sendBtn.disabled = userMessage === "";
});
// If the user presses enter, click the send button
userInput.addEventListener("keyup", function (event) {
if (event.code === "Enter" && !event.shiftKey) {
event.preventDefault();
sendBtn.click();
}
});
// Auto-resize the input field based on the content
userInput.addEventListener("input", function () {
this.style.height = "auto"; // Reset the height
// Set the height of the input field based on the content
this.style.height = Math.min(this.scrollHeight, 100) + "px";
// Enable scrolling if the content is too long
this.style.overflowY = this.scrollHeight > 100 ? "scroll" : "auto";
});
// Send user's input to background script when the send button is clicked
sendBtn.addEventListener("click", function () {
const userMessage = userInput.value.trim();
if (userMessage !== "") {
sendMessage(userMessage);
userInput.value = ""; // Clear the input field
sendBtn.disabled = true;
sendBtn.innerHTML = '<i class="fa fa-spinner fa-pulse"></i>';
userInput.disabled = true;
}
});
// Listen for messages from the background script
chrome.runtime.onMessage.addListener(function (
message,
sender,
sendResponse
) {
if (message.answer) {
displayMessage("assistant", message.answer);
} else if (message.imageUrl) {
displayMessage("assistant", message.imageUrl);
} else if (message.error) {
displayMessage("system", message.error);
}
// Update UI elements regardless of the type of response
sendBtn.innerHTML = '<i class="fa fa-paper-plane"></i>';
sendBtn.disabled = false;
userInput.disabled = false;
});
// Function to send user's input to the background script and display it in the chat
function sendMessage(userMessage) {
const message = { userInput: userMessage };
// Send the user's message to the background script
chrome.runtime.sendMessage(message, function (response) {
if (response.error) {
displayMessage("system", response.error);
}
});
if (document.getElementById("assistant-info-wrapper")) {
hideAssistanInfo();
}
// Display the user's message in the chat
displayMessage("user", userMessage);
}
// Function to display messages in the chat
function displayMessage(role, content) {
const messageElement = document.createElement("div");
// add id to the message element
messageElement.classList.add("message");
messageElement.classList.add(role);
// check of message starts with a dall-e image URL
if (
content.startsWith("https://oaidalleapiprodscus.blob.core.windows.net/")
) {
const imageElement = document.createElement("img");
imageElement.src = content;
messageElement.appendChild(imageElement);
// add a download button to the message if it's from the assistant
if (role === "assistant") {
// create container for the action buttons
const actionBtns = document.createElement("div");
actionBtns.className = "action-btns";
// add the action buttons to the message
messageElement.appendChild(actionBtns);
const downloadIcon = document.createElement("i");
downloadIcon.className = "fa fa-download download-btn";
downloadIcon.title = "Download image";
downloadIcon.addEventListener("click", function () {
// download image to the user's device
chrome.downloads
.download({
url: content,
filename: "dall-e-image.png",
saveAs: false,
})
.then(() => {
// Change the icon to a check
downloadIcon.className = "fa fa-check action-btn";
// Revert to the default icon after 2 seconds
setTimeout(() => {
downloadIcon.className = "fa fa-download action-btn";
}, 2000);
})
// Display an x icon if the copy operation fails
.catch(() => {
downloadIcon.className = "fa fa-times action-btn";
// Revert to the default icon after 2 seconds
setTimeout(() => {
downloadIcon.className = "fa fa-download action-btn";
}, 2000);
});
});
actionBtns.appendChild(downloadIcon);
}
} else {
// if it's not an image, it's a text message
// format the message content
content = formatMessageContent(content);
// add the message content to the message element
messageElement.innerHTML = content;
// add a copy button to the message if it's from the assistant
if (role === "assistant") {
// create container for the action buttons
const actionBtns = document.createElement("div");
actionBtns.className = "action-btns";
// add the action buttons to the message
messageElement.appendChild(actionBtns);
const copyIcon = document.createElement("i");
copyIcon.className = "fa fa-copy action-btn";
copyIcon.title = "Copy to clipboard";
copyIcon.addEventListener("click", function () {
// Copy the message to the clipboard
navigator.clipboard
.writeText(content)
.then(() => {
// Change the icon to a check
copyIcon.className = "fa fa-check action-btn";
// Revert to the default icon after 2 seconds
setTimeout(() => {
copyIcon.className = "fa fa-copy action-btn";
}, 2000);
})
// Display an x icon if the copy operation fails
.catch(() => {
copyIcon.className = "fa fa-times action-btn";
// Revert to the default icon after 2 seconds
setTimeout(() => {
copyIcon.className = "fa fa-copy action-btn";
}, 2000);
});
});
actionBtns.appendChild(copyIcon);
}
}
chatMessages.appendChild(messageElement);
// enable the clear chat button
checkClearChatBtn();
// scroll to the displayed message in the chat-messages div
messageElement.scrollIntoView();
}
function formatMessageContent(text) {
const converter = new showdown.Converter();
text = converter.makeHtml(text);
return text.replace(/```(\w+)([\s\S]*?)```/g, function (match, lang, code) {
// Create a code element
var codeElement = document.createElement("code");
// remove the first line break from the code
code = code.replace(/^\n/, "");
// Add the code to the code element
codeElement.innerText = code;
// Create a container for the code element
var codeContainer = document.createElement("div");
codeContainer.appendChild(codeElement);
// Set the class of the container based on the language (optional)
codeContainer.className = "code-block";
// Return the HTML content with the replaced code
return codeContainer.outerHTML;
});
}
// Function to display an array of messages
function displayMessages(messages) {
for (const message of messages) {
if (message.role !== "system") {
displayMessage(message.role, message.content);
}
}
}
// fucntion to check if the clear chat button should be enabled or disabled
function checkClearChatBtn() {
chrome.storage.local.get(["chatHistory"], function (result) {
const chatHistory = result.chatHistory || [];
if (chatHistory.length > 0) {
clearChatBtn.disabled = false;
} else {
clearChatBtn.disabled = true;
}
});
}
// Clear the chat history when the clear chat button is clicked
clearChatBtn.addEventListener("click", function () {
// Display a confirmation popup
const isConfirmed = window.confirm(
"Are you sure you want to clear the chat history?"
);
// If the user confirms, clear the chat history
if (isConfirmed) {
chrome.storage.local.set({ chatHistory: [] }, function () {
console.log("Chat history cleared");
chatMessages.innerHTML = "";
sendBtn.disabled = true;
checkClearChatBtn();
displayAssistanInfo();
});
}
});
// Open the dropdown when the button is clicked
document
.getElementById("model-dropdown-btn")
.addEventListener("click", function () {
var dropdownContent = document.getElementById("model-dropdown-content");
// Toggle the display property
dropdownContent.style.display =
dropdownContent.style.display === "flex" ? "none" : "flex";
// Add active class to the button if the dropdown is open
document
.getElementById("model-dropdown-btn")
.classList.toggle("active", dropdownContent.style.display === "flex");
});
// Close the dropdown if the user clicks outside of it
window.addEventListener("click", function (event) {
if (!event.target.matches("#model-dropdown-btn")) {
var dropdownContent = document.getElementById("model-dropdown-content");
if (dropdownContent.style.display === "flex") {
dropdownContent.style.display = "none";
}
// remove active class from the button if the dropdown is closed
document.getElementById("model-dropdown-btn").classList.remove("active");
}
});
// Handle button clicks in the dropdown
var dropdownButtons = document.querySelectorAll(".model-dropdown-btn");
dropdownButtons.forEach(function (button) {
button.addEventListener("click", function () {
// Get the ID of the clicked button
var buttonId = button.id;
// Set the localStorage value
chrome.storage.local.set({ apiModel: buttonId });
// Update the text on the main button
document.getElementById("model-dropdown-btn-text").innerText =
button.innerText;
// Set active model
setActiveModel(buttonId);
});
});
// Function to set the active model in the dropdown
function setActiveModel(model) {
// add active class to the button and remove it from the other buttons
dropdownButtons.forEach(function (button) {
button.classList.remove("active");
});
document.getElementById(model).classList.add("active");
}
// Set the active model when the popup is opened
chrome.storage.local.get(["apiModel"], function (result) {
if (result.apiModel) {
// Update the text on the main button
document.getElementById("model-dropdown-btn-text").innerText =
document.getElementById(result.apiModel).innerText;
// Set active model in the dropdown
setActiveModel(result.apiModel);
}
});
function displayAssistanInfo() {
// Create a div element for the message
const messageElement = document.createElement("div");
messageElement.id = "assistant-info-wrapper";
// Create an img element for the icon
const icon = document.createElement("img");
icon.src = "/assets/icons/Zorb.png";
icon.alt = "Assistant icon";
icon.className = "assistant-info-icon";
messageElement.appendChild(icon);
// Create a p element for the text
const text = document.createElement("p");
text.innerText = "Please fill your OpenAI API key at settings to start chatting.";
text.className = "assistant-info-text";
messageElement.appendChild(text);
// Append the message element to the chatMessages container
chatMessages.appendChild(messageElement);
}
function hideAssistanInfo() {
const assistantInfo = document.getElementById("assistant-info-wrapper");
if (assistantInfo) {
assistantInfo.remove();
}
}
});
// API KEY UPLOADING PART
document.addEventListener("DOMContentLoaded", function () {
// Fetch the elements
const apiKeyInput = document.getElementById("apiKey");
const saveButton = document.getElementById("save-button");
const deleteButton = document.getElementById("delete-button");
const statusMessage = document.getElementById("status-message");
// Retrieve the saved API key from local storage
chrome.storage.local.get(["apiKey"], function (result) {
if (result.apiKey) {
apiKeyInput.value = result.apiKey;
}
});
// Add event listener to the save button
saveButton.addEventListener("click", function () {
// Get the entered API key
const apiKey = apiKeyInput.value.trim();
// Check if the API key is not empty
if (
apiKey !== "" &&
apiKey.length > 10 &&
apiKey.length < 100 &&
apiKey.includes("sk-")
) {
// Save the API key to local storage
chrome.storage.local.set({ apiKey }, function () {
// Update the status message
statusMessage.textContent = "API key saved successfully!";
setTimeout(function () {
// Clear the status message after 2 seconds
statusMessage.textContent = "";
}, 2000);
});
} else {
// Display an error message if the API key is empty
statusMessage.textContent = "Please enter a valid API key.";
}
});
// Add event listener to the delete button
deleteButton.addEventListener("click", function () {
// Remove the API key from local storage
chrome.storage.local.remove(["apiKey"], function () {
// Update the status message
statusMessage.textContent = "API key deleted successfully!";
apiKeyInput.value = "";
setTimeout(function () {
// Clear the status message after 2 seconds
statusMessage.textContent = "";
}, 2000);
});
});
});