【问题标题】:New windows not able to acess NWJS modules?新窗口无法访问 NW JS 模块?
【发布时间】:2018-06-11 21:52:05
【问题描述】:

如果我在我的 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;
});



}

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

【问题讨论】:

    标签: javascript node.js nwjs


    【解决方案1】:

    您打开的窗口中唯一不是静态的部分是+ 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来加载文本

    【讨论】:

    • 完美运行...但是为什么呢?为什么在我的代码中,node 和 nwjs 模块不起作用?
    • 因为您打开 about:blank ... 而我打开的页面是“nwjs 应用程序”的“一部分”:p
    • 为什么 js/node/nwjs 需要这么多令人困惑的东西? ;-;对我来说,主窗口打开的任何东西都是应用程序的一部分
    • 我明白为什么你不希望外部网站访问 nwjs
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 2018-05-24
    • 2013-10-31
    • 2011-09-13
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多