peerigon / phridge

A bridge between node and PhantomJS
The Unlicense
519 stars 50 forks source link

page.onConsoleMessage event handler is null after page.includeJs #49

Closed moshewe closed 8 years ago

moshewe commented 8 years ago

I got really confused about this, I thought my page.evaluate() is not working...

        var page = phantom.createPage();
        page.onConsoleMessage = function (msg) {
            console.log(msg);
        };

        page.run(target, function (product, resolve, reject) {
            console.log('phantom> loading page:', product);
            // `this` is now a webpage instance
            this.open(product, function (status) {
                if (status !== "success") {
                    return reject(new Error("Cannot load " + this.reason_url +
                        " due to " + this.reason));
                }

                var page = this
                this.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js', function () {
                    // only after this event handler was added I saw the EVALUATING print
                    page.onConsoleMessage = function (msg) {
                        console.log(msg);
                    };
                    var enriched = page.evaluate(function () {
                        console.log('EVALUATING... @title' + document.title);
                        return document.title
                    });

                    console.log('page> attached exId and eventHandler to all' +
                        ' elements');
                    resolve(enriched);
                });
            });

Only after adding the second event registration did I see anything inside the evaluate.

jhnns commented 8 years ago

As stated in the README, the page object provided by phridge in node has a totally different API than Phantom's page object. So, adding page.onConsoleMessage has no effect.