Help with installation / infinite loop


#1

Hello PhantomJS team,

I am trying to run phantomJS here is my configuration

Ubuntu 16.04.3
I have installed phantomJS

phantomjs --version returns 2.1.1


Now I am using code igniter ( without composer )

When I run the code it gets stuck into a loop, but the remote page is nearly empty.
( it is my first try )

here is my controller code

 $config = array(
            "selector"=>".container",
            "url"=>"https://metroexit.me/dbbus/viewpage"
        );

/*viewpage is a simple  "<html><head></head><body><div class='container'>container</div></body></html>"*/

$templateScript = FCPATH."inc/js/phantom.js";
        $templateFileCopy = FCPATH."inc/js/phantom-copy-".time().".js";

        if (!copy($templateScript, $templateFileCopy)) {
            echo "failed to copy $templateFileCopy";
            return false;
        }else{
        	//echo "copied";
        }

        //Prepend configuration object onto script

        $configObj = file_get_contents($templateFileCopy);
        $configObj = 'var config = ' . json_encode($config,JSON_UNESCAPED_SLASHES). ';' . "\n" . $configObj;

        file_put_contents($templateFileCopy,$configObj);

        //Run the phantom script with php exec function, redirect output of script to an $output array;
        $output = array();
        echo exec("phantomjs $templateFileCopy 2>&1 --debug=true --load-images=false",$output);

        //delete the copied version of the phantom script as we don't need it anymore

        if ( !unlink( $templateFileCopy ) ) {
            echo "failed to delete $templateFileCopy";
            return false;
        }

        // The first element of the output will be message about adding jquery and the last element will be the 'EXIT' message from the script,
        // lets remove those so all we have is the html lines

        array_shift($output);
        array_pop($output);

        //remove any whitespace from the array elements and join all the html lines into one string of all the html

        $output= array_map('trim', $output);
        $output = join("",$output);

here is my inc/js/phantom.js script

var page = require('webpage').create();

page.onError = function (msg, trace) {

    console.log("msg");
    console.log(msg);
    console.log("trace");
    console.log(trace);
    phantom.exit();

};

page.onResourceTimeout = function(request)
{
    alert("timeout");
}

page.onAlert = function( msg ) {

    console.log( msg );

    if( msg == "EXIT" ){
        phantom.exit();
    }
};

page.open(config.url, function(status) {

    alert("test");
    console.log("url-->"+config.url)
    page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', function() {

        page.evaluate(function(config){

            window.setTimeout(function(){
                setInterval(function(){
                    pullHtmlString(config);
                }, 2000);
            }, 1);

        }, config);
    });

});

function pullHtmlString(config){

    alert($(config.selector).wrap('<p/>').parent().html());

    alert( "EXIT" );

}

any idea ?