-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
67 lines (55 loc) · 1.63 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
import { defaultActions } from "./defaultActions.js";
// Create the initial context menu items,
chrome.runtime.onInstalled.addListener(async () => {
const initialActions = defaultActions.map((action) => {
return {
id: Math.random().toString(36).substring(2, 15),
option: action.option,
action: action.action,
enabled: action.enabled,
};
});
await chrome.storage.local.set({
allActions: initialActions,
});
for (const action of initialActions) {
chrome.contextMenus.create({
id: action.id,
title: action.option,
type: "normal",
contexts: ["selection"],
});
}
});
chrome.contextMenus.onClicked.addListener(async (item, tab) => {
const { allActions } = await chrome.storage.local.get("allActions");
const action = allActions.find(
(action) => action.id === item.menuItemId
)?.action;
if (!action) return;
const selectText = item.selectionText;
chrome.storage.local.set({
tempText: `${action}
"${selectText}"`,
});
const url = new URL(`https://chat.openai.com/chat`);
chrome.tabs.create({ url: url.href, index: tab.index + 1 });
});
chrome.storage.onChanged.addListener(async ({ allActions }) => {
if (typeof allActions === "undefined") return;
chrome.contextMenus.removeAll();
for (const action of allActions.newValue) {
if (!action.enabled) continue;
chrome.contextMenus.create({
id: action.id,
title: action.option,
type: "normal",
contexts: ["selection"],
});
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message === "ChatGPT_On") {
sendResponse("OK");
}
});