4

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.

11
  • Is there a good reason you can't modify HardCoded? Commented Feb 9, 2018 at 21:43
  • 1
    Have a look: stackoverflow.com/questions/3406467/… Commented Feb 9, 2018 at 21:44
  • If you really cannot, just chain the handler and wrap the hardcoded call in a promise Commented Feb 9, 2018 at 21:44
  • what does HardCoded do? Is it making a DOM change that you can watch for? If you cant edit the function to add a callback, then you will need to determine what changes you can watch for when the function fires. Commented Feb 9, 2018 at 21:44
  • @SterlingArcher this is for building chrome extension so cant modify Commented Feb 9, 2018 at 21:45

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

StackOverflow (too many recursions) detected! Try to execute HardCode()
Make sure you pass the arguments along
Still going to say: Uncaught RangeError: Maximum call stack size exceeded
hoisting is why this fails.
It needs to be declared as var oldFn = HardCoded; HardCoded = function (){
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.