简体   繁体   English

在没有浏览器的情况下执行 JavaScript?

[英]Executing JavaScript without a browser?

I am looking into Javascript programming without a browser.我正在研究没有浏览器的 Javascript 编程。 I want to run scripts from the Linux or Mac OS X command line, much like we run any other scripting language (ruby, php, perl, python...)我想从 Linux 或 Mac OS X 命令行运行脚本,就像我们运行任何其他脚本语言(ruby、php、perl、python...)

$ javascript my_javascript_code.js

I looked into spider monkey (Mozilla) and v8 (Google), but both of these appear to be embedded.我研究了蜘蛛猴 (Mozilla) 和 v8 (Google),但它们似乎都是嵌入的。

Is anyone using Javascript as a scripting language to be executed from the command line?是否有人使用 Javascript 作为脚本语言从命令行执行?

If anyone is curious why I am looking into this, I've been poking around node.js .如果有人好奇我为什么要研究这个,我一直在寻找node.js The performance of node.js makes me wonder if javascript may be a viable scripting language for processing large data. node.js 的性能让我想知道 javascript 是否可以成为处理大数据的可行脚本语言。

Main Answer主要答案

Yes, to answer your question, it is possible to use JavaScript as a "regular" scripting language from the command line, without a browser.是的,要回答您的问题,可以在命令行中使用 JavaScript 作为“常规”脚本语言,而无需浏览器。 Since others have not mentioned it yet, I see that it is worth mentioning:由于其他人还没有提到它,我看到它值得一提:

On Debian-based systems (and this includes Ubuntu, Linux Mint, and aptosid/sidux, at least), besides the options of installing Rhino and others already mentioned, you have have other options:在基于 Debian 的系统上(至少包括 Ubuntu、Linux Mint 和 aptosid/sidux),除了安装 Rhino 和其他已经提到的选项外,您还有其他选项:

  • Install the libmozjs-78-0 package, which will provide you with Mozilla's Spidermonkey engine on the command line as a simple js24 , which can be used also as an interactive interpreter.安装libmozjs-78-0包,它将在命令行上为您提供 Mozilla 的 Spidermonkey 引擎作为一个简单的js24 ,它也可以用作交互式解释器。 (The 24 in the name means that it corresponds to version 24 of Firefox). (名称中的24表示对应 Firefox 的 24 版本)。

  • Install the libv8-dev package, which will provide you Google's V8 engine.安装libv8-dev包,它将为您提供 Google 的 V8 引擎。 It has, as one of its examples, the file /usr/share/doc/libv8-dev/examples/shell.cc.gz which you can uncompress and compile very simply (eg, g++ -Os shell.cc -o shell -lv8 ).作为示例之一,它有文件/usr/share/doc/libv8-dev/examples/shell.cc.gz可以非常简单地解压缩和编译(例如, g++ -Os shell.cc -o shell -lv8 ).

  • Install the package nodejs and it will be available both as the executable nodejs and as an alternative (in the Debian-sense) to provide the js executable.安装包nodejs ,它既可以作为可执行的nodejs使用,也可以作为替代方案(在 Debian 意义上)来提供js可执行文件。 JIT compilation is provided as a courtesy of V8. JIT 编译是由 V8 提供的。

  • Install the package libjavascriptcoregtk-4.0-bin and use WebKit's JavaScriptCore interpreter ( jsc ) as a regular interpreter from the command-line.从命令行安装包libjavascriptcoregtk-4.0-bin并使用 WebKit 的 JavaScriptCore 解释器 ( jsc ) 作为常规解释器。 And this is without needing to have access to a Mac.这无需访问 Mac。 On many platforms (eg, x86 and x86_64), this interpreter will come with a JIT compiler.在许多平台上(例如,x86 和 x86_64),这个解释器会附带一个 JIT 编译器。

So, with almost no compilation you will have three of the heavy-weight JavaScript engines at your disposal.因此,几乎不需要编译,您将拥有三个重量级的 JavaScript 引擎供您使用。

Addendum附录

Once you have things installed, you can simply create files with the #!/usr/bin/js shebang line and things will just work:一旦你安装了东西,你可以简单地使用#!/usr/bin/js shebang 行创建文件,一切都会正常工作:

$ cat foo.js 
#!/usr/bin/js

