补环境复制原型属性脚本

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
const copyObject=(obj, maxDepth = 3) =>{
function extract(o, depth = 0) {
if (depth > maxDepth) return '[Max Depth]';
const result = {};
for (let key in o) {
try {
const value = o[key];
if (typeof value === 'function') continue;
if (value === null || value === undefined || typeof value !== 'object') {
result[key] = value;
} else if (Array.isArray(value)) {
result[key] = value;
} else {
try {
JSON.stringify(value);
result[key] = value;
} catch {
result[key] = extract(value, depth + 1);
}
}
} catch (e) {
result[key] = `[Error: ${e.message}]`;
}
}
return result;
}

const data = extract(obj);
copy(JSON.stringify(data, null, 2));
console.log('Copied to clipboard!');
}

补环境框架

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
const envPatcher = {
config: {
debug: true,
log: console.log,
throwErrors: true, // 是否抛出代理中捕获的错误
},

// 代理创建器
createProxy: function (name, target) {
return new Proxy(target, {
get(obj, prop) {
try {
const value = Reflect.get(obj, prop);
if (envPatcher.config.debug) {

const excludeProps = ["Array", "String", "Date", "Object", "Symbol", "constructor"];
if (!excludeProps.includes(String(prop))) {
envPatcher.config.log(`[GET] ${name}.${String(prop)} -> ${typeof value}`);
}



}
// 如果获取的值是函数,也为其创建代理
if (typeof value === "function" && !value._isProxied) {
const proxiedFunc = new Proxy(value, {
apply: (target, thisArg, args) => {
try {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[CALL] ${name}.${String(prop)}(${args
.map((a) => typeof a)
.join(", ")})`
);
}
return Reflect.apply(target, thisArg, args);
} catch (error) {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[ERROR] Failed to call ${name}.${String(prop)}: ${error.message
}`
);
}
// 在错误处添加debugger语句,方便查看堆栈
debugger;
if (envPatcher.config.throwErrors) {
throw error; // 根据配置决定是否重新抛出错误
} else {
return undefined; // 如果不抛出错误,则返回undefined
}
}
},
});
// 标记已代理,避免重复代理
proxiedFunc._isProxied = true;
return proxiedFunc;
}
return value;
} catch (error) {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[ERROR] Failed to get ${name}.${String(prop)}: ${error.message}`
);
}
// 在错误处添加debugger语句,方便查看堆栈
debugger;
if (envPatcher.config.throwErrors) {
throw error; // 根据配置决定是否重新抛出错误
} else {
return undefined; // 如果不抛出错误,则返回undefined
}
}
},

set(obj, prop, value) {
try {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[SET] ${name}.${String(prop)} = ${value} (${typeof value})`
);
}
return Reflect.set(obj, prop, value);
} catch (error) {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[ERROR] Failed to set ${name}.${String(prop)}: ${error.message}`
);
}
// 在错误处添加debugger语句,方便查看堆栈
debugger;
if (envPatcher.config.throwErrors) {
throw error; // 根据配置决定是否重新抛出错误
} else {
return false; // 如果不抛出错误,则返回false
}
}
},

has(obj, prop) {
try {
const result = Reflect.has(obj, prop);
if (envPatcher.config.debug) {
envPatcher.config.log(`[HAS] ${name}.${String(prop)} -> ${result}`);
}
return result;
} catch (error) {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[ERROR] Failed to check has ${name}.${String(prop)}: ${error.message
}`
);
}
// 在错误处添加debugger语句,方便查看堆栈
debugger;
if (envPatcher.config.throwErrors) {
throw error; // 根据配置决定是否重新抛出错误
} else {
return {}; // 如果不抛出错误,则返回空对象
}
}
},

