4

Hope this isnt a stupid question.
I have recently had an idea about something which I am very curious about.
I am a fan of Node.js (not really relevent here I think) and the V8 engine but I was wondering if its possible to run a browser (get it to execute JS) but INTERNALLY.

What I mean by that is to create a program (possibly using the V8 engine) which can open a page (as if in the browser) and execute its javascript.

For instance say I have the below file hosted on www.mysite.co.uk/home.php

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
//javascript AJAX call to www.mysite.co.uk/ping.php
}
myFunction();
</script>
</head>

<body>
</body>
</html>

And ping.php looks something like:

<?php
//connect mysql, database ping and table ping
//it is a single column table with integer value starting on 0
//increment by 1 and update the table

Say I wanted to get the Javascript to execute by using some sort of script on my command line/linux box (essentially WITHOUT using a browser).

So something like:

./mybrowser http://www.mysite.co.uk/home.php

or even:

./mybrowser home.php

I feel like it should be possible as the V8 (or different JS engine) should technically be able to execute Javascript but I havnt the foggiest how it could do so out of a browser context (or even if its possible).

Any ideas?

Christophe Roussy
  • 13,732
  • 2
  • 75
  • 75
Craig Taub
  • 4,019
  • 1
  • 16
  • 24
  • 2
    Do you mean a headless browser, like [PhantomJS](http://phantomjs.org/)? – Teemu Feb 28 '13 at 09:24
  • yes a couple of google searches and could not find this...looks just the think....will give it a try and update ticket...thanks alot – Craig Taub Feb 28 '13 at 09:27

2 Answers2

3

You can use any js engine to run js scripts as long as they do not rely on the DOM.

You could start by looking at:

Edit: as I understand you want a headless browser, here are some:

  • HTMLUnit (I use that one for unit testing)
  • PhantomJS
  • Zombie.js
Community
  • 1
  • 1
Christophe Roussy
  • 13,732
  • 2
  • 75
  • 75
  • standalone seems to be JS from command line...i need it to be able to do everything a browser can do but without a browser...thanks anyway – Craig Taub Feb 28 '13 at 09:27
  • doing everything that a browser can do sounds a lot like you will end up needing a browser, so you mean like a browser but without the window/UI ? – Christophe Roussy Feb 28 '13 at 09:38
  • yes, so a script can open/execute a website many times over and produce same results as if a user was...it can be very useful for a lot of things commercial and personal – Craig Taub Feb 28 '13 at 09:44
  • ah I see, I updated my answer accordingly, I use something similar like this myself for unit tests – Christophe Roussy Feb 28 '13 at 09:51
  • Phantom.js is currently dead (https://github.com/ariya/phantomjs/issues/15344) for new development. – ansebbian0 Apr 01 '19 at 05:44
0

Running JavaScript on the command line by using either Rhino for Java or Windows Script Host.

http://www.mozilla.org/rhino/

http://msdn.microsoft.com/en-us/library/9bbdkx3k%28VS.85%29.aspx
Sagar Kadam
  • 477
  • 1
  • 3
  • 10