console.log("Hello, world!");
$ ls -lAF /usr/bin/js /etc/alternatives/js /usr/bin/nodejs
lrwxrwxrwx 1 root root      15 Jul 16 04:26 /etc/alternatives/js -> /usr/bin/nodejs*
lrwxrwxrwx 1 root root      20 Jul 16 04:26 /usr/bin/js -> /etc/alternatives/js*
-rwxr-xr-x 1 root root 1422004 Apr 28 20:31 /usr/bin/nodejs*
$ chmod a+x foo.js 
$ ./foo.js 
Hello, world!
$ js ./foo.js
Hello, world!
$

Older Versions:旧版本:
libmozjs-24-bin , libmozjs-52 , libmozjs-60 , libmozjs-91 , libjavascriptcoregtk-3.0-bin libmozjs-24-binlibmozjs-52libmozjs-60libmozjs-91libjavascriptcoregtk-3.0-bin

I found this related question on the topic, but if you want direct links, here they are:我在这个主题上找到了这个相关的问题,但如果你想要直接链接,它们是:

  • You can install Rhino as others have pointed out.正如其他人指出的那样,您可以安装 Rhino。 This post shows an easy way to get it up and running and how to alias a command to invoke it easily 这篇文章展示了一种启动和运行它的简单方法,以及如何给命令起别名以轻松调用它
  • If you're on a Mac, you can use JavaScriptCore, which invokes WebKit's JavaScript engine.如果您使用的是 Mac,则可以使用 JavaScriptCore,它调用 WebKit 的 JavaScript 引擎。 Here's a post on it这是一个关于它的帖子
  • You can use Chome/Google's V8 interpreter as well.您也可以使用 Chome/Google 的 V8 解释器。 Here are instructions以下是说明
  • The JavaScript as OSA is interesting because it lets you (AFAIK) interact with scriptable OS X apps as though you were in AppleScript (without the terrible syntax)作为 OSA 的 JavaScript 很有趣,因为它让您 (AFAIK) 与可编写脚本的 OS X 应用程序进行交互,就好像您在 AppleScript 中一样(没有糟糕的语法)

I'm surprised node.js doesn't come with a shell, but I guess it's really more like an epoll/selector-based callback/event-oriented webserver, so perhaps it doesn't need the full JS feature set, but I'm not too familiar with its inner workings.我很惊讶 node.js 没有附带 shell,但我想它真的更像是一个基于 epoll/selector 的回调/面向事件的 webserver,所以也许它不需要完整的 JS 功能集,但我不太熟悉它的内部运作。

Since you seem interested in node.js and since it's based on V8, it might be best to follow those instructions on getting a V8 environment set up so you can have a consistent basis for your JavaScript programming (I should hope JSC and V8 are mostly the same, but I'm not sure).由于您似乎对 node.js 感兴趣并且它基于 V8,因此最好按照这些说明设置 V8 环境,这样您就可以为 JavaScript 编程有一个一致的基础(我希望 JSC 和 V8 主要是一样,但我不确定)。

I have installed Node.js on an iMac and我已经在 iMac 上安装了 Node.js,并且

node somefile.js

in bash will work.在 bash 中将起作用。

I know you asked about Linux and Mac;我知道你问过 Linux 和 Mac; I am going to provide the answer for Windows, in case other people who are interested in Windows find your question .我将为 Windows 提供答案,以防其他对 Windows 感兴趣的人发现您的问题。

Windows includes a Javascript engine that can be used from the command line. Windows 包含一个可以从命令行使用的 Javascript 引擎。

All versions of Windows, since Windows 98, have included something called " The Windows Script Host ".从 Windows 98 开始,所有版本的 Windows 都包含一个名为“ Windows 脚本宿主”的东西。 It's a windows-standard way to support script "engines".这是支持脚本“引擎”的 Windows 标准方式。 Since the first release, WSH supports JScript, Microsoft's version of Javascript.从第一个版本开始,WSH 就支持 JScript,即 Microsoft 的 Javascript 版本。 Among other things, this means that, from a windows command line, you can just invoke the name of any *.js file, and it will run in the JScript engine.除此之外,这意味着,您可以从 Windows 命令行调用任何 *.js 文件的名称,它将在 JScript 引擎中运行。 (via either wscript.exe or cscript.exe) (通过 wscript.exe 或 cscript.exe)

You can see this question: What is the ProgId or CLSID for IE9's Javascript engine (code-named "Chakra") to learn how to invoke the higher-performance IE9 Javascript engine from cscript.exe.可以看这个问题: 什么是IE9的Javascript引擎(代号为“Chakra”)的ProgId或CLSID来学习如何从cscript.exe调用更高性能的IE9 Javascript引擎。

