通过代理对象的方式来修改get set
代理代码
const proxy = function(obj) {
return new Proxy(obj, {
set: (target, prop, val) => {
console.log("SET>>>>", prop, prop, val)
return Reflect.set(...arguments);
},
get: (target, prop, r) => {
console.log("GET>>>>", prop, prop, target[prop])
return target[prop]
},
})
}
//假设要对window进行监听
window = proxy(window)
简单的获取第一层
check_array=["document","window","navigator"];
function pr(check_array){
for(let a = 0;a < check_array.length; a++) {
eval(check_array[a]+`=new Proxy(`+check_array[a]+`,{
get:function(k,v){
console.log("获取了",check_array[a],"里面的",v)
return k[v];
}
})`)
}
}
pr(check_array)
check数组可以添加更多的可能被检测的东东
然后放js最上面跑一下
方法2 深度递归30层
(function () {
function set_traverse_object(tarrget, obj, recursion_layers) {
recursion_layers -= 1;
console.log();
for (let prop in obj) {
const value = obj[prop];
const tg_name = `${tarrget}.${prop.toString()}`;
const value_type = get_value_type(value);
if (value && value_type === "object" && recursion_layers >= 1) {
set_traverse_object(tg_name, value, recursion_layers);
continue
}
if (value && value.toString() !== '[object Object]') {
console.log(`setter hook->${tg_name}; value-> ${value}; typeof-> ${value_type}`);
continue
}
console.log(`setter hook->${tg_name}; value-> ${value}; typeof-> ${value_type}\n`)
}
}
function new_handel(target_name, obj, number) {
return new Proxy(obj, my_handler(target_name, number))
}
function get_value_type(value) {
if (Array.isArray(value)) {
return 'Array'
}
return typeof value;
}
function my_handler(target_name, number) {
return {
set: function (obj, prop, value) {
const value_type = get_value_type(value);
const tg_name = `${target_name}.${prop.toString()}`;
if (value && value_type === "object") {
set_traverse_object(tg_name, value, number)
} else {
console.log(`setter hook->${tg_name}; value-> ${value}; typeof-> ${value_type}`)
}
return Reflect.set(obj, prop, value);
},
get: function (obj, prop) {
const tg_name = `${target_name}.${prop.toString()}`;
const value = Reflect.get(obj, prop);
let value_type = get_value_type(value);
if (value && value_type === 'object') {
return new_handel(tg_name, value, number)
}
console.log(`getter hook->${tg_name}; value-> ${value}; typeof-> ${value_type}\n`);
return value
}
}
}
window = new Proxy(window, my_handler(Object.keys({window})[0], 30));
}());
评论 (1)