GorvGoyl
GorvGoyl

Reputation: 49220

Detect when a function is getting called in JavaScript

There are several elements on HTML page which triggers a js function HardCoded(). I cannot modify HardCoded() function.

I want to run some custom js code after the HardCoded() function is getting called. How can I do that? Is there any handlers for js functions?

I'm building a chrome extension that's why I cannot modify page source code.
I have access to JQuery.

One way is to find all elements who are calling HardCoded() and attach events to those elements but I would like to avoid this method.

Upvotes: 3

Views: 356

Answers (1)

Titus
Titus

Reputation: 22474

You could do something like this:

var oldFn = HardCoded;
window.HardCoded = function(){
   var res = oldFn.apply(this, arguments);
   // New Code ....
   return res;
}

What this does is to create a reference to the HardCoded function, redefine this function and then call the old implementation using the previously created reference.

Upvotes: 5

Related Questions