-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackground.js
312 lines (282 loc) · 9.13 KB
/
background.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
// background.js
chrome.commands.onCommand.addListener((command, tab) => {
if (command === "show-overlay") {
chrome.tabs.sendMessage(tab.id, { action: "openWaiMenu" });
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (
request.action === "summarize" ||
request.action === "translate" ||
request.action === "explain" ||
request.action === "keywords" ||
request.action === "sentiment"
) {
handleAIRequest(request.action, request.selectedText, sender.tab.id);
}
});
async function handleAIRequest(action, text, tabId) {
const prompt = getPromptForAction(action, text);
const response = await queryOpenAI(prompt);
if (response) {
showToast(tabId, response);
} else {
showToast(tabId, "Error processing request. Please try again.", true);
}
}
function getPromptForAction(action, text) {
switch (action) {
case "summarize":
return `Summarize the following text:\n\n${text}`;
case "translate":
return `Translate the following text to English:\n\n${text}`;
case "explain":
return `Explain the following text in simple terms:\n\n${text}`;
case "keywords":
return `Extract key words from the following text:\n\n${text}`;
case "sentiment":
return `Analyze the sentiment of the following text:\n\n${text}`;
default:
return text;
}
}
// Keep your existing queryOpenAI and showToast functions here
// Handle the Alt+Shift+K shortcut
chrome.commands.onCommand.addListener(function (command) {
if (command === "show-overlay") {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]) {
showOverlay(tabs[0].id);
}
});
}
});
chrome.commands.onCommand.addListener((command, tab) => {
// Command listener for Alt+Shift+Z/X
//if (command === 'search-with-openai' || command === 'search-mcq') {
if (command === "search-mcq") {
chrome.scripting.executeScript(
{
target: { tabId: tab.id },
function: getSelectedText,
},
async (selection) => {
if (selection[0]) {
const isMCQ = command === "search-mcq";
const response = await queryOpenAI(selection[0].result, isMCQ);
handleQueryResponse(response, tab.id, isMCQ);
}
}
);
}
// Command listener for Alt+Shift+C (custom copy)
if (command === "custom-copy") {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
const selectedText = window.getSelection().toString();
const textarea = document.createElement("textarea");
textarea.textContent = selectedText;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
},
});
}
// Command listener for Alt+Shift+V (custom paste)
if (command === "custom-paste") {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: async () => {
const clipboardText = await navigator.clipboard.readText();
document.activeElement.value = clipboardText;
document.activeElement.dispatchEvent(new Event("input", { bubbles: true }));
},
});
}
// Command listener for Alt+Shift+H (HELP)
if (command === "show-help") {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: showHelpToast,
});
}
});
// showHelpToast function
function showHelpToast() {
const overlayId = "image-toast-overlay";
if (document.getElementById(overlayId)) {
document.getElementById(overlayId).remove();
return;
}
const overlay = document.createElement("div");
overlay.id = overlayId;
overlay.innerHTML = `<img src="https://i.imgur.com/qEQuh64.png" style="width: 255px; height: auto;">`;
overlay.style.cssText = "position: fixed; bottom: 20px; right: 20px; z-index: 9999;";
document.body.appendChild(overlay);
setTimeout(() => {
if (document.getElementById(overlayId)) {
document.getElementById(overlayId).remove();
}
}, 5000);
document.addEventListener("keydown", function onKeyPress(e) {
if (e.key === "Escape" && document.getElementById(overlayId)) {
document.getElementById(overlayId).remove();
document.removeEventListener("keydown", onKeyPress);
}
});
}
// Function to get the selected text in the current tab
function getSelectedText() {
return window.getSelection().toString();
}
// Function to handle the response from queryOpenAI
function handleQueryResponse(response, tabId, isMCQ = false) {
if (response) {
if (isMCQ) {
showMCQToast(tabId, response);
} else {
copyToClipboard(response);
showToast(tabId, "Successful!");
}
} else {
showToast(tabId, "Error. Try again after 30s.", true);
}
}
async function queryOpenAI(text, isMCQ = false) {
const API_URL = localStorage.getItem("endpoint") || "";
const API_KEY = localStorage.getItem("apiKey") || "";
if (isMCQ) {
text +=
"\nThis is a MCQ question, Just give the option number and the correct answer option alone. No need any explanation. The output should be in this format : <option no.>. <answer option>. If you think the question is ot an mcq, just only say `Not an MCQ`.";
}
try {
const response = await fetch(API_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-4-1106-preview",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: text },
],
}),
});
if (!response.ok) {
const errorData = await response.json();
console.error("Error querying OpenAI:", errorData);
return null;
}
const data = await response.json();
return (
data.choices &&
data.choices[0] &&
data.choices[0].message &&
data.choices[0].message.content.trim()
);
} catch (error) {
console.error("Exception while querying OpenAI:", error);
return null;
}
}
function copyToClipboard(text) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: function (content) {
const textarea = document.createElement("textarea");
textarea.textContent = content;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
},
args: [text],
});
}
});
}
function showToast(tabId, message, isError = false) {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: function (msg, isError) {
const toast = document.createElement("div");
toast.textContent = msg;
toast.style.position = "fixed";
toast.style.bottom = "20px";
toast.style.right = "20px";
toast.style.backgroundColor = "black";
toast.style.color = isError ? "red" : "white";
toast.style.padding = "10px";
toast.style.borderRadius = "5px";
toast.style.zIndex = 1000;
const closeBtn = document.createElement("span");
closeBtn.textContent = " ◉";
closeBtn.style.float = "right";
closeBtn.style.cursor = "pointer";
closeBtn.onclick = function () {
toast.remove();
};
toast.appendChild(closeBtn);
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 5000);
},
args: [message, isError],
});
}
function showMCQToast(tabId, message) {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: function (msg) {
// Parse the message to separate option number and answer
const [optionNumber, ...optionAnswer] = msg.split(" ");
const formattedMsg = `<b>${optionNumber}</b> ${optionAnswer.join(" ")}`;
const toast = document.createElement("div");
toast.innerHTML = formattedMsg;
toast.style.position = "fixed";
toast.style.bottom = "10px";
toast.style.left = "50%";
toast.style.transform = "translateX(-50%)";
toast.style.backgroundColor = "black";
toast.style.color = "white";
toast.style.padding = "15px";
toast.style.borderRadius = "5px";
toast.style.zIndex = 1000;
toast.style.fontSize = "16px";
toast.style.textAlign = "center";
toast.style.maxWidth = "80%";
// Add close button
const closeBtn = document.createElement("span");
closeBtn.innerHTML = "×";
closeBtn.style.float = "right";
closeBtn.style.cursor = "pointer";
closeBtn.style.marginLeft = "10px";
closeBtn.onclick = function () {
toast.remove();
};
toast.appendChild(closeBtn);
document.body.appendChild(toast);
// Set timeout for auto-dismiss
setTimeout(() => {
toast.remove();
}, 5000);
},
args: [message],
});
}
function showAlert(tabId, message) {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: function (msg) {
alert(msg);
},
args: [message],
});
}