Execute browser page/javascript from a script/command-line

最后都变了- 提交于 2019-12-06 04:21:49

问题


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?


回答1:


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:

  • Running V8 Javascript Engine Standalone

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

  • HTMLUnit (I use that one for unit testing)
  • PhantomJS
  • Zombie.js



回答2:


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


来源:https://stackoverflow.com/questions/15131698/execute-browser-page-javascript-from-a-script-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!