As I understand, the ES spec says that Proxy
(global constructor for proxifying objects, functions and classes) is not detectable. It means that if I proxify a function, nobody who uses that proxified function can detect that I used Proxy. However, apparently I misunderstood it, becuase proxifying functions is detectable.
For example, new Proxy(a=>a,{apply:a=>a})+''
throws an error. It says
Uncaught TypeError: Function.prototype.toString requires that 'this' be a Function
However, typeof new Proxy(a=>a,{apply:a=>a})
is indeed "function"
, but it somehow fails to stringify the proxy. So, obviously, here is a situation when proxified function doesn't behave as non-proxified one should. Function.prototype.toString
is able to distinguish proxified and non-proxified function.
My goal is to proxify a function such that it simple become undetectable. My first idea is to literally proxify the Proxy like so:
Proxy.toString = (a => () => a)(Proxy + '');
Proxy = new Proxy(Proxy, {
construct: (f, args) => {
if(typeof args[0] == 'function'){
var a = args[0] + '';
args[0].toString = () => a;
}
return new f(...args);
}
});
But, sadly, this is detectable. If someone call Function.prototype.toString
binded to my proxified function, the error will occur and he can therefore detect that my function is actually a proxy. So, I tried to proxify the Function
and Function.prototype
and also Function.prototype.toString
, but then I realized I cannot proxify the Function
because even if I override the global property Function
, someone may access it using (a=>a).constructor
.
So, this is why I am asking it here because I ran out of ideas. How to proxify a function to make it completelly undetectable? In the ES spec it explicitly says that "Proxy is undetectable", so as a side question, why is then proxifying a function detectable?
Edit
The reason I'm trying to achieve this is because I'm working on enhanced advertisement blocking extension for Chrome. I am dealing with very agressive website which exploits a huge amount of JavaScript tricks to detect if I'm viewing the ads or not. So, basically, I deleted an advertisement, and then their script checks if there is specific element, if not, then I cannot visit the website. So, I tried to proxify document.getElementById
, but they check if it is proxified and if it is, I cannot visit the website, so I must make it undetectable.