-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathutils.ts
More file actions
294 lines (284 loc) · 8.32 KB
/
utils.ts
File metadata and controls
294 lines (284 loc) · 8.32 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
import { type ScriptRunResource } from "@App/app/repo/scripts";
import { has } from "@App/pkg/utils/lodash";
// 构建脚本运行代码
export function compileScriptCode(scriptRes: ScriptRunResource): string {
let { code } = scriptRes;
let require = "";
if (scriptRes.metadata.require) {
scriptRes.metadata.require.forEach((val) => {
const res = scriptRes.resource[val];
if (res) {
require = `${require}\n${res.content}`;
}
});
}
code = require + code;
return `with (context) return (async ()=>{\n${code}\n//# sourceURL=${chrome.runtime.getURL(
`/${encodeURI(scriptRes.name)}.user.js`
)}\n})()`;
}
// eslint-disable-next-line camelcase
export type ScriptFunc = (context: any, GM_info: any) => any;
// 通过脚本代码编译脚本函数
export function compileScript(code: string): ScriptFunc {
// eslint-disable-next-line no-new-func
return <ScriptFunc>new Function("context", "GM_info", code);
}
export function compileInjectScript(script: ScriptRunResource): string {
return `window['${script.flag}']=function(context,GM_info){\n${script.code}\n}`;
}
export const writables: { [key: string]: any } = {
addEventListener: global.addEventListener.bind(global),
removeEventListener: global.removeEventListener.bind(global),
dispatchEvent: global.dispatchEvent.bind(global),
};
// 记录初始的window字段
export const init = new Map<string, boolean>();
// 需要用到全局的
export const unscopables: { [key: string]: boolean } = {
NodeFilter: true,
RegExp: true,
};
// 复制原有的,防止被前端网页复写
const descs = Object.getOwnPropertyDescriptors(global);
Object.keys(descs).forEach((key) => {
const desc = descs[key];
// 可写但不在特殊配置writables中
if (desc && desc.writable && !writables[key]) {
if (typeof desc.value === "function") {
// 判断是否需要bind,例如Object、Function这些就不需要bind
if (desc.value.prototype) {
writables[key] = desc.value;
} else {
writables[key] = desc.value.bind(global);
}
} else {
writables[key] = desc.value;
}
} else {
init.set(key, true);
}
});
export function warpObject(thisContext: Object, ...context: Object[]) {
// 处理Object上的方法
thisContext.hasOwnProperty = (name: PropertyKey) => {
return (
Object.hasOwnProperty.call(thisContext, name) ||
context.some((val) => Object.hasOwnProperty.call(val, name))
);
};
thisContext.isPrototypeOf = (name: Object) => {
return (
Object.isPrototypeOf.call(thisContext, name) ||
context.some((val) => Object.isPrototypeOf.call(val, name))
);
};
thisContext.propertyIsEnumerable = (name: PropertyKey) => {
return (
Object.propertyIsEnumerable.call(thisContext, name) ||
context.some((val) => Object.propertyIsEnumerable.call(val, name))
);
};
}
// 拦截上下文
export function proxyContext(
global: any,
context: any,
thisContext?: { [key: string]: any }
) {
const special = Object.assign(writables);
// 处理某些特殊的属性
// 后台脚本要不要考虑不能使用eval?
if (!thisContext) {
thisContext = {};
}
thisContext.eval = global.eval;
thisContext.define = undefined;
warpObject(thisContext, special, global, context);
// keyword是与createContext时同步的,避免访问到context的内部变量
const contextKeyword: { [key: string]: any } = {
message: 1,
valueChangeListener: 1,
connect: 1,
runFlag: 1,
valueUpdate: 1,
sendMessage: 1,
scriptRes: 1,
};
// @ts-ignore
const proxy = new Proxy(context, {
defineProperty(_, name, desc) {
if (Object.defineProperty(thisContext, name, desc)) {
return true;
}
return false;
},
get(_, name): any {
switch (name) {
case "window":
case "self":
case "globalThis":
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return proxy;
case "top":
case "parent":
if (global[name] === global.self) {
return special.global || proxy;
}
return global[name];
default:
break;
}
if (name !== "undefined") {
if (has(thisContext, name)) {
// @ts-ignore
return thisContext[name];
}
if (typeof name === "string") {
if (has(context, name)) {
if (has(contextKeyword, name)) {
return undefined;
}
return context[name];
}
if (has(special, name)) {
if (
typeof special[name] === "function" &&
!(<{ prototype: any }>special[name]).prototype
) {
return (<{ bind: any }>special[name]).bind(global);
}
return special[name];
}
if (has(global, name)) {
// 特殊处理onxxxx的事件
if (name.startsWith("on")) {
if (
typeof global[name] === "function" &&
!(<{ prototype: any }>global[name]).prototype
) {
return (<{ bind: any }>global[name]).bind(global);
}
return global[name];
}
}
if (init.has(name)) {
const val = global[name];
if (
typeof val === "function" &&
!(<{ prototype: any }>val).prototype
) {
return (<{ bind: any }>val).bind(global);
}
return val;
}
} else if (name === Symbol.unscopables) {
return unscopables;
}
}
return undefined;
},
has(_, name) {
switch (name) {
case "window":
case "self":
case "globalThis":
case "top":
case "parent":
return true;
default:
break;
}
if (name !== "undefined") {
if (typeof name === "string") {
if (has(unscopables, name)) {
return false;
}
if (has(thisContext, name)) {
return true;
}
if (has(context, name)) {
if (has(contextKeyword, name)) {
return false;
}
return true;
}
if (has(special, name)) {
return true;
}
// 只处理onxxxx的事件
if (has(global, name)) {
if (name.startsWith("on")) {
return true;
}
}
} else if (typeof name === "symbol") {
return has(thisContext, name);
}
}
return false;
},
set(_, name: string, val) {
switch (name) {
case "window":
case "self":
case "globalThis":
return false;
default:
}
if (has(special, name)) {
special[name] = val;
return true;
}
if (init.has(name)) {
const des = Object.getOwnPropertyDescriptor(global, name);
// 只读的return
if (des && des.get && !des.set && des.configurable) {
return true;
}
// 只处理onxxxx的事件
if (has(global, name) && name.startsWith("on")) {
if (val === undefined) {
global.removeEventListener(name.slice(2), thisContext[name]);
} else {
if (thisContext[name]) {
global.removeEventListener(name.slice(2), thisContext[name]);
}
global.addEventListener(name.slice(2), val);
}
thisContext[name] = val;
return true;
}
}
// @ts-ignore
thisContext[name] = val;
return true;
},
getOwnPropertyDescriptor(_, name) {
try {
let ret = Object.getOwnPropertyDescriptor(thisContext, name);
if (ret) {
return ret;
}
ret = Object.getOwnPropertyDescriptor(context, name);
if (ret) {
return ret;
}
ret = Object.getOwnPropertyDescriptor(global, name);
return ret;
} catch (e) {
return undefined;
}
},
});
proxy[Symbol.toStringTag] = "Window";
return proxy;
}
export function addStyle(css: string): HTMLElement {
const dom = document.createElement("style");
dom.textContent = css;
if (document.head) {
return document.head.appendChild(dom);
}
return document.documentElement.appendChild(dom);
}