I appreciate if anyone can tell me how to intercept a function call in javascript. I know it is possible with making use of proxies.
for example I tried the code below to intercept it but now I want to intercept toDataURL(). in order to call toDataURL you need to create a canvas element first.So, now I want to know how is this possible to define a proxy to intercept toDataURL().
Example code to intercept it :
window.x = 0;
let calls = (function(){
let canvas = document.createElement('canvas');
let fun = canvas.toDataURL;
canvas.toDataURL = function(){
window.x++;
return fun.apply(document, arguments);
}
return ()=>calls;
})();
canvas.toDataURLand not, as you say,document.createElement. I just wonder whatreturn ()=>calls;is for, inside the IIFE. Besides that this approach also is not a generic one; it modifies the method of exactly the created element. One might try modifyingHTMLCanvasElement.prototype.toDataURLinstead.Proxybased one. Each proving itself by working example code.