Since nobody mentioned it: Since Java 1.6 The Java JDK also comes bundled with a JavaScript commandline and REPL.由于没有人提到它:从 Java 1.6 开始,Java JDK 还捆绑了 JavaScript 命令行和 REPL。

It is based on Rhino: https://developer.mozilla.org/en/docs/Rhino它基于 Rhino: https ://developer.mozilla.org/en/docs/Rhino

In Java 1.6 and 1.7 the command is called jrunscript ( jrunscript.exe on Windows) and can be found in the bin folder of the JDK.在 Java 1.6 和 1.7 中,该命令称为jrunscript (在 Windows 上为jrunscript.exe ),可以在 JDK 的 bin 文件夹中找到。

Starting from Java 1.8 there is bundled a new JavaScript implementation (Nashorn: https://blogs.oracle.com/nashorn/ )从 Java 1.8 开始,捆绑了一个新的 JavaScript 实现(Nashorn: https ://blogs.oracle.com/nashorn/)

So in Java 1.8 the command is called jjs ( jjs.exe on Windows)因此,在 Java 1.8 中,该命令称为jjs (在 Windows 上为jjs.exe

FWIW, node.js comes with a shell, try typing in: FWIW,node.js 带有一个外壳,尝试输入:

node-repl

once you've installed node.js to see it in action.一旦您安装了 node.js 以查看它的运行情况。 It's pretty standard to install rlwrap to get it to work nicely.安装 rlwrap 以使其正常工作是非常标准的。

I use Ubuntu 12.10 and js from commandline我从命令行使用 Ubuntu 12.10 和 js

It is available with my installation of java:它在我安装的 java 中可用:

el@apollo:~/foo$ java -version
java version "1.6.0_27"
el@apollo:~/foo$ which js
/usr/bin/js

Some examples:一些例子:

el@apollo:~/foo$ js
> 5
5

> console.log("hello");
hello
undefined

> var f = function(){ console.log("derp"); };
undefined
> f();
derp

> var mybool = new Boolean();
undefined
> mybool
{}
> mybool == true
false
> mybool == false
true

> var myobj = {};
undefined
> myobj.skipper = "on my mark, engage!"
'on my mark, engage!'
> myobj.skipper.split(" ");
[ 'on',
  'my',
  'mark,',
  'engage!' ]

The sky is the limit, then keep right on going.天空是极限,然后继续前进。

You may want to check out Rhino .你可能想看看Rhino

The Rhino Shell provides a way to run JavaScript scripts in batch mode: Rhino Shell 提供了一种以批处理模式运行 JavaScript 脚本的方法:

java org.mozilla.javascript.tools.shell.Main my_javascript_code.js [args]

I know this is old but you should also try Zombie.js .我知道这很旧,但您也应该尝试Zombie.js A headless browser which is insanely fast and ideal for testing !一个无头浏览器,速度非常快,非常适合测试!

PhantomJS allows you to do this as well PhantomJS 也允许你这样做

http://phantomjs.org/ http://phantomjs.org/

I found this really nifty open source ECMAScript compliant JS Engine completely written in C called duktape我发现这个非常漂亮的开源 ECMAScript 兼容 JS 引擎完全用 C 语言编写,称为duktape

Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint. Duktape 是一个可嵌入的 Javascript 引擎,专注于便携性和紧凑的占用空间。

Good luck!祝你好运!

JSDB , available for Linux, Windows, and Mac should fit the bill pretty well. JSDB可用于 Linux、Windows 和 Mac 应该非常适合。 It uses Mozilla's Spidermonkey Javascript engine and seems to be less of a hassle to install compared to node.js (at least last time I tried node.js a couple of years ago).它使用 Mozilla 的 Spidermonkey Javascript 引擎,与 node.js 相比,安装起来似乎更轻松(至少几年前我上次尝试 node.js 时)。

I found JSDB from this interesting list of Javascript shells: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells我从这个有趣的 Javascript shell 列表中找到了 JSDB: https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells

Well there is JavaScript as OSA , an extension that provides JavaScript as an alternative to appleScript.那么有JavaScript as OSA ,一个提供 JavaScript 作为 appleScript 替代品的扩展。 I've been using that about 10 years ago, don't know if it's still working with current OS versions大约 10 年前我一直在使用它,不知道它是否仍然适用于当前的操作系统版本

是的,您可以使用 node.js 环境来运行 js 脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM