-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackground.ts
241 lines (209 loc) · 7.77 KB
/
background.ts
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
/*
background.js is the web worker code, which is basically "faceless" from a
browser tab perspective.
From here we execute the content.js script against the active tab when the
extension is invoked.
The code is built via esbuild, allowing us to include external modules
See App.tsx for an overview of how this code interacts with the browser
tab and the injected content.js script generated from the React app.
*/
import { error } from "console";
import { get_default_configuration, merge_config } from "./Configuration";
export { }
// -------
// Configuration handling
let configuration = get_default_configuration();
// Watch for changes to the user's configuration & apply them
chrome.storage.onChanged.addListener((changes, area) => {
console.log("Background detected storage change", changes, area);
if (area === 'sync' && changes.configuration) {
console.log("Background detected configuration changed");
configuration = changes.configuration.newValue;
console.log("Background using configuration", configuration);
}
});
async function readConfiguration() {
// read our stored configuration, if any
await chrome.storage.sync.get("configuration").then((data) => {
configuration = get_default_configuration();
console.log("Initial config", configuration);
if (data?.configuration) {
const new_config = merge_config(get_default_configuration(), data.configuration);
configuration = new_config;
console.log("Background retrieved configuration");
}
console.log("Background using configuration", configuration);
});
}
// now go get our config on load
readConfiguration();
let currentTab = undefined;
//----------------
// Track active tab so popup can use it
chrome.tabs.onActivated.addListener(function () {
console.log("TAB CHANGED")
//firstTimeRunning = true
//feedbackActivated = false
getCurrentTab()
.then((tab) => {
currentTab = tab;
console.log("Background current tab is now", currentTab);
});
});
//----------------
// Command handling
// Listen for direct invocation of the clip2tana extension command
// typically via the keyboard shortcut
chrome.commands.onCommand.addListener((command) => {
console.log("Background got extension command: " + command);
askContentScript({ command: "clip2tana", configuration: configuration })
.then((response) => {
if (configuration.config.inbox.pushinbox) {
pushDataToEndpoint(response.nodes, configuration.config.inbox.tanaapikey)
.catch(error => {
console.error("Failed to push data:", error);
// 这里可以添加错误通知逻辑
});
}
console.log("Background command action complete");
});
});
// listen for commands back from the content.js script running in the page
// or from the popup.js script running in the popup
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log("Background got request", request);
if (sender.tab) {
console.log("Background got message from content.js: " + request);
// what kind of requests do we need to service?
if (request === "selection-changed") {
currentTab = sender.tab;
}
return false; // signal that we will NOT send async responses
}
else {
// request is from an extension page
console.log("Background got relayed request from popup", request);
if (request.command === "get-tanapaste") {
// relay the clip request
let content_request = { command: "get-tanapaste", configuration: request.configuration };
askContentScript(content_request)
.then((result) => {
console.log("Background message action complete with result", result);
if (result) {
sendResponse(result);
}
else {
sendResponse({ result: "get-tanapaste-result", selection: "(cannot read current tab)" });
}
});
return true; // signal that we will send async responses
}
else if (request.command === "get-tananodes") {
// relay the clip request
let content_request = { command: "get-tananodes", configuration: request.configuration };
askContentScript(content_request)
.then((result) => {
console.log("Background message action complete with result", result);
if (result) {
sendResponse(result);
}
else {
sendResponse({ result: "get-tananodes-result"});
}
});
return true; // signal that we will send async responses
}
else if (request.command === "set-clipboard") {
// relay the clip request
askContentScript(request)
.then((result) => {
console.log("Background message action complete with result", result);
sendResponse(result);
});
return true; // signal that we will send async responses
}
}
});
// If we don't have a popup, this handles a click on the extension icon
// If we DO have a popup, this is never called
// Add our main listener for extension activation via the icon
chrome.action.onClicked.addListener(async (tab) => {
// invoke the main code within the context of the foreground
// chrome tab process. Note we do not expect a response from
// this message
console.log("Background got click action with tab", tab);
if (configuration === undefined) {
// wait for configuration to become available first
console.log("Waiting for configuration");
readConfiguration().then(() => {
askContentScript({ command: "get-tanapaste", configuration: configuration }, tab)
.then((result) => {
console.log("Background click action complete", result);
});
})
}
else {
askContentScript({ command: "get-tanapaste", configuration: configuration }, tab)
.then((result) => {
console.log("Background click action complete", result);
});
}
});
// anything needed at extension startup time, add it here
chrome.runtime.onInstalled.addListener(() => {
console.log("Background got Installed event");
// mark our extension to say we're alive
// chrome.action.setBadgeText({
// text: "AWAKE",
// });
});
// ----------------
// Helper functions
async function getCurrentTab() {
console.log("Background getting current tab");
let queryOptions = { active: true, currentWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);
console.log("Background got current tab: ", tab);
return tab;
}
// send a message to the React app in content.js in the browser tab
async function askContentScript(message, tab = undefined) {
if (tab === undefined) {
tab = await getCurrentTab();
}
if (tab === undefined) {
tab = currentTab; // use the last active tab
console.log("Background using last active tab: ", tab);
}
if (tab === undefined) {
console.log("Can't determine active tab");
}
else {
console.log("Background asking content script ", tab, message);
const response = await chrome.tabs.sendMessage(tab?.id, message);
console.log("Background got response: ", response);
return response;
}
}
export const endpointUrl = "https://europe-west1-tagr-prod.cloudfunctions.net/addToNodeV2";
async function pushDataToEndpoint(payload: any, token: string) {
console.log("Pushing data to endpoint", payload);
try {
const response = await fetch(endpointUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: 'Bearer ' + token,
},
body: JSON.stringify(payload),
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`Failed to push data: ${errorBody}`);
}
} catch (error) {
console.error("An error occurred while pushing data:", error);
throw error;
}
}