从脚本/命令行执行浏览器页面/javascript [英] Execute browser page/javascript from a script/command-line

查看:50
本文介绍了从脚本/命令行执行浏览器页面/javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这不是一个愚蠢的问题.
我最近对我很好奇的事情有了一个想法.
我是 Node.js 的粉丝(我认为在这里并不是真正相关)和 V8 引擎,但我想知道是否可以在内部运行浏览器(让它执行 JS).

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.

我的意思是创建一个程序(可能使用 V8 引擎),它可以打开一个页面(就像在浏览器中一样)并执行它的 javascript.

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.

例如说我在 www.mysite.co.uk/home.php 上托管了以下文件

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>

而 ping.php 看起来像:

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

假设我想通过在我的命令行/linux 框上使用某种脚本来执行 Javascript(基本上不使用浏览器).

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).

比如:

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

甚至:

./mybrowser home.php

我觉得这应该是可能的,因为 V8(或不同的 JS 引擎)在技术上应该能够执行 Javascript,但我不知道它是如何在浏览器上下文之外执行的(或者即使可能).

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).

有什么想法吗?

推荐答案

你可以使用任何 js 引擎来运行 js 脚本,只要它们不依赖于 DOM.

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

你可以先看看:

据我所知,您想要一个无头浏览器,这里有一些:

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

  • HTMLUnit(我用它来进行单元测试)
  • PhantomJS
  • Zombie.js

这篇关于从脚本/命令行执行浏览器页面/javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