Javascript with PhantomJS

I’m trying to use PhantomJS in my Xojo app to create pdf out of html. This works okay but it’s way slower than my original solution. I want to use an interactive shell in Xojo so that I have to start and unload PhantomJS only once. But my Javascript is minimal and the code doesn’t work. If I type in “create” then the script should echo the value back to me. Otherwise it should exit. When executing the script in Terminal whatever I type nothing happens.

PS: The Xojo language is so beautiful. With the additional , and ; and {} the code is so much harder to read and write.

[code]“use strict”;
var page = require(‘webpage’).create(),
system = require(‘system’),
fs = require(‘fs’);

if (system.args.length !== 2) {
console.log('wrong useage ’ + system.args.length);
phantom.exit(1);}

var theCommand = system.args[1];
var marginLeft = ‘1cm’;
var marginTop = ‘1cm’;
var marginRight = ‘1cm’;
var marginBottom = ‘1cm’;
page.viewportSize = { width: 600, height: 600 };
page.paperSize = {
format: ‘A4’,
margin: {
left: marginLeft,
top: marginTop,
right: marginRight,
bottom: marginBottom},
header: {
height: “1cm”,
contents: phantom.callback(function(pageNum, numPages) {
return "

Header " + pageNum + " / " + numPages + “”;})}};

var doLoop = true;
while (doLoop) {
theCommand = fs.read(’/dev/stdin’);
if (theCommand == ‘create’)
{console.log(theCommand);}
else
{doLoop = false;
phantom.exit();}}[/code]

I think it’s the while loop. I tried searching for PhantomJS and while loops to see if there was an explicitly documented “don’t do this” type instruction. I didn’t find anything documented about not using while loops, but I also couldn’t find any success stories of using one. I even found one Stack Overflow thread where someone replaced their while loop with a setTimeout and it magically worked.

Did you get any of this code from examples somewhere?
I don’t have experience with PhantomJS, but my guess is that the while loop is locking it up.

If you have links to documentation or examples that use a while loop like that, I’d love to look around.

When you say “When executing the script in Terminal whatever I type nothing happens.” do you mean that the script immediately exits, or does it continually wait for input but no input does anything?

Lastly, unrelated, I have to disagree with you on ; and {} because I find it make things easier to read than Xojo if you format in a consistent manner. You can’t tell where ifs and endifs match up in Xojo without the auto-indentation and matching lines in Xojo. You can read vanilla javascript in text edit if you want to.

It would not hurt either to make the condition more explicit like :

while (doLoop == true) {

@Tim Parnell: the code up to the while loop is from the examples. I have successfully created pdfs with PhantomJS. The while loop is from me.

If I execute the script with

/Users/beatrixwillius/Documents/Development/phantom/phantomjs-2.1.1-macosx/bin/phantomjs /Users/beatrixwillius/Documents/Development/phantom/phantom2.js start

I get a prompt. So something is happening. Whatever I type nothing else happens. No log entry for “create”, no exit for anything else.

@Michel: thanks, but this didn’t make a difference.

How do you feed your script to PhantomJS ?

From what I see here you should use the script file name as argument :
http://phantomjs.org/quick-start.html

I found this for reading stdin: https://gist.github.com/marcelaraujo/317798da1aa900a350b6
Try something like

while (true) {
  var input = system.stdin.read();

  if (input == 'create') {
    console.log(input);
  } else {
    phantom.exit();
    break;
  }

}

(note the way the brackets are, you may find this formatting easier to read if you try it)

@Tim Parnell: doesn’t seem to make a difference.

@Michel Bujardet : /Users/beatrixwillius/Documents/Development/phantom/phantomjs-2.1.1-macosx/bin/phantomjs /Users/beatrixwillius/Documents/Development/phantom/phantom3.js bla

Well, this is the part where I start over logging everything to see what’s actually happening.
Find out if the while loop is even running I guess, lol

The while loop doesn’t seem to run. But I haven’t a clue how to use a JavaScript IDE.

Thanks for trying to help, Tim!

The equivalent of System.debuglog() is Console.Log() .