Created
October 16, 2022 15:52
-
-
Save martian17/3fcd2e6e9efa6dcbb2d5588e424e62bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [chrome,navigator,window,document,$vars] = (()=>{ | |
let vars = []; | |
let proxyMethodNames = | |
`apply | |
construct | |
defineProperty | |
deleteProperty | |
get | |
getOwnPropertyDescriptor | |
getPrototypeOf | |
has | |
isExtensible | |
ownKeys | |
preventExtensions | |
set | |
setPrototypeOf` | |
.split("\n").map(v=>v.trim()); | |
let makeTarget = function(obj){ | |
let fn = function(){}; | |
for(let key in obj){ | |
fn[key] = obj[key]; | |
} | |
return fn; | |
} | |
let defaultHandler = {}; | |
for(let name of proxyMethodNames){ | |
defaultHandler[name] = (target)=>{ | |
console.log(`[proxy] ${name} on ${target.path}`); | |
}; | |
} | |
let handler = Object.create(defaultHandler); | |
handler.get = function(target,key){ | |
if(typeof key === 'symbol'){ | |
key = key.toString(); | |
} | |
let path = `${target.path}.${key}`; | |
if(path === "document.getElementsByTagName($2[1]).length"){ | |
return 2; | |
} | |
return new Proxy(makeTarget({path}),handler); | |
}; | |
handler.set = function(target,key,value){ | |
let n = vars.length; | |
vars.push(value); | |
console.log(`[proxy] ${target.path}.${key} = $${n}`); | |
}; | |
handler.construct = function(target,args){ | |
let path; | |
if(args.length === 0){ | |
path = `new ${target.path}()`; | |
}else{ | |
let n = vars.length; | |
vars.push(args); | |
path = `new ${target.path}($${n}[${args.length}])`; | |
} | |
console.log(`[proxy] ${path}`); | |
return new Proxy(makeTarget({path:`(${path})`}),handler); | |
}; | |
handler.apply = function(target,that,args){ | |
console.log(args); | |
let path; | |
if(args.length === 0){ | |
path = `${target.path}()`; | |
}else{ | |
let n = vars.length; | |
vars.push(args); | |
path = `${target.path}($${n}[${args.length}])`; | |
} | |
console.log(`[proxy] ${path}`); | |
return new Proxy(makeTarget({path:`${path}`}),handler); | |
} | |
return [ | |
new Proxy(makeTarget({path:"chrome"}),handler), | |
new Proxy(makeTarget({path:"navigator"}),handler), | |
new Proxy(makeTarget({path:"window"}),handler), | |
new Proxy(makeTarget({path:"document"}),handler), | |
vars]; | |
})(); | |
//a = chrome.a; | |
//chrome.a = 2; | |
//let b = new chrome.a.b.c(2,3,4); | |
//b(2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment