如果我在NWJS应用程序上使用window.open创建窗口,则该窗口似乎根本无法访问任何nodejs或nwjs模块。我该如何解决呢?
我使用document.write来编写页面的内容,因为根据代码运行的上下文它会有所不同。
import java.io.*;
public class sort {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
//Declare BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//declare variables
int x=0, y=0;
double grade=0.0;
String course;
//ask user how many courses they completed
System.out.println("How many courses have you completed?");
//obtain answer
int completed=Integer.parseInt(br.readLine());
//declare array for course
String courses[]=new String[completed];
//ask user to enter the course names use a FOR loop for this
for(int i=0;i<courses.length;i++)
{
System.out.println("Please enter course name " + (i+1));
course = br.readLine();
System.out.println("What is the grade you got for " + course);
//get their answer
grade = Double.parseDouble(br.readLine());
//end for loop
//display to the user the high achievement award qualifiers:
System.out.println("High-Ahcievement Award Qualifiers: \n");
if(grade>93)
{
//output
}
else if(grade<70)
{
System.out.println("Needs Improvement:");
//output
}
}
}
}
在app.js里面有:
var fs = require("fs");
var text = fs.readFileSync(myfolder);
text=text.toString();
var wndo = window.open("about:blank", "", "scrollbars=1,width=300,height=500");
wndo.moveTo(screen.width/2-250,screen.height/2-175);
wndo.document.write(`<!DOCTYPE html>
<html>
<head>
<title>JS Coder</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="editor/style.css"/>
</head>
<input style="display:none;" name="file" id="fileDialog" type="file" onchange="openeditor()" />
<label class="button button1" for="fileDialog">Open</label>
<input style="display:none;" name="filesave" id="filesave" type="file" onchange="saveeditor()" nwsaveas />
<label class="button button1" for="filesave" >Save</label>
<div id="content" contenteditable="true"></div>
<script src="editor/app.js"></script>
<script type="text/javascript">
document.getElementById('content').innerHTML = "`+text+`"
</script>
</html>`);
有没有办法在该窗口中启用node / nwjs模块?如果没有,我如何在我的应用程序中打开一个新窗口,该窗口可以访问node / nwjs模块,一旦打开,就可以在其中执行代码?
答案 0 :(得分:0)
您打开的窗口中唯一不是静态的部分是+ text +
...所以,您有一个几乎静态的HTML页面
这是可行的。像这样创建一个静态页面:
<!DOCTYPE html>
<html>
<head>
<title>JS Coder</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input style="display:none;" name="file" id="fileDialog" type="file" onchange="openeditor()" />
<label class="button button1" for="fileDialog">Open</label>
<input style="display:none;" name="filesave" id="filesave" type="file" onchange="saveeditor()" nwsaveas />
<label class="button button1" for="filesave" >Save</label>
<div id="content" contenteditable="true"></div>
<script src="editor/app.js"></script>
<script type="text/javascript">
function loadText(text) {
document.getElementById('content').innerHTML = text;
}
</script>
</body>
</html>
让我们说它叫otherpage.html
- 它也和你的“主页”在同一个文件夹中
现在,主页中的javascript是
var fs = require("fs");
var text = fs.readFileSync(myfolder);
text=text.toString();
var wndo = window.open("otherpage.html", "", "scrollbars=1,width=300,height=500");
wndo.addEventListener('load', () => {
wndo.loadText(text);
});
wndo.moveTo(screen.width/2-250,screen.height/2-175);
因此主页面等待加载其他页面,然后调用另一页loadText
来加载文本