-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathexec_warp.ts
More file actions
103 lines (96 loc) · 2.82 KB
/
exec_warp.ts
File metadata and controls
103 lines (96 loc) · 2.82 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
import ExecScript from "./exec_script";
import type { Message } from "@Packages/message/types";
import type { ScriptLoadInfo } from "../service_worker/types";
import type { GMInfoEnv } from "./types";
import { type TExtensionEnv } from "../extension/extension_env";
export class CATRetryError {
msg: string;
time: Date;
constructor(msg: string, time: number | Date) {
this.msg = msg;
if (typeof time === "number") {
this.time = new Date(Date.now() + time * 1000);
} else {
this.time = time;
}
}
}
export class BgExecScriptWarp extends ExecScript {
setTimeout: Map<number, boolean>;
setInterval: Map<number, boolean>;
constructor(scriptRes: ScriptLoadInfo, message: Message, extensionEnv: TExtensionEnv | undefined) {
const thisContext: { [key: string]: any } = {};
const setTimeout = new Map<number, any>();
const setInterval = new Map<number, any>();
thisContext.setTimeout = function (handler: () => void, timeout: number | undefined, ...args: any) {
const t = global.setTimeout(
function () {
setTimeout.delete(t);
if (typeof handler === "function") {
handler();
}
},
timeout,
...args
);
setTimeout.set(t, true);
return t;
};
thisContext.clearTimeout = function (t: number) {
setTimeout.delete(t);
global.clearTimeout(t);
};
thisContext.setInterval = function (handler: () => void, timeout: number | undefined, ...args: any) {
const t = global.setInterval(
function () {
if (typeof handler === "function") {
handler();
}
},
timeout,
...args
);
setInterval.set(t, true);
return t;
};
thisContext.clearInterval = function (t: number) {
setInterval.delete(t);
global.clearInterval(t);
};
// @ts-ignore
thisContext.CATRetryError = CATRetryError;
const envInfo: GMInfoEnv = {
sandboxMode: "raw",
userAgentData: {
brands: [],
mobile: false,
platform: "",
},
isIncognito: false,
};
const { inIncognitoContext, userAgentData } = extensionEnv || {};
if (typeof inIncognitoContext === "boolean") envInfo.isIncognito = inIncognitoContext;
if (userAgentData) envInfo.userAgentData = userAgentData;
super(scriptRes, {
envPrefix: "offscreen",
message: message,
contentMsg: message,
code: scriptRes.code,
envInfo,
globalInjection: thisContext,
});
this.setTimeout = setTimeout;
this.setInterval = setInterval;
}
stop() {
this.setTimeout.forEach((_, t) => {
global.clearTimeout(t);
});
this.setTimeout.clear();
this.setInterval.forEach((_, t) => {
global.clearInterval(t);
});
this.setInterval.clear();
return super.stop();
}
}