// 处理构造函数调用
construct(target, args) {
try {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[CONSTRUCT] new ${name}(${args.map((a) => typeof a).join(", ")})`
);
}
const instance = Reflect.construct(target, args);
return instance;
} catch (error) {
if (envPatcher.config.debug) {
envPatcher.config.log(
`[ERROR] Failed to construct ${name}: ${error.message}`
);
}
// 在错误处添加debugger语句,方便查看堆栈
debugger;
if (envPatcher.config.throwErrors) {
throw error; // 根据配置决定是否重新抛出错误
} else {
return {}; // 如果不抛出错误,则返回空对象
}
}
},
});
},

// 函数保护器
protectFunction: function (func, nativeCode) {
const symbol = Symbol("native");
const originalToString = Function.prototype.toString;

func[symbol] =
nativeCode || `function ${func.name || "anonymous"}() { [native code] }`;

if (!func.toString[symbol]) {
func.toString = function () {
return func[symbol] || originalToString.call(func);
};
func.toString[symbol] = "function toString() { [native code] }";
}

return func;
},

// 设置属性
setProp: function (obj, prop, value, writable = true) {
Object.defineProperty(obj, prop, {
value: value,
writable: writable,
enumerable: true,
configurable: true,
});
return obj;
},

// 构造函数创建器,嵌套 protectFunction
createConstructor: function (name, implementation) {
// 定义构造函数
const Constructor = implementation || function () { };
// 使用 protectFunction 包装构造函数
const ProtectedConstructor = envPatcher.protectFunction(
Constructor,
`function ${name}() { [native code] }`
);
// 设置构造函数名称
Object.defineProperty(ProtectedConstructor, "name", {
value: name,
writable: false,
enumerable: false,
configurable: true,
});
// 设置原型
ProtectedConstructor.prototype = Object.create(Object.prototype, {
constructor: {
value: ProtectedConstructor,
writable: true,
enumerable: false,
configurable: true,
},
});
return ProtectedConstructor;
},


// 设置继承关系
setInheritance: function (child, parent) {
// 设置构造函数继承
Object.setPrototypeOf(child, parent);
// 设置原型继承
Object.setPrototypeOf(child.prototype, parent.prototype);
return child;
},

// 批量设置属性
setProps: function (obj, props) {
Object.keys(props).forEach(key => {
const config = props[key];
if (typeof config === 'object' && config.value !== undefined) {
this.setProp(obj, key, config.value, config);
} else {
this.setProp(obj, key, config);
}
});
return obj;
},

};


const $toString = Function.prototype.toString;
const toStringSymbol = Symbol('toString');

Function.prototype.toString = new Proxy($toString, {
apply(target, thisArg, args) {
if (new.target) {
throw new TypeError("toString is not a constructor");
}

// 检查是否有自定义的 native code 标记
if (thisArg && thisArg[toStringSymbol]) {
return thisArg[toStringSymbol];
}

return Reflect.apply(target, thisArg, args);
}
});

const Window = envPatcher.createConstructor("Window");
const Navigator = envPatcher.createConstructor("Navigator");
const Location = envPatcher.createConstructor("Location");
const Document = envPatcher.createConstructor("Document");



const navigatorObj = {};
Object.setPrototypeOf(navigatorObj, Navigator.prototype);
const locationObj = {};
Object.setPrototypeOf(locationObj, Location.prototype);
const documentObj = {};
Object.setPrototypeOf(documentObj, Document.prototype);

Object.setPrototypeOf(globalThis, Window.prototype);
globalThis.window = globalThis;
globalThis.self = globalThis;
globalThis.top = globalThis;
globalThis.parent = globalThis;
globalThis.frames = globalThis;

// 设置其他全局对象
globalThis.navigator = navigatorObj;
globalThis.Navigator = Navigator;
globalThis.location = locationObj;
globalThis.Location = Location;
globalThis.document = documentObj;
globalThis.Document = Document;



globalThis.window = envPatcher.createProxy('window', window)
globalThis.document = envPatcher.createProxy('document', document)
globalThis.navigator = envPatcher.createProxy('navigator', navigator)
globalThis.location = envPatcher.createProxy('location', location);


;;;;;;
0:00 /0:00
暧昧合伙人