-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
65 lines (60 loc) · 1.95 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
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({
title: "Ask GPT",
id: "askGPT",
contexts: ["selection"],
});
chrome.contextMenus.create({
title: "Summarize",
parentId: "askGPT",
id: "summarize",
contexts: ["selection"],
});
chrome.contextMenus.create({
title: "Explain",
parentId: "askGPT",
id: "explain",
contexts: ["selection"],
});
chrome.contextMenus.create({
title: "Quick Reply",
parentId: "askGPT",
id: "quickReply",
contexts: ["selection"],
});
});
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId === "summarize") {
openPopupWithQuestion("Briefly summarize this, ", info.selectionText);
} else if (info.menuItemId === "explain") {
openPopupWithQuestion("Explain this to me in a few sentences, ", info.selectionText);
} else if (info.menuItemId === "quickReply") {
openPopupWithQuestion("Provide a good quick reply to this, ", info.selectionText);
}
});
function openPopupWithQuestion(prompt, selectedText) {
chrome.storage.local.set({question: prompt + selectedText}, function() {
chrome.windows.create({
url: chrome.runtime.getURL("popup.html"),
type: "popup"
}, function(newWindow) {
chrome.windows.update(newWindow.id, {
width: 590,
height: 645
});
});
});
}
chrome.runtime.onConnect.addListener(function(port) {
if (port.name === "popup") {
port.onMessage.addListener(function(msg) {
if (msg.action === "getQuestion") {
chrome.storage.local.get("question", function(data) {
port.postMessage({action: "setQuestion", question: data.question});
});
}
});
}
});