Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to hook into a javascript function call, any way to do this?

Tags:

javascript

I'm trying to hook into a function that loads Facebook's news feed:

UIIntentionalStream.instance && UIIntentionalStream.instance.loadOlderPosts();

on Facebook.com.

Is there a way for me to do this with my own JavaScript? Basically, I need to have some sort of callback - when that function is called, I'd like my own function to be called.

like image 234
Ringo Blancke Avatar asked Apr 23 '12 00:04

Ringo Blancke


People also ask

How do you link a function in JavaScript?

loadOlderPosts = function() { // hook before call old(); // hook after call }; Just hook in wherever you want, before or after the original function's call.

How do I call a JavaScript function from code?

Javascript Function call() The JavaScript Function call() method calls a function with a given this value and arguments provided individually. The call() method calls a function by passing this and specified values as arguments.

What does call () do in JavaScript?

The call() allows for a function/method belonging to one object to be assigned and called for a different object.


1 Answers

A more complete method will be:

var old = UIIntentionalStream.instance.loadOlderPosts;
UIIntentionalStream.instance.loadOlderPosts = function(arguments) {
    // hook before call
    var ret = old.apply(this, arguments);
    // hook after call
    return ret;
};

This makes sure that if loadOlderPosts is expecting any parameters or using this, it will get the correct version of them as well as if the caller expects any return value it will get it

like image 145
yossi eilaty Avatar answered Sep 21 '22 10:09

yossi eilaty