新 windows 无法访问 NWJS 模块?

New windows not able to acess NWJS modules?

如果我在我的 NWJS 应用程序上使用 window.open 创建一个 window,似乎这个 window 根本无法访问任何 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;
});



}

有没有办法在 window 中启用 node/nwjs 模块?如果没有,我如何在我的应用程序中打开一个新的 window,它可以访问 node/nwjs 模块,并在打开后执行其中的代码?

您打开的 window 中唯一不是静态的部分是 + 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 - 它也与您的 "main" 页面位于同一文件夹中

现在,您主页中的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加载文本