无需浏览器即可执行 JavaScript?

发布于 2024-09-03 09:12:35 字数 396 浏览 4 评论 0原文

我正在研究没有浏览器的 Javascript 编程。我想从 Linux 或 Mac OS X 命令行运行脚本,就像我们运行任何其他脚本语言(ruby、php、perl、python...)一样。

$ javascript my_javascript_code.js

我研究了蜘蛛猴(Mozilla)和 v8(Google),但是这两者似乎都是嵌入的。

有人使用 Javascript 作为从命令行执行的脚本语言吗?

如果有人好奇我为什么要研究这个,我一直在研究 node.js。 Node.js 的性能让我想知道 javascript 是否可能是处理大数据的可行脚本语言。

I am looking into Javascript programming without a browser. 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...)

$ javascript my_javascript_code.js

I looked into spider monkey (Mozilla) and v8 (Google), but both of these appear to be embedded.

Is anyone using Javascript as a scripting language to be executed from the command line?

If anyone is curious why I am looking into this, I've been poking around node.js. The performance of node.js makes me wonder if javascript may be a viable scripting language for processing large data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

葮薆情 2024-09-10 09:12:35

主要答案

是的,为了回答您的问题,可以从命令行使用 JavaScript 作为“常规”脚本语言,而无需浏览器。由于其他人还没有提到它,我认为值得一提:

在基于 Debian 的系统上(至少包括 Ubuntu、Linux Mint 和 aptosid/sidux),除了安装 Rhino 和其他已经提到的选项之外,您还有其他选择:

因此,几乎不需要编译,您就可以使用三个重量级 JavaScript 引擎。

附录

一旦安装完毕,您只需使用 #!/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!
$

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

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

  • 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. (The 24 in the name means that it corresponds to version 24 of Firefox).

  • Install the libv8-dev package, which will provide you Google's V8 engine. 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 (e.g., 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. JIT compilation is provided as a courtesy of V8.

  • Install the package libjavascriptcoregtk-4.0-bin and use WebKit's JavaScriptCore interpreter (jsc) as a regular interpreter from the command-line. And this is without needing to have access to a Mac. On many platforms (e.g., x86 and x86_64), this interpreter will come with a JIT compiler.

So, with almost no compilation you will have three of the heavy-weight JavaScript engines at your disposal.

Addendum

Once you have things installed, you can simply create files with the #!/usr/bin/js shebang line and things will just work:

$ 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

一向肩并 2024-09-10 09:12:35

我发现了这个关于该主题的相关问题,但如果您想要直接链接,这里有是:

但我很惊讶node.js没有附带shell,但我想它实际上更像是一个基于epoll/选择器的回调/面向事件的网络服务器,所以也许它没有不需要完整的 JS 功能集,但我不太熟悉其内部工作原理。

由于您似乎对 Node.js 感兴趣并且它基于 V8,因此最好遵循有关设置 V8 环境的说明,以便您可以为 JavaScript 编程拥有一致的基础(我希望 JSC 和 V8 主要是一样,但我不确定)。

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. 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. Here's a post on it
  • You can use Chome/Google's V8 interpreter as well. 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)

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.

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

野侃 2024-09-10 09:12:35

我已经在 iMac 上安装了 Node.js,并且

node somefile.js

在 bash 中可以工作。

I have installed Node.js on an iMac and

node somefile.js

in bash will work.

梦途 2024-09-10 09:12:35

我知道你问过 Linux 和 Mac;我将提供Windows的答案,以防其他对Windows感兴趣的人发现你的问题。

Windows 包含一个可以从命令行使用的 Javascript 引擎。

自 Windows 98 以来,所有版本的 Windows 都包含名为“Windows 脚本宿主"。这是支持脚本“引擎”的 Windows 标准方式。自第一个版本以来,WSH 支持 JScript,即 Microsoft 的 Javascript 版本。除此之外,这意味着,您可以从 Windows 命令行调用任何 *.js 文件的名称,并且它将在 JScript 引擎中运行。 (通过 wscript.exe 或 cscript.exe)

您可以看到这个问题:IE9 Javascript 引擎(代号“Chakra”)的 ProgId 或 CLSID 是什么,了解如何从 cscript.exe 调用更高性能的 IE9 Javascript 引擎。

I know you asked about Linux and Mac; I am going to provide the answer for Windows, in case other people who are interested in Windows find your question .

