0

I have the following JavaScript code saved in a file name ph_test.js.

var page;
var args = require('system').args;
var url_str = 'http://'+args[1];

var renderPage = function(){

    page = require('webpage').create();
    var myArgs = Array.prototype.slice.call(arguments),
        url_str = myArgs[0];

    // Set the viewport size
    page.viewportSize = {
        width: 320,
        height: 480
    };

    // Sets the User Agent
    page.settings.userAgent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7';

    /**
     * From PhantomJS documentation:
     * This callback is invoked when there is a JavaScript console. The callback may accept up to three arguments:
     * the string for the message, the line number, and the source identifier.
     */
    page.onConsoleMessage = function (msg, line, source) {
        console.log('console> ' + msg);
    };

    /**
     * From PhantomJS documentation:
     * This callback is invoked when there is a JavaScript alert. The only argument passed to the callback is the string for the message.
     */
    page.onAlert = function (msg) {
        console.log('alert!!> ' + msg);
    };

    /**
     * Handle Redirection
     */
    page.onNavigationRequested = function(url_sub_str, type, willNavigate, main) {
        if (main && url_sub_str != url_str)
        {
            url_str = url_sub_str;
            console.log("redirect caught");
            page.close();
            renderPage(url_str);
        }
    };

    /**
     * Open the web page and run RRunner
     */
    page.open(url_str, function(status) {
        if (status === 'success') {
            page.injectJs('https://code.jquery.com/jquery-1.11.2.min.js');

            // Our "event loop"
            if(!phantom.state)
            {
                phFunction(url_str);
            }
            else {
                phantom.state();
            }
        }
        else
        {
            console.log('failed');
        }

        page.close();
        setTimeout(function(){
            phantom.exit();
        }, 1000);

        function phFunction()
        {
            var myArgs = Array.prototype.slice.call(arguments),
                url_str = myArgs[0]
                ;

            page.evaluate(function (url_str) {
                console.log('evaluate');
            }, url_str);

            page.render('screenshots/screenshot_full.png');
        }
    });
};

renderPage(url_str);

When I run the following command [pointing to my website]:

phantomjs ph_test.js www.restive.io

Everything works ok with no issues. However, when I run it for another website with mobile redirection [in this case taobao]

 phantomjs ph_test.js www.taobao.com

page.evaluate doesn't run as I do not see the message 'evaluate' in my console.

Note: I'm using PhantomJS 1.9.8.

ObiHill
  • 11,448
  • 20
  • 86
  • 135
  • The `page.evaluate` still works, but the console message from the page context doesn't arrive. You can pass anything to the page context and return anything from it. – Artjom B. Feb 09 '15 at 22:54
  • @ArtjomB. Any ideas how I can fix this? If the page.evaluate works, how come it doesn't show the message? – ObiHill Feb 10 '15 at 19:09
  • No, I don't know how to fix it. You can see that it works if you use `console.log(page.evaluate(function(a){return "out+"+a;}, "in"));`. I suspect that PhantomJS has a problem registering `onConsoleMessage` and `onAlert`, because of the structure of your script. – Artjom B. Feb 10 '15 at 19:13
  • Thanks for the feedback. I'm just stumped as to why websites like yahoo.com, google.com, etc. work without any issues, but taobao.com (and I assume a number of mobile redirecting sites) don't work. I just started using PhantomJS so I'm not too familiar with how it works. I'll keep looking for a solution. Cheers. – ObiHill Feb 10 '15 at 19:52

0 Answers0