-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_worker.js
162 lines (148 loc) · 4.36 KB
/
service_worker.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
import {
DEFAULT_ONYX_DOMAIN,
CHROME_SPECIFIC_STORAGE_KEYS,
ACTIONS,
} from "./src/utils/constants.js";
async function setupSidePanel() {
if (chrome.sidePanel) {
try {
await chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
} catch (error) {
console.error("Error setting up side panel:", error);
}
}
}
async function openSidePanel(tabId) {
try {
await chrome.sidePanel.open({ tabId });
} catch (error) {
console.error("Error opening side panel:", error);
}
}
async function errorModal() {
try {
await chrome.sidePanel.setOptions({ enabled: false });
} catch (error) {
console.error("Error closing side panel:", error);
}
}
async function sendToOnyx(info, tab) {
const selectedText = encodeURIComponent(info.selectionText);
const currentUrl = encodeURIComponent(tab.url);
try {
const result = await chrome.storage.local.get({
[CHROME_SPECIFIC_STORAGE_KEYS.ONYX_DOMAIN]: DEFAULT_ONYX_DOMAIN,
});
const url = `${
result[CHROME_SPECIFIC_STORAGE_KEYS.ONYX_DOMAIN]
}/chat?input=${selectedText}&url=${currentUrl}`;
await openSidePanel(tab.id);
chrome.runtime.sendMessage({
action: ACTIONS.OPEN_ONYX_WITH_INPUT,
url: url,
pageUrl: tab.url,
});
} catch (error) {
console.error("Error sending to Onyx:", error);
}
}
async function toggleNewTabOverride() {
try {
const result = await chrome.storage.local.get(
CHROME_SPECIFIC_STORAGE_KEYS.USE_ONYX_AS_DEFAULT_NEW_TAB
);
const newValue =
!result[CHROME_SPECIFIC_STORAGE_KEYS.USE_ONYX_AS_DEFAULT_NEW_TAB];
await chrome.storage.local.set({
[CHROME_SPECIFIC_STORAGE_KEYS.USE_ONYX_AS_DEFAULT_NEW_TAB]: newValue,
});
chrome.notifications.create({
type: "basic",
iconUrl: "icon.png",
title: "Onyx New Tab",
message: `New Tab Override ${newValue ? "enabled" : "disabled"}`,
});
// Send a message to inform all tabs about the change
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, {
action: "newTabOverrideToggled",
value: newValue,
});
});
});
} catch (error) {
console.error("Error toggling new tab override:", error);
}
}
chrome.action.onClicked.addListener((tab) => {
openSidePanel(tab.id);
});
chrome.commands.onCommand.addListener(async (command) => {
if (command === ACTIONS.SEND_TO_ONYX) {
try {
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
});
if (tab) {
const response = await chrome.tabs.sendMessage(tab.id, {
action: ACTIONS.GET_SELECTED_TEXT,
});
const selectedText = response?.selectedText || "";
sendToOnyx({ selectionText: selectedText }, tab);
}
} catch (error) {
console.error("Error sending to Onyx:", error);
}
} else if (command === ACTIONS.TOGGLE_NEW_TAB_OVERRIDE) {
toggleNewTabOverride();
} else if (command === ACTIONS.CLOSE_SIDE_PANEL) {
try {
await chrome.sidePanel.hide();
} catch (error) {
console.error("Error closing side panel via command:", error);
}
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === ACTIONS.GET_CURRENT_ONYX_DOMAIN) {
chrome.storage.local.get(
{ [CHROME_SPECIFIC_STORAGE_KEYS.ONYX_DOMAIN]: DEFAULT_ONYX_DOMAIN },
(result) => {
sendResponse({
[CHROME_SPECIFIC_STORAGE_KEYS.ONYX_DOMAIN]:
result[CHROME_SPECIFIC_STORAGE_KEYS.ONYX_DOMAIN],
});
}
);
return true;
}
if (request.action === ACTIONS.CLOSE_SIDE_PANEL) {
closeSidePanel();
chrome.storage.local.get(
{ [CHROME_SPECIFIC_STORAGE_KEYS.ONYX_DOMAIN]: DEFAULT_ONYX_DOMAIN },
(result) => {
chrome.tabs.create({
url: `${result[CHROME_SPECIFIC_STORAGE_KEYS.ONYX_DOMAIN]}/auth/login`,
active: true,
});
}
);
return true;
}
});
chrome.storage.onChanged.addListener((changes, namespace) => {
if (
namespace === "local" &&
changes[CHROME_SPECIFIC_STORAGE_KEYS.USE_ONYX_AS_DEFAULT_NEW_TAB]
) {
const newValue =
changes[CHROME_SPECIFIC_STORAGE_KEYS.USE_ONYX_AS_DEFAULT_NEW_TAB]
.newValue;
if (newValue === false) {
chrome.runtime.openOptionsPage();
}
}
});
setupSidePanel();