开发者

Can I intercept a function called directly?

In this code I created a function called someFunction. Then I modified Function.prototype.apply and call methods. So instead of my function code is working I am running my interception code (whic开发者_开发技巧h shows an alert). But neither "call" nor "apply" intercepts direct method call. Is it possiple to intercept this?

Function.prototype.call = function(){alert("call");};
Function.prototype.apply = function(){alert("apply");};
function someFunction(){}
window.onload = function(){
    someFunction.call(this); //call alert is shown
    someFunction.apply(this); //apply alert is shown
    someFunction(); //how can I intercept this?
}


You can only override a known function by setting another function in its place (e.g., you can't intercept ALL function calls):

(function () {
    // An anonymous function wrapper helps you keep oldSomeFunction private
    var oldSomeFunction = someFunction;

    someFunction = function () {
        alert("intercepted!");
        oldSomeFunction();
    }
})();

Note that, if someFunction was already aliased/referenced by another script before it was changed by this code, those references would still point to the original function not be overridden by the replacement function.


Function.prototype.callWithIntercept = function () {
    alert("intercept");
    return this.apply(null, arguments);
};

var num = parseInt.callWithIntercept("100px", 10);

It is worth noting that in newer versions of JS, there are Proxy objects you can use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy


There is a chance you can intercept direct function call. This requires:

  • Either the function is created by Function.prototype.bind and you have to overwrite Function.prototype.bind before creating the function, or
  • The function is created from Function() (or new Function()) and you also have to overwrite Function function before creating the target function.

If neither of the above two can be met, the only way to intercept a direct call is to wrap the target function, which is the solution provided by AndyE https://stackoverflow.com/a/3406523/1316480

For a function that is created by function literal and is hidden in private scope, there is no way to intercept a direct call to it.

I have a blog post concludes all of these: http://nealxyc.wordpress.com/2013/11/25/intercepting-javascript-function/


You could iterate over the global scope and replace any objects of function type you find which aren't "yours".


Brilliant, love it :)

const originalApply = window.Function.prototype.apply;
window.Function.prototype.apply = function(){
    console.log("INTERCEPTING APPLY", arguments);
    return originalApply.call(this, ...arguments);
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