lib/navigation.js
function openPage(url, callback, options) {

    // set up default values
    const MAX_RETRIES = 5;

    if (options) {
        var maxAttempts = options['retries'] || MAX_RETRIES;
        var flags = options['flags'];
    } else {
        maxAttempts = MAX_RETRIES;
        flags = [];
    }

    // finds an unused port so that if we queue up multiple phantom instances sequentially with async
    // the exception EADDRINUSE will not be triggered because phantom runs on a separate process (I think)
    findUnusedPort(function (port) {

        // creates new Phantom instance

        var phantomOptions = {
            onStdout: function (data) {
                // uncomment this to print the phantom stdout to the console
                //return console.log('PHANTOM STDOUT: ' + data);
            },
            port: port
        };

        if (helpers.platformIsWindows()) {
            phantomOptions['dnodeOpts'] = {
                weak: false
            }
        }

        var doAfterCreate = function(ph) {
            // create a new page
            ph.createPage(function (page) {

                // sets a user agent
                // somehow this causes the stdout for the browser to be printed to the console, so we temporarily disable
                // setting of the user agent.
                page.set('settings.userAgent', USER_AGENT);

                // SOMEHOW commenting this out stops the random phantomjs assertion error
                // set up the log to print errors
                //page.set('onResourceError', function (resourceError) {
                //    page.set('errorReason', resourceError);
                //});

                async.retry(
                    maxAttempts,
                    function (callback) {

                        page.open(url, function (status) {
                            if (status === "fail") {
                                page.get('errorReason', function (errorReason) {

                                    if (errorReason) {
                                        var errorReasonString = JSON.stringify(errorReason);
                                    } else {
                                        errorReasonString = '';
                                    }

                                    callback('Failed to open: ' + url + ' REASON: ' + errorReasonString, [undefined, ph]);
                                });
                            } else {
                                // success

                                // check if url is valid here, so that the phantom process and page is returned
                                if (!url) {
                                    callback('Url is not valid', [page, ph]);
                                    return;
                                }

                                // success, execute callback
                                callback(null, [page, ph]);
                            }
                        });
                    },
                    function (error, results) {
                        callback(error, results[0], results[1]);
                    }
                )
            });
        };

        var phantomParams = flags.concat([phantomOptions, doAfterCreate]);
        phantom.create.apply(this, phantomParams);
    });
}
function openPage(url, callback, options) {

    // set up default values
    const MAX_RETRIES = 5;

    if (options) {
        var maxAttempts = options['retries'] || MAX_RETRIES;
        var flags = options['flags'];
    } else {
        maxAttempts = MAX_RETRIES;
        flags = [];
    }

    // finds an unused port so that if we queue up multiple phantom instances sequentially with async
    // the exception EADDRINUSE will not be triggered because phantom runs on a separate process (I think)
    findUnusedPort(function (port) {

        // creates new Phantom instance

        var phantomOptions = {
            onStdout: function (data) {
                // uncomment this to print the phantom stdout to the console
                //return console.log('PHANTOM STDOUT: ' + data);
            },
            port: port
        };

        if (helpers.platformIsWindows()) {
            phantomOptions['dnodeOpts'] = {
                weak: false
            }
        }

        var doAfterCreate = function(ph) {
            // create a new page
            ph.createPage(function (page) {

                // sets a user agent
                // somehow this causes the stdout for the browser to be printed to the console, so we temporarily disable
                // setting of the user agent.
                page.set('settings.userAgent', USER_AGENT);

                // SOMEHOW commenting this out stops the random phantomjs assertion error
                // set up the log to print errors
                //page.set('onResourceError', function (resourceError) {
                //    page.set('errorReason', resourceError);
                //});

                async.retry(
                    maxAttempts,
                    function (callback) {

                        page.open(url, function (status) {
                            if (status === "fail") {
                                page.get('errorReason', function (errorReason) {

                                    if (errorReason) {
                                        var errorReasonString = JSON.stringify(errorReason);
                                    } else {
                                        errorReasonString = '';
                                    }

                                    callback('Failed to open: ' + url + ' REASON: ' + errorReasonString, [undefined, ph]);
                                });
                            } else {
                                // success

                                // check if url is valid here, so that the phantom process and page is returned
                                if (!url) {
                                    callback('Url is not valid', [page, ph]);
                                    return;
                                }

                                // success, execute callback
                                callback(null, [page, ph]);
                            }
                        });
                    },
                    function (error, results) {
                        callback(error, results[0], results[1]);
                    }
                )
            });
        };

        var phantomParams = flags.concat([phantomOptions, doAfterCreate]);
        phantom.create.apply(this, phantomParams);
    });
}
function openPage ( url , callback , options ) { // set up default values const MAX_RETRIES = 5 ; if ( options ) { var maxAttempts = options [ 'retries' ] || MAX_RETRIES ; var flags = options [ 'flags' ] ; } else { maxAttempts = MAX_RETRIES ; flags = [ ] ; } // finds an unused port so that if we queue up multiple phantom instances sequentially with async // the exception EADDRINUSE will not be triggered because phantom runs on a separate process (I think) findUnusedPort ( function ( port ) { // creates new Phantom instance var phantomOptions = { onStdout : function ( data ) { // uncomment this to print the phantom stdout to the console //return console.log('PHANTOM STDOUT: ' + data); } , port : port } ; if ( helpers . platformIsWindows ( ) ) { phantomOptions [ 'dnodeOpts' ] = { weak : false } } var doAfterCreate = function ( ph ) { // create a new page ph . createPage ( function ( page ) { // sets a user agent // somehow this causes the stdout for the browser to be printed to the console, so we temporarily disable // setting of the user agent. page . set ( 'settings.userAgent' , USER_AGENT ) ; // SOMEHOW commenting this out stops the random phantomjs assertion error // set up the log to print errors //page.set('onResourceError', function (resourceError) { //    page.set('errorReason', resourceError); //}); async . retry ( maxAttempts , function ( callback ) { page . open ( url , function ( status ) { if ( status === "fail" ) { page . get ( 'errorReason' , function ( errorReason ) { if ( errorReason ) { var errorReasonString = JSON . stringify ( errorReason ) ; } else { errorReasonString = '' ; } callback ( 'Failed to open: ' + url + ' REASON: ' + errorReasonString , [ undefined , ph ] ) ; } ) ; } else { // success // check if url is valid here, so that the phantom process and page is returned if ( ! url ) { callback ( 'Url is not valid' , [ page , ph ] ) ; return ; } // success, execute callback callback ( null , [ page , ph ] ) ; } } ) ; } , function ( error , results ) { callback ( error , results [ 0 ] , results [ 1 ] ) ; } ) } ) ; } ; var phantomParams = flags . concat ( [ phantomOptions , doAfterCreate ] ) ; phantom . create . apply ( this , phantomParams ) ; } ) ; }
Callback that holds a result

@callback pageResultCallback
@param error
@param page
@param ph
@param [result]
 
Persistently opens a page until the number of max retries have been reached
@param {string} url
@param {pageCallback} callback what to do on the page. The status of the page open will be passed in
@param {Object} [options] Options to configure how the page will be opened
@param {Array} [options.flags] Flags which will be passed to the PhantomJS process
@param {int} [options.retries] Number of times PhantomJS will try to open a page
Callback that holds a result