javascript Java,nashorn 访问另一个 js 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29051472/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:01:13  来源:igfitidea点击:

Java, nashorn accessing another js file

javajavascriptscopenashorn

提问by hnnn

Is it possible to include one js from another with java nashorn engine?

是否可以使用 java nashorn 引擎从另一个 js 中包含一个 js?

ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
InputStreamReader rs = new InputStreamReader(new FileInputStream(new File(.../script.js));
engine.eval(rs);

script.js

脚本.js

var System = Java.type('java.lang.System');
// document.write("./test.js"); - javax.script.ScriptException: ReferenceError: "document" is not defined 
// require('./test.js'); - require is not defined

test.js

测试.js

System.out.println("reading test.js file");

i want to create top level script (in this example its script.js) and use it as a library for other scripts in same directory.

我想创建顶级脚本(在本例中是它的 script.js)并将其用作同一目录中其他脚本的库。

回答by Vik Gamov

you can use Nashorn's load()function

你可以使用 Nashorn 的load()功能

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions

// can load script from files, URLs

load("foo.js"); // loads script from file "foo.js" from current directory
load("http://www.example.com/t.js"); // loads script file from given URL

// loads script from an object's properties. 

// Object should have "script" and "name" properties.
//     "script" property contains string code of the script. 
//     "name" property specifies name to be used while reporting errors from script
// This is almost like the standard "eval" except that it associates a name with
// the script string for debugging purpose.

load({ script: "print('hello')", name: "myscript.js"})

// load can also load from pseudo URLs like "nashorn:", "fx:". "nashorn:" pseudo URL scheme
// for nashorn's built-in scripts. "fx:" pseudo URL scheme for JavaFX support scripts

// load nashorn's parser support script - defines 'parse'
// function in global scope

load("nashorn:parser.js"); 

// load Mozilla compatibility script - which defines global functions
// like importPackage, importClass for rhino compatibility.

load("nashorn:mozilla_compat.js");

回答by Zo72

Nashornis brilliant.

纳什恩是个天才。

And it comes with the built-in loadmethod !!!

并且自带加载方法!!!

load can take a URL so you can exploit great JavaScript libraries on the CDNJS

load 可以接受一个 URL,这样你就可以利用 CDNJS 上的优秀 JavaScript 库

if you use nudge4jyou can get kangax's html minifierrunning just like this:

如果你使用nudge4j,你可以 像这样运行kangax 的 html minifier

load('https://cdnjs.cloudflare.com/ajax/libs/html-minifier/3.3.3/htmlminifier.js')
minify = require('html-minifier').minify;
input = '<div> <p>    foo </p>    </div>';
output = minify(input, { collapseWhitespace: true });