-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathruntime.ts
More file actions
574 lines (525 loc) · 18 KB
/
Copy pathruntime.ts
File metadata and controls
574 lines (525 loc) · 18 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import { MessageQueue, Unsubscribe } from "@Packages/message/message_queue";
import { ExtMessageSender, GetSender, Group, MessageSend } from "@Packages/message/server";
import {
Script,
SCRIPT_STATUS,
SCRIPT_STATUS_DISABLE,
SCRIPT_STATUS_ENABLE,
SCRIPT_TYPE_NORMAL,
ScriptDAO,
ScriptRunResouce,
} from "@App/app/repo/scripts";
import { ValueService } from "./value";
import GMApi from "./gm_api";
import { subscribeScriptDelete, subscribeScriptEnable, subscribeScriptInstall } from "../queue";
import { ScriptService } from "./script";
import { runScript, stopScript } from "../offscreen/client";
import { getRunAt } from "./utils";
import { isUserScriptsAvailable, randomString } from "@App/pkg/utils/utils";
import Cache from "@App/app/cache";
import { dealPatternMatches, UrlMatch } from "@App/pkg/utils/match";
import { ExtensionContentMessageSend } from "@Packages/message/extension_message";
import { sendMessage } from "@Packages/message/client";
import { compileInjectScript } from "../content/utils";
import LoggerCore from "@App/app/logger/core";
import PermissionVerify from "./permission_verify";
import { SystemConfig } from "@App/pkg/config/config";
import { ResourceService } from "./resource";
import { LocalStorageDAO } from "@App/app/repo/localStorage";
import Logger from "@App/app/logger/logger";
// 为了优化性能,存储到缓存时删除了code、value与resource
export interface ScriptMatchInfo extends ScriptRunResouce {
matches: string[];
excludeMatches: string[];
customizeExcludeMatches: string[];
}
export interface EmitEventRequest {
uuid: string;
event: string;
eventId: string;
data?: any;
}
export class RuntimeService {
scriptDAO: ScriptDAO = new ScriptDAO();
scriptMatch: UrlMatch<string> = new UrlMatch<string>();
scriptCustomizeMatch: UrlMatch<string> = new UrlMatch<string>();
scriptMatchCache: Map<string, ScriptMatchInfo> | null | undefined;
isEnableDeveloperMode = false;
isEnableUserscribe = true;
constructor(
private systemConfig: SystemConfig,
private group: Group,
private sender: MessageSend,
private mq: MessageQueue,
private value: ValueService,
private script: ScriptService,
private resource: ResourceService
) {}
async init() {
// 启动gm api
const permission = new PermissionVerify(this.group.group("permission"));
const gmApi = new GMApi(this.systemConfig, permission, this.group, this.sender, this.mq, this.value, this);
permission.init();
gmApi.start();
this.group.on("stopScript", this.stopScript.bind(this));
this.group.on("runScript", this.runScript.bind(this));
this.group.on("pageLoad", this.pageLoad.bind(this));
// 检查是否开启了开发者模式
this.isEnableDeveloperMode = isUserScriptsAvailable();
if (!this.isEnableDeveloperMode) {
// 未开启加上警告引导
// 判断是否首次
const localStorage = new LocalStorageDAO();
localStorage.get("firstShowDeveloperMode").then((res) => {
if (!res) {
localStorage.save({
key: "firstShowDeveloperMode",
value: true,
});
// 打开页面
chrome.tabs.create({
url: `https://docs.scriptcat.org/docs/use/open-dev/`,
});
}
});
chrome.action.setBadgeBackgroundColor({
color: "#ff8c00",
});
chrome.action.setBadgeTextColor({
color: "#ffffff",
});
chrome.action.setBadgeText({
text: "!",
});
}
// 监听脚本开启
subscribeScriptEnable(this.mq, async (data) => {
const script = await this.scriptDAO.getAndCode(data.uuid);
if (!script) {
return;
}
// 如果是普通脚本, 在service worker中进行注册
// 如果是后台脚本, 在offscreen中进行处理
if (script.type === SCRIPT_TYPE_NORMAL) {
// 加载页面脚本
// 不管开没开启都要加载一次脚本信息
await this.loadPageScript(script);
if (!data.enable) {
await this.unregistryPageScript(script.uuid);
}
}
});
// 监听脚本安装
subscribeScriptInstall(this.mq, async (data) => {
const script = await this.scriptDAO.get(data.script.uuid);
if (!script) {
return;
}
if (script.type === SCRIPT_TYPE_NORMAL) {
await this.loadPageScript(script);
}
});
// 监听脚本删除
subscribeScriptDelete(this.mq, async ({ uuid }) => {
await this.unregistryPageScript(uuid);
this.deleteScriptMatch(uuid);
});
this.systemConfig.addListener("enable_script", (enable) => {
this.isEnableUserscribe = enable;
if (enable) {
this.registerUserscripts();
} else {
this.unregisterUserscripts();
}
});
// 检查是否开启
this.isEnableUserscribe = await this.systemConfig.getEnableScript();
if (this.isEnableUserscribe) {
this.registerUserscripts();
}
}
unsubscribe: Unsubscribe[] = [];
// 取消脚本注册
unregisterUserscripts() {
chrome.userScripts.unregister();
this.deleteMessageFlag();
}
async registerUserscripts() {
// 读取inject.js注入页面
this.registerInjectScript();
// 将开启的脚本发送一次enable消息
const scriptDao = new ScriptDAO();
const list = await scriptDao.all();
list.forEach((script) => {
if (script.type !== SCRIPT_TYPE_NORMAL) {
return;
}
this.mq.publish("enableScript", { uuid: script.uuid, enable: script.status === SCRIPT_STATUS_ENABLE });
});
// 监听offscreen环境初始化, 初始化完成后, 再将后台脚本运行起来
this.mq.subscribe("preparationOffscreen", () => {
list.forEach((script) => {
if (script.type === SCRIPT_TYPE_NORMAL) {
return;
}
this.mq.publish("enableScript", { uuid: script.uuid, enable: script.status === SCRIPT_STATUS_ENABLE });
});
});
this.loadScriptMatchInfo();
}
messageFlag() {
return Cache.getInstance().getOrSet("scriptInjectMessageFlag", () => {
return Promise.resolve(randomString(16));
});
}
deleteMessageFlag() {
return Cache.getInstance().del("scriptInjectMessageFlag");
}
getMessageFlag() {
return Cache.getInstance().get("scriptInjectMessageFlag");
}
// 给指定tab发送消息
sendMessageToTab(to: ExtMessageSender, action: string, data: any) {
if (to.tabId === -1) {
// 如果是-1, 代表给offscreen发送消息
return sendMessage(this.sender, "offscreen/runtime/" + action, data);
}
return sendMessage(
new ExtensionContentMessageSend(to.tabId, {
documentId: to.documentId,
frameId: to.frameId,
}),
"content/runtime/" + action,
data
);
}
// 给指定脚本触发事件
emitEventToTab(to: ExtMessageSender, req: EmitEventRequest) {
if (to.tabId === -1) {
// 如果是-1, 代表给offscreen发送消息
return sendMessage(this.sender, "offscreen/runtime/emitEvent", req);
}
return sendMessage(
new ExtensionContentMessageSend(to.tabId, {
documentId: to.documentId,
frameId: to.frameId,
}),
"content/runtime/emitEvent",
req
);
}
async getPageScriptUuidByUrl(url: string, includeCustomize?: boolean) {
const match = await this.loadScriptMatchInfo();
// 匹配当前页面的脚本
const matchScriptUuid = match.match(url!);
// 包含自定义排除的脚本
if (includeCustomize) {
const excludeScriptUuid = this.scriptCustomizeMatch.match(url!);
const match = new Set<string>();
excludeScriptUuid.forEach((uuid) => {
match.add(uuid);
});
matchScriptUuid.forEach((uuid) => {
match.add(uuid);
});
// 转化为数组
return Array.from(match);
}
return matchScriptUuid;
}
async getPageScriptByUrl(url: string, includeCustomize?: boolean) {
const matchScriptUuid = await this.getPageScriptUuidByUrl(url, includeCustomize);
return matchScriptUuid.map((uuid) => {
return Object.assign({}, this.scriptMatchCache?.get(uuid));
});
}
async pageLoad(_: any, sender: GetSender) {
const [scriptFlag] = await Promise.all([this.messageFlag(), this.loadScriptMatchInfo()]);
const chromeSender = sender.getSender() as chrome.runtime.MessageSender;
// 匹配当前页面的脚本
const matchScriptUuid = await this.getPageScriptUuidByUrl(chromeSender.url!);
const scripts = matchScriptUuid.map((uuid) => {
const scriptRes = Object.assign({}, this.scriptMatchCache?.get(uuid));
// 判断脚本是否开启
if (scriptRes.status === SCRIPT_STATUS_DISABLE) {
return undefined;
}
// 如果是iframe,判断是否允许在iframe里运行
if (chromeSender.frameId) {
if (scriptRes.metadata.noframes) {
return undefined;
}
}
// 获取value
return scriptRes;
});
const enableScript = scripts.filter((item) => item) as ScriptMatchInfo[];
await Promise.all([
// 加载value
...enableScript.map(async (script) => {
const value = await this.value.getScriptValue(script!);
script.value = value;
}),
// 加载resource
...enableScript.map(async (script) => {
const resource = await this.resource.getScriptResources(script);
script.resource = resource;
}),
]);
this.mq.emit("pageLoad", {
tabId: chromeSender.tab?.id,
frameId: chromeSender.frameId,
scripts: enableScript,
});
console.log("pageLoad", enableScript);
return Promise.resolve({ flag: scriptFlag, scripts: enableScript });
}
// 停止脚本
stopScript(uuid: string) {
return stopScript(this.sender, uuid);
}
// 运行脚本
async runScript(uuid: string) {
const script = await this.scriptDAO.get(uuid);
if (!script) {
return;
}
const res = await this.script.buildScriptRunResource(script);
return runScript(this.sender, res);
}
// 注册inject.js
async registerInjectScript() {
// 如果没设置过, 则更新messageFlag
let messageFlag = await this.getMessageFlag();
if (!messageFlag) {
messageFlag = await this.messageFlag();
const injectJs = await fetch("/src/inject.js").then((res) => res.text());
// 替换ScriptFlag
const code = `(function (MessageFlag) {\n${injectJs}\n})('${messageFlag}')`;
chrome.userScripts.configureWorld({
csp: "script-src 'self' 'unsafe-inline' 'unsafe-eval' *",
messaging: true,
});
try {
// 注册content.js
await chrome.scripting.registerContentScripts([
{
id: "scriptcat-content",
js: ["/src/content.js"],
matches: ["<all_urls>"],
allFrames: true,
runAt: "document_start",
world: "ISOLATED",
},
]);
} catch (e) {
LoggerCore.logger().error("update inject.js error", Logger.E(e));
throw e;
}
const scripts: chrome.userScripts.RegisteredUserScript[] = [
{
id: "scriptcat-inject",
js: [{ code }],
matches: ["<all_urls>"],
allFrames: true,
world: "MAIN",
runAt: "document_start",
},
];
try {
// 如果使用getScripts来判断, 会出现找不到的问题
// 另外如果使用
await chrome.userScripts.register(scripts);
} catch (e: any) {
LoggerCore.logger().error("register inject.js error", Logger.E(e));
if (e.message?.indexOf("Duplicate script ID") !== -1) {
// 如果是重复注册, 则更新
try {
await chrome.userScripts.update(scripts);
} catch (e) {
LoggerCore.logger().error("update inject.js error", Logger.E(e));
}
}
}
}
}
loadingScript: Promise<void> | null | undefined;
// 加载脚本匹配信息,由于service_worker的机制,如果由不活动状态恢复过来时,会优先触发事件
// 可能当时会没有脚本匹配信息,所以使用脚本信息时,尽量使用此方法获取
async loadScriptMatchInfo() {
if (this.scriptMatchCache) {
return this.scriptMatch;
}
if (this.loadingScript) {
await this.loadingScript;
} else {
// 如果没有缓存, 则创建一个新的缓存
const cache = new Map<string, ScriptMatchInfo>();
this.loadingScript = Cache.getInstance()
.get("scriptMatch")
.then((data: { [key: string]: ScriptMatchInfo }) => {
if (data) {
Object.keys(data).forEach((key) => {
const item = data[key];
cache.set(item.uuid, item);
this.syncAddScriptMatch(item);
});
}
});
await this.loadingScript;
this.loadingScript = null;
this.scriptMatchCache = cache;
}
return this.scriptMatch;
}
// 保存脚本匹配信息
async saveScriptMatchInfo() {
if (!this.scriptMatchCache) {
return;
}
const scriptMatch = {} as { [key: string]: ScriptMatchInfo };
this.scriptMatchCache.forEach((val, key) => {
scriptMatch[key] = val;
// 优化性能,将不需要的信息去掉
// 而且可能会超过缓存的存储限制
scriptMatch[key].code = "";
scriptMatch[key].value = {};
scriptMatch[key].resource = {};
});
return await Cache.getInstance().set("scriptMatch", scriptMatch);
}
async addScriptMatch(item: ScriptMatchInfo) {
if (!this.scriptMatchCache) {
await this.loadScriptMatchInfo();
}
this.scriptMatchCache!.set(item.uuid, item);
this.syncAddScriptMatch(item);
this.saveScriptMatchInfo();
}
syncAddScriptMatch(item: ScriptMatchInfo) {
// 清理一下老数据
this.scriptMatch.del(item.uuid);
this.scriptCustomizeMatch.del(item.uuid);
// 添加新的数据
item.matches.forEach((match) => {
this.scriptMatch.add(match, item.uuid);
});
item.excludeMatches.forEach((match) => {
this.scriptMatch.exclude(match, item.uuid);
});
item.customizeExcludeMatches.forEach((match) => {
this.scriptCustomizeMatch.add(match, item.uuid);
});
}
async updateScriptStatus(uuid: string, status: SCRIPT_STATUS) {
if (!this.scriptMatchCache) {
await this.loadScriptMatchInfo();
}
const script = await this.scriptMatchCache!.get(uuid);
if (script) {
script.status = status;
this.saveScriptMatchInfo();
}
}
async deleteScriptMatch(uuid: string) {
if (!this.scriptMatchCache) {
await this.loadScriptMatchInfo();
}
this.scriptMatchCache!.delete(uuid);
this.scriptMatch.del(uuid);
this.scriptCustomizeMatch.del(uuid);
this.saveScriptMatchInfo();
}
// 加载页面脚本, 会把脚本信息放入缓存中
// 如果脚本开启, 则注册脚本
async loadPageScript(script: Script) {
const scriptRes = await this.script.buildScriptRunResource(script);
const matches = scriptRes.metadata["match"];
if (!matches) {
return;
}
scriptRes.code = compileInjectScript(scriptRes);
matches.push(...(scriptRes.metadata["include"] || []));
const patternMatches = dealPatternMatches(matches);
const scriptMatchInfo: ScriptMatchInfo = Object.assign(
{ matches: patternMatches.result, excludeMatches: [], customizeExcludeMatches: [] },
scriptRes
);
const registerScript: chrome.userScripts.RegisteredUserScript = {
id: scriptRes.uuid,
js: [{ code: scriptRes.code }],
matches: patternMatches.patternResult,
world: "MAIN",
};
// 排除由loadPage时决定, 不使用userScript的excludeMatches处理
if (script.metadata["exclude"]) {
const excludeMatches = script.metadata["exclude"];
const result = dealPatternMatches(excludeMatches, {
exclude: true,
});
// registerScript.excludeMatches = result.patternResult;
scriptMatchInfo.excludeMatches = result.result;
}
// 自定义排除
if (script.selfMetadata && script.selfMetadata.exclude) {
const excludeMatches = script.selfMetadata.exclude;
const result = dealPatternMatches(excludeMatches, {
exclude: true,
});
if (!registerScript.excludeMatches) {
registerScript.excludeMatches = [];
}
// registerScript.excludeMatches.push(...result.patternResult);
scriptMatchInfo.customizeExcludeMatches = result.result;
}
// 将脚本match信息放入缓存中
this.addScriptMatch(scriptMatchInfo);
// 如果脚本开启, 则注册脚本
if (this.isEnableDeveloperMode && this.isEnableUserscribe && script.status === SCRIPT_STATUS_ENABLE) {
if (scriptRes.metadata["noframes"]) {
registerScript.allFrames = false;
} else {
registerScript.allFrames = true;
}
if (scriptRes.metadata["run-at"]) {
registerScript.runAt = getRunAt(scriptRes.metadata["run-at"]);
}
const res = await chrome.userScripts.getScripts({ ids: [script.uuid] });
const logger = LoggerCore.logger({
name: script.name,
registerMatch: {
matches: registerScript.matches,
excludeMatches: registerScript.excludeMatches,
},
});
if (res.length > 0) {
try {
await chrome.userScripts.update([registerScript]);
} catch (e) {
logger.error("update registerScript error", Logger.E(e));
}
} else {
try {
await chrome.userScripts.register([registerScript]);
} catch (e) {
logger.error("registerScript error", Logger.E(e));
}
}
await Cache.getInstance().set("registryScript:" + script.uuid, true);
}
}
async unregistryPageScript(uuid: string) {
if (
!this.isEnableDeveloperMode ||
!this.isEnableUserscribe ||
!(await Cache.getInstance().get("registryScript:" + uuid))
) {
return;
}
// 删除缓存
Cache.getInstance().del("registryScript:" + uuid);
// 修改脚本状态为disable
this.updateScriptStatus(uuid, SCRIPT_STATUS_DISABLE);
chrome.userScripts.unregister({ ids: [uuid] });
}
}