-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathclient.ts
More file actions
373 lines (297 loc) · 10.3 KB
/
client.ts
File metadata and controls
373 lines (297 loc) · 10.3 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import type { Script, ScriptCode, ScriptRunResource, TClientPageLoadInfo } from "@App/app/repo/scripts";
import { type Resource } from "@App/app/repo/resource";
import { type Subscribe } from "@App/app/repo/subscribe";
import { type Permission } from "@App/app/repo/permission";
import type { InstallSource, ScriptMenu, ScriptMenuItem, TBatchUpdateListAction } from "./types";
import { Client } from "@Packages/message/client";
import type { MessageSend } from "@Packages/message/types";
import type PermissionVerify from "./permission_verify";
import { type UserConfirm } from "./permission_verify";
import { type FileSystemType } from "@Packages/filesystem/factory";
import { type ResourceBackup } from "@App/pkg/backup/struct";
import { type VSCodeConnect } from "../offscreen/vscode-connect";
import { type ScriptInfo } from "@App/pkg/utils/scriptInstall";
import type { ScriptService, TCheckScriptUpdateOption, TOpenBatchUpdatePageOption } from "./script";
import { encodeRValue, type TKeyValuePair } from "@App/pkg/utils/message_value";
import { type TSetValuesParams } from "./value";
import { type CSPRule, type CSPConfig } from "@App/app/repo/cspRule";
export class ServiceWorkerClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker");
}
preparationOffscreen() {
return this.do("preparationOffscreen");
}
}
export class ScriptClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/script");
}
// 脚本数据量大的时候,options页要读取全部的数据,可能会导致options页卡顿,直接调用serviceWorker的接口从内存中读取数据
getAllScripts(): Promise<Script[]> {
return this.doThrow("getAllScripts");
}
// 获取安装信息
getInstallInfo(uuid: string) {
return this.do<[boolean, ScriptInfo, { byWebRequest?: boolean }]>("getInstallInfo", uuid);
}
install(params: {
script: Script;
code: string;
upsertBy?: InstallSource;
createtime?: number;
updatetime?: number;
}): Promise<{ update: boolean }> {
if (!params.upsertBy) params.upsertBy = "user";
return this.doThrow("install", { ...params });
}
// delete(uuid: string) {
// return this.do("delete", uuid);
// }
deletes(uuids: string[]) {
return this.do("deletes", uuids);
}
enable(uuid: string, enable: boolean) {
return this.do("enable", { uuid, enable });
}
enables(uuids: string[], enable: boolean) {
return this.do("enables", { uuids, enable });
}
info(uuid: string): Promise<Script> {
return this.doThrow("fetchInfo", uuid);
}
getFilterResult(req: { value: string }): Promise<ScriptCode | undefined> {
return this.do("getFilterResult", req);
}
getScriptRunResourceByUUID(uuid: string): Promise<ScriptRunResource> {
return this.doThrow("getScriptRunResourceByUUID", uuid);
}
excludeUrl(uuid: string, excludePattern: string, remove: boolean) {
return this.do("excludeUrl", { uuid, excludePattern, remove });
}
// 重置匹配项
resetMatch(uuid: string, match: string[] | undefined) {
return this.do("resetMatch", { uuid, match });
}
// 重置排除项
resetExclude(uuid: string, exclude: string[] | undefined) {
return this.do("resetExclude", { uuid, exclude });
}
requestCheckUpdate(uuid: string) {
return this.do("requestCheckUpdate", uuid);
}
sortScript(data: { before: string[]; after: string[] }) {
return this.do("sortScript", data);
}
pinToTop(uuids: string[]) {
return this.do("pinToTop", uuids);
}
importByUrl(url: string): ReturnType<ScriptService["importByUrl"]> {
return this.doThrow("importByUrl", url);
}
installByCode(uuid: string, code: string, upsertBy: InstallSource = "user") {
return this.do("installByCode", { uuid, code, upsertBy });
}
setCheckUpdateUrl(uuid: string, checkUpdate: boolean, checkUpdateUrl?: string) {
return this.do("setCheckUpdateUrl", { uuid, checkUpdate, checkUpdateUrl });
}
updateMetadata(uuid: string, key: string, value: string[]) {
return this.do("updateMetadata", { uuid, key, value });
}
async getBatchUpdateRecordLite(i: number) {
return this.do<any>("getBatchUpdateRecordLite", i);
}
async fetchCheckUpdateStatus() {
return this.do<void>("fetchCheckUpdateStatus");
}
async sendUpdatePageOpened() {
return this.do<void>("sendUpdatePageOpened");
}
async batchUpdateListAction(action: TBatchUpdateListAction): Promise<any> {
return this.do<any>("batchUpdateListAction", action);
}
async openUpdatePageByUUID(uuid: string) {
return this.do<void>("openUpdatePageByUUID", uuid);
}
async openBatchUpdatePage(opts: TOpenBatchUpdatePageOption) {
return this.do<boolean>("openBatchUpdatePage", opts);
}
async checkScriptUpdate(opts: TCheckScriptUpdateOption) {
return this.do<void>("checkScriptUpdate", opts);
}
}
export class ResourceClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/resource");
}
getScriptResources(script: Script): Promise<{ [key: string]: Resource }> {
return this.doThrow("getScriptResources", script);
}
deleteResource(url: string) {
return this.do("deleteResource", url);
}
}
export class ValueClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/value");
}
getScriptValue(script: Script): Promise<{ [key: string]: any }> {
return this.doThrow("getScriptValue", script);
}
setScriptValue({ uuid, key, value, ts }: { uuid: string; key: string; value: any; ts?: number }) {
const keyValuePairs = [[key, encodeRValue(value)]] as TKeyValuePair[];
return this.do("setScriptValues", { uuid, keyValuePairs, ts } as TSetValuesParams);
}
setScriptValues(params: TSetValuesParams) {
return this.do("setScriptValues", params);
}
}
export class RuntimeClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/runtime");
}
runScript(uuid: string) {
return this.do("runScript", uuid);
}
stopScript(uuid: string) {
return this.do("stopScript", uuid);
}
pageLoad(): Promise<TClientPageLoadInfo> {
return this.doThrow("pageLoad");
}
scriptLoad(flag: string, uuid: string) {
return this.do("scriptLoad", { flag, uuid });
}
}
export type GetPopupDataReq = {
tabId: number;
url: string;
};
export type GetPopupDataRes = {
// 在黑名单
isBlacklist: boolean;
scriptList: ScriptMenu[];
backScriptList: ScriptMenu[];
};
export type MenuClickParams = {
uuid: string;
menus: ScriptMenuItem[];
inputValue?: any;
};
export class PopupClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/popup");
}
getPopupData(data: GetPopupDataReq): Promise<GetPopupDataRes> {
return this.doThrow("getPopupData", data);
}
menuClick(uuid: string, menus: ScriptMenuItem[], inputValue?: any) {
return this.do("menuClick", {
uuid,
menus,
inputValue,
} as MenuClickParams);
}
}
export class PermissionClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/runtime/permission");
}
confirm(uuid: string, userConfirm: UserConfirm): Promise<void> {
return this.do("confirm", { uuid, userConfirm });
}
getPermissionInfo(uuid: string): ReturnType<PermissionVerify["getInfo"]> {
return this.doThrow("getInfo", uuid);
}
deletePermission(uuid: string, permission: string, permissionValue: string) {
return this.do("deletePermission", { uuid, permission, permissionValue });
}
getScriptPermissions(uuid: string): ReturnType<PermissionVerify["getScriptPermissions"]> {
return this.doThrow("getScriptPermissions", uuid);
}
addPermission(permission: Permission) {
return this.do("addPermission", permission);
}
updatePermission(permission: Permission) {
return this.do("updatePermission", permission);
}
resetPermission(uuid: string) {
return this.do("resetPermission", uuid);
}
}
export class SynchronizeClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/synchronize");
}
export(uuids?: string[]) {
return this.do("export", uuids);
}
backupToCloud(type: FileSystemType, params: any) {
return this.do("backupToCloud", { type, params });
}
importResources(
uuid: string | undefined,
requires: ResourceBackup[],
resources: ResourceBackup[],
requiresCss: ResourceBackup[]
) {
return this.do("importResources", { uuid, requires, resources, requiresCss });
}
}
export class SubscribeClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/subscribe");
}
install(subscribe: Subscribe) {
return this.do("install", { subscribe });
}
delete(url: string) {
return this.do("delete", { url });
}
checkUpdate(url: string) {
return this.do("checkUpdate", { url });
}
enable(url: string, enable: boolean) {
return this.do("enable", { url, enable });
}
}
export class SystemClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/system");
}
connectVSCode(params: Parameters<VSCodeConnect["connect"]>[0]): ReturnType<VSCodeConnect["connect"]> {
return this.do("connectVSCode", params);
}
}
export class CSPRuleClient extends Client {
constructor(msgSender: MessageSend) {
super(msgSender, "serviceWorker/cspRule");
}
getAllRules(): Promise<CSPRule[]> {
return this.doThrow("getAllRules");
}
getEnabledRules(): Promise<CSPRule[]> {
return this.doThrow("getEnabledRules");
}
getCSPConfig(): Promise<CSPConfig> {
return this.doThrow("getCSPConfig");
}
toggleGlobal(enabled: boolean): Promise<CSPConfig> {
return this.doThrow("toggleGlobal", { enabled });
}
createRule(rule: Omit<CSPRule, "id" | "createtime" | "updatetime">): Promise<CSPRule> {
return this.doThrow("createRule", rule);
}
updateRule(params: { id: string; changes: Partial<CSPRule> }): Promise<CSPRule | false> {
return this.doThrow("updateRule", params);
}
deleteRule(id: string): Promise<void> {
return this.do("deleteRule", id);
}
toggleRule(params: { id: string; enabled: boolean }): Promise<CSPRule | false> {
return this.doThrow("toggleRule", params);
}
reorderRules(ruleIds: string[]): Promise<void> {
return this.do("reorderRules", ruleIds);
}
}