如何在 Bash 中执行 javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/791758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I execute javascript in Bash?
提问by Léo Léopold Hertz ??
I try to get to a page straight from Bash at http://www.ocwconsortium.org/. The page appears when you write mathematics to the field at the top right corner. I tested
我尝试直接从 Bash 访问http://www.ocwconsortium.org/的页面。当您将数学写入右上角的字段时,该页面会出现。我测试过
but it leads to the main page. It is clearly some javascript thing. How can I get the results straight from Bash on the first page?
但它会导致主页。这显然是一些 javascript 的东西。如何在第一页直接从 Bash 获得结果?
[Clarification]
[澄清]
Let's take an example. I have the following lines for a Math search engine in .bashrc:
让我们举个例子。对于 .bashrc 中的数学搜索引擎,我有以下几行:
alias mathundergradsearch='/Users/user/bin/mathundergraduate'
Things in a separate file:
单独文件中的内容:
#!/bin/sh
q=
w=
e=
r=
t=
open "http://www.google.com/cse?cx=007883453237583604479%3A1qd7hky6khe&ie=UTF-8&q=$q+$w+$e+$r+$t&hl=en"
Now, I want something similar to the example. The difference is that the other site contains javascript or something that does not allow me to see the parameters. How could I know where to put the search parameters as I cannot see the details?
现在,我想要类似于示例的东西。不同之处在于其他站点包含 javascript 或不允许我查看参数的内容。由于看不到详细信息,我怎么知道将搜索参数放在哪里?
采纳答案by Chas. Owens
open "http://www.ocwconsortium.org/index.php?q=mathematics&option=com_coursefinder&uss=1&l=&s=&Itemid=166&b.x=0&b.y=0&b=search"
You need quotes because the URL contains characters the shell considers to be special.
您需要引号,因为 URL 包含 shell 认为特殊的字符。
回答by Wouter van Nifterick
The Links web browsermore or less runs from the commandline (like lynx) and supports basic javascript.
该链接网页浏览器或多或少从运行在命令行(像山猫),并支持基本的JavaScript。
Even though the title of the post sounds general, your question is very specific. It's unclear to me what you're trying to achieve in the end. Clearly you can access sites that rely heavily on javascript (else you wouldn't be able to post your question here), so I'm sure that you can open the mentioned site in a normal browser.
尽管帖子的标题听起来很笼统,但您的问题非常具体。我不清楚你最终想要实现什么。显然,您可以访问严重依赖 javascript 的站点(否则您将无法在此处发布您的问题),因此我确定您可以在普通浏览器中打开提到的站点。
If you just want to execute javascript from the commandline (as the title suggests), it's easy if you're running bash via cygwin. You just call cscript.exe and provide a .js scriptname of what you wish to execute.
如果您只想从命令行执行 javascript(如标题所示),那么通过 cygwin 运行 bash 很容易。您只需调用 cscript.exe 并提供您希望执行的 .js 脚本名称。
回答by Samir Talwar
I didn't get anything handled by JavaScript - it just took me to
我没有通过 JavaScript 处理任何事情 - 它只是让我
http://www.ocwconsortium.org/index.php?q=mathematics&option=com_coursefinder&uss=1&l=&s=&Itemid=166&b.x=0&b.y=0&b=search
Replacing mathematics(right after q=) should work. You may be able to strip out some of that query string, but I tried a couple of things and and it didn't play nice.
更换mathematics(在 之后q=)应该可以工作。您也许可以删除一些查询字符串,但我尝试了一些方法,但效果不佳。
Don't forget to encode your query for URLs.
不要忘记对您的 URL 查询进行编码。
回答by Andrew Hare
You will need to parse the response, find the URL that is being opened via JavaScript and then openthat URL.
您需要解析响应,找到通过 JavaScript 打开的 URL,然后找到open该 URL。
回答by Detro
Check this out: http://www.phantomjs.org/.
看看这个:http: //www.phantomjs.org/。
PhantomJS it's a CLI tool that runs a real, fully-fledged Browser without the Chrome.
PhantomJS 它是一个 CLI 工具,可以在没有 Chrome 的情况下运行一个真正的、成熟的浏览器。

