覆盖 JavaScript 全局原生 Object() 构造函数
Override JavaScript global native Object() constructor
我正在尝试覆盖 JavaScript 中的 Object()
调用,以便拦截并保存传递给对象构造函数的所有参数。
我无法访问原始代码,因为我是 运行ning 来自注入的代码,但我 运行 来自相同的范围并共享相同的 window
原始代码。
我试过了:
(function (nativeObject) {
window.Object = function (value) {
if (value.magic) {
window.myMagic = value.magic;
}
return nativeObject(value);
}
})(Object);
以及在 window.nativeObject
上保存原始 window.Object
并从我的钩子中调用它,但在这两种方式中我最终得到:
TypeError: Object.defineProperty is not a function
TypeError: Object.keys is not a function
Uncaught (in promise) TypeError: Object.isExtensible is not a function
Uncaught (in promise) TypeError: Object.create is not a function
是否因为我的 window.myMagic
调用了 Object
方法来设置 window
对象中的 myMagic
键?
我想做的事情可行吗?
您正在用一个没有任何预期静态方法的函数替换 Object
。调用 Object.defineProperties
不再有效,需要调用 nativeObject.defineProperties
。
然而,您可以轻松地将这些属性从本机函数复制到替换函数,以避免破坏依赖它们的代码:
(function (nativeObject) {
window.Object = function (value) {
if (value.magic) {
window.myMagic = value.magic;
}
return nativeObject(value);
}
for (const key of Reflect.ownKeys(nativeObject)) { // names and symbols
const descriptor = nativeObject.getOwnPropertyDescriptor(nativeObject, key);
nativeObject.defineProperty(Object, key, descriptor);
}
})(Object);
我将展示如何用代理替换 Object
构造函数:我们需要为 [=11= 的调用实现 apply
和 construct
陷阱] 没有或有 new
.
(nativeObject => {
function Object(value) {
if (value.magic) {
globalThis.myMagic = value.magic;
}
return nativeObject(value);
}
globalThis.Object = new Proxy(nativeObject, {
apply: (target, thisArg, args) => Object(...args),
construct: (target, args) => Object(...args)
});
})(Object);
// Test
new Object({ magic: 42 });
console.log(myMagic); // prints 42
Object({ magic: 'foo' });
console.log(myMagic); // prints "foo"
console.log(Object.getOwnPropertyNames(Object)); // prints an array of property names
请注意,此方法不适用于任何预定义函数,这可能需要对 apply
和 construct
进行不同的处理。 Object
函数是一种特殊情况,因为无论有无 new
.
,它的行为都相同
我正在尝试覆盖 JavaScript 中的 Object()
调用,以便拦截并保存传递给对象构造函数的所有参数。
我无法访问原始代码,因为我是 运行ning 来自注入的代码,但我 运行 来自相同的范围并共享相同的 window
原始代码。
我试过了
(function (nativeObject) {
window.Object = function (value) {
if (value.magic) {
window.myMagic = value.magic;
}
return nativeObject(value);
}
})(Object);
以及在 window.nativeObject
上保存原始 window.Object
并从我的钩子中调用它,但在这两种方式中我最终得到:
TypeError: Object.defineProperty is not a function
TypeError: Object.keys is not a function
Uncaught (in promise) TypeError: Object.isExtensible is not a function
Uncaught (in promise) TypeError: Object.create is not a function
是否因为我的 window.myMagic
调用了 Object
方法来设置 window
对象中的 myMagic
键?
我想做的事情可行吗?
您正在用一个没有任何预期静态方法的函数替换 Object
。调用 Object.defineProperties
不再有效,需要调用 nativeObject.defineProperties
。
然而,您可以轻松地将这些属性从本机函数复制到替换函数,以避免破坏依赖它们的代码:
(function (nativeObject) {
window.Object = function (value) {
if (value.magic) {
window.myMagic = value.magic;
}
return nativeObject(value);
}
for (const key of Reflect.ownKeys(nativeObject)) { // names and symbols
const descriptor = nativeObject.getOwnPropertyDescriptor(nativeObject, key);
nativeObject.defineProperty(Object, key, descriptor);
}
})(Object);
我将展示如何用代理替换 Object
构造函数:我们需要为 [=11= 的调用实现 apply
和 construct
陷阱] 没有或有 new
.
(nativeObject => {
function Object(value) {
if (value.magic) {
globalThis.myMagic = value.magic;
}
return nativeObject(value);
}
globalThis.Object = new Proxy(nativeObject, {
apply: (target, thisArg, args) => Object(...args),
construct: (target, args) => Object(...args)
});
})(Object);
// Test
new Object({ magic: 42 });
console.log(myMagic); // prints 42
Object({ magic: 'foo' });
console.log(myMagic); // prints "foo"
console.log(Object.getOwnPropertyNames(Object)); // prints an array of property names
请注意,此方法不适用于任何预定义函数,这可能需要对 apply
和 construct
进行不同的处理。 Object
函数是一种特殊情况,因为无论有无 new
.