1

Today I tried to inject some javascript logic into remote page using CasperJS with PhantomJS together.

Well, I'm quite surprised because of that:

casper.then(function() {
    console.log(this.evaluate(function() {
        function myMethod() {
            return 'Any thing?!';
        }
        return myMethod();
    }));
    console.log(this.evaluate(function() {
        return myMethod();
    }));
});

I tried many combinations... Like:

casper.evaluate(...)
this.evaluate(...)
casper.page.evaluate(...) <- directly to phantomJS
this.page.evaluate(...)   <- as above

First case gives me exactly what I want. But next call to evaluate act as an idiot, which never saw executed code above.

I just want to change variables, functions, and other over remote site js runtime.

Can anybody tell me why this is happening? Greetings.

1
  • Your question doesn't make sense to me, myMethod is private to an anonymous function (in the first call). How do you expect it to be available in a different anonymous function *(second call)? You should show what you are actually trying to do so we can suggest something else. Commented Feb 4, 2014 at 17:11

1 Answer 1

5

You cannot do what you are thinking. myMethod is private to the function that is passed to this.evaluate. It doesn't make sense that you would expect a private function to be available in another function, just because it was passed to the same method.

In your example, you could try

casper.then(function() {
    function myMethod() {
        return 'Any thing?!';
    }
    console.log(this.evaluate(function() {
        return myMethod();
    }));
    console.log(this.evaluate(function() {
        return myMethod();
    }));
});

But that is probably not what you are looking for? Or is it?

Or are you trying to attach code to the page itself? Maybe the following?

casper.then(function() {
    console.log(this.evaluate(function() {
        // Just creating a variable won't attach it to the window, it will
        // be local to the function. However, you can attach it to the window
        // object, which is in the scope chain
        window.myMethod = function () {return 'anything'};
    }));
    console.log(this.evaluate(function() {
        return myMethod(); // or window.myMethod();
    }));
});
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I've been looking for.
This is what I've been looking for, your second example. Well, I'm not surprised it's private, but I wasn't sure about that, how phantom will inject it into webkit. Firstly I thought, it will pass anonymous function body into page context and this way, it will attach code to window. Thank's for your answer! :)
is this fine I inject whole libraries in such way?
@Копать_Шо_я_нашел I am not sure... There are restrictions on the data that can be shared between the evaluation contexts (can only pass primitives), but you should try it out and post a question if something doesn't work

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.