This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
background.ts
230 lines (214 loc) · 6.08 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
import { getUUID } from "~utils/uuid";
import { Chat, db, Message } from "~utils/store/indexedDB";
import { Messages, Settings } from "~utils/constants";
import { ip_rule, ua_rule } from '~utils/rules';
import { browserSyncGet } from "~utils/store/browser";
// TODO: debug mode
// chrome.storage.onChanged.addListener(console.log.bind(console))
// TODO: refactor this
chrome.storage.onChanged.addListener(
(changes, areaName) => {
if (areaName === "sync") {
if (changes[Settings.REQUEST_IP]) {
const rules = [];
if (changes[Settings.REQUEST_IP].newValue) {
rules.push(ip_rule);
} else {
}
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, ip_rule.id],
addRules: rules
}).then(r => {
console.log(`REQUEST_IP 2: ${r}`);
});
}
if (changes[Settings.REQUEST_UA]) {
const rules = [];
if (changes[Settings.REQUEST_UA].newValue) {
rules.push(ua_rule);
} else {
}
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, ua_rule.id],
addRules: rules
}).then(r => {
console.log(`REQUEST_IP 2: ${r}`);
});
}
}
}
)
browserSyncGet(Settings.REQUEST_IP).then(r => {
const rules = [];
console.log(`REQUEST_IP: ${r}`);
if (r) {
rules.push(ip_rule);
} else {
}
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, ip_rule.id],
addRules: rules
}).then(r => {
console.log(`REQUEST_IP 2: ${r}`);
});
});
browserSyncGet(Settings.REQUEST_UA).then(r => {
console.log(r);
const rules = [];
console.log(`REQUEST_IP: ${r}`);
if (r) {
rules.push(ua_rule);
} else {
}
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, ua_rule.id],
addRules: rules
}).then(r => {
console.log(r);
});
});
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
const saveChat = async () => {
await saveChatToDB(request.body);
sendResponse({ success: true });
}
const handleGetWindowSize = async (request, sendResponse) => {
console.log(`GET_WINDOW_SIZE: ${request.body}`);
const currentWindow = await chrome.windows.getCurrent();
console.log(`GET_WINDOW_SIZE:currentWindow`);
console.log(currentWindow);
sendResponse(currentWindow);
}
async function resizeWindow() {
console.log(request.body);
const r1 = await chrome.windows.update(request.body.id, {
width: request.body.width,
height: request.body.height,
state: request.body.state
}).catch(error => {
console.log(error);
}
);
await sendResponse(r1);
}
async function resizeWindow2() {
await chrome.windows.update(request.body.id, {
width: request.body.width,
height: request.body.height
}).catch(error => {
console.log(error);
}
);
const r2 = await chrome.windows.update(request.body.id, {
state: request.body.state
}).catch(error => {
console.log(error);
}
);
await sendResponse(r2);
}
if (request.type === Messages.SAVE_CHAT) {
// TODO: 完善日志体系……
saveChat();
} else if (request.type === Messages.RESIZE_WINDOW) {
resizeWindow();
} else if (request.type === Messages.RESIZE_WINDOW_2) {
// Have no idea why RESIZE_WINDOW is not working when resizing from small to large
resizeWindow2();
} else if (request.type === Messages.GET_WINDOW_SIZE) {
handleGetWindowSize(request, sendResponse);
}
return true;
}
);
const saveChatToDB = async ({qAsJSON, id}) => {
const firstQuestionText = getFirstQuestionText(qAsJSON);
const chat_id = id;
// const title = handleTitle(firstQuestionText, chat_id);
if (firstQuestionText === undefined || firstQuestionText === null) {
// don't store record that only have bing hello message
return ;
}
const title = firstQuestionText;
const currentUsers = db.users.where("login").equals(1);
const currentUser = await currentUsers.first();
const user_id = currentUser.id;
const chat: Chat = {
id: chat_id,
title: title,
user_id: user_id,
created_time: new Date(),
updated_time: new Date()
};
console.log("save chat");
console.log(chat);
db.chats.put(chat);
// message
// :
// "BulkError chats.bulkAdd(): 1 of 2 operations failed. Errors: ConstraintError: Key already exists in the object store."
// name
// :
// "DatabaseClosedError"
const messages = [];
function isIterable(obj) {
return obj != null && typeof obj[Symbol.iterator] === "function";
}
for (let chat_turn of qAsJSON) {
// map to messages
if (isIterable(chat_turn.questions)) {
for (let question of chat_turn.questions) {
const result_question: Message = {
id: getUUID(),
body: question.text,
is_bing: false,
chat_id,
user_id,
order: messages.length,
created_time: new Date()
};
messages.push(result_question);
}
}
if (isIterable(chat_turn.answers)) {
for (let answer of chat_turn.answers) {
const result_answer: Message = {
id: getUUID(),
body: answer.text,
html: answer.html,
meta: answer.meta,
refs: answer.refs,
is_bing: true,
chat_id,
user_id,
order: messages.length,
created_time: new Date()
};
messages.push(result_answer);
}
}
}
// bulk add to db
// TODO: if debug
console.log(messages);
db.messages.bulkAdd(messages)
.catch(error => {
console.log(error);
});
};
const getFirstQuestionText = (input) => {
for (let chat_turn of input) {
if (chat_turn.questions) {
for (let question of chat_turn.questions) {
if (question.text) {
return question.text;
}
}
} else {
}
}
return null;
};