Windows includes a Javascript engine that can be used from the command line.

All versions of Windows, since Windows 98, have included something called "The Windows Script Host". It's a windows-standard way to support script "engines". Since the first release, WSH supports JScript, Microsoft's version of 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. (via either wscript.exe or 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.

画离情绘悲伤 2024-09-10 09:12:35

因为没有人提到它:从 Java 1.6 开始,Java JDK 还捆绑了 JavaScript 命令行和 REPL。

它基于 Rhino:https://developer.mozilla.org/en/docs/Rhino

在 Java 1.6 和 1.7 中,该命令称为 jrunscript(在 Windows 上为 jrunscript.exe),可以在 JDK 的 bin 文件夹中找到。

从 Java 1.8 开始,捆绑了一个新的 JavaScript 实现(Nashorn:https://blogs.oracle.com/nashorn/

因此在 Java 1.8 中该命令称为 jjs(在 Windows 上为 jjs.exe

Since nobody mentioned it: Since Java 1.6 The Java JDK also comes bundled with a JavaScript commandline and REPL.

It is based on 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.

Starting from Java 1.8 there is bundled a new JavaScript implementation (Nashorn: https://blogs.oracle.com/nashorn/)

So in Java 1.8 the command is called jjs (jjs.exe on Windows)

禾厶谷欠 2024-09-10 09:12:35

FWIW,node.js 带有一个 shell,请尝试输入:

node-repl

一旦安装了 node.js 即可查看它的运行情况。安装 rlwrap 以使其正常工作是非常标准的。

FWIW, node.js comes with a shell, try typing in:

node-repl

once you've installed node.js to see it in action. It's pretty standard to install rlwrap to get it to work nicely.

会傲 2024-09-10 09:12:35

我使用 Ubuntu 12.10 和命令行中的 js

它可以通过我安装的 java 来使用:

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

一些示例:

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!' ]

天空是极限,然后继续前进。

I use Ubuntu 12.10 and js from commandline

It is available with my installation of 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.

南巷近海 2024-09-10 09:12:35

您可能想查看 Rhino

Rhino Shell 提供了一种以批处理模式运行 JavaScript 脚本的方法:

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

You may want to check out Rhino.

The Rhino Shell provides a way to run JavaScript scripts in batch mode:

java org.mozilla.javascript.tools.shell.Main my_javascript_code.js [args]
唐婉 2024-09-10 09:12:35

我知道这已经过时了,但你也应该尝试 Zombie.js。一个无头浏览器,速度非常快,非常适合测试!

I know this is old but you should also try Zombie.js. A headless browser which is insanely fast and ideal for testing !

渡你暖光 2024-09-10 09:12:35

PhantomJS 也允许您执行此操作

http://phantomjs.org/

PhantomJS allows you to do this as well

http://phantomjs.org/

浮生面具三千个 2024-09-10 09:12:35

我发现这个非常漂亮的开源 ECMAScript 兼容 JS 引擎完全用 C 编写,名为 duktape

Duktape 是一个嵌入式 Javascript 引擎,注重可移植性和紧凑的占用空间。

祝你好运!

I found this really nifty open source ECMAScript compliant JS Engine completely written in C called duktape

Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint.

Good luck!

做个少女永远怀春 2024-09-10 09:12:35

嗯,有 JavaScript as OSA,一个提供 JavaScript 作为 OSA 替代方案的扩展苹果脚本。我大约 10 年前就已经使用过它,不知道它是否仍然适用于当前的操作系统版本

Well there is JavaScript as OSA, an extension that provides JavaScript as an alternative to appleScript. I've been using that about 10 years ago, don't know if it's still working with current OS versions

素食主义者 2024-09-10 09:12:35

JSDB,适用于 Linux、Windows 和 Mac 应该非常适合这个要求。它使用 Mozilla 的 Spidermonkey Javascript 引擎,与 Node.js 相比,安装似乎不那么麻烦(至少几年前我上次尝试 Node.js 时是这样)。

我从这个有趣的 Javascript shell 列表中找到了 JSDB:https://developer。 mozilla.org/en-US/docs/Web/JavaScript/Shells

JSDB, available for Linux, Windows, and Mac should fit the bill pretty well. 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).

I found JSDB from this interesting list of Javascript shells: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells

云柯 2024-09-10 09:12:35

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

Yes, you can use node.js environment for running js scripts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文