如果我在我的NWJS应用程序上使用window.open创建一个窗口,该窗口似乎根本无法访问任何nodejs或nwjs模块。我该如何解决这个问题?
我使用document.write编写页面的内容,因为它将根据代码运行的上下文而有所不同。
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>`);在app.js内部有:
document.getElementById('content').innerHTML = '';
openeditor=function(){
var fileinput = document.querySelector('input[type=file]');
var path = fileinput.value;
path=path.toString()
var fs = require('fs');
fs.readFile(path, 'utf8', function(err, txt) {
if (err) {
alert(err)
return;
}
document.getElementById('content').innerHTML=txt
});
}
saveeditor=function(){
var fileinput = document.querySelector('input[id=filesave]');
var path = fileinput.value;
const fs = require("fs")
texto=document.getElementById('content').innerHTML
texto=texto.toString()
fs.writeFile(path,texto , function (err) {
if (err) return;
});
}是否有方法在该窗口中启用节点/nwjs模块?如果不是,我如何在我的应用程序中打开一个新窗口,它可以访问节点/nwjs模块,并且一旦打开,就在其中执行代码?
发布于 2018-06-11 22:59:30
你打开的窗口中唯一不是静态的部分是+ 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来加载文本。
https://stackoverflow.com/questions/50806540
复制相似问题