If I create a window using window.open on my NWJS application, it seems that this window cannot acess any nodejs or nwjs module at all. How to I work around it?
I use document.write to write the content of the page, because it will be different according to the context in which the code is running.
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>`);
inside app.js there is:
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;
});
}
Is there a way to enable node/nwjs modules in that window? And if not, how do I open a new window in my application which has acess to node/nwjs modules and once open, execute code in it?
The only part of your opened window that isn't static is + text +
... so, you have an almost static HTML page
Here's something that will work. Create a static page like so:
<!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>
Lets say it's called otherpage.html
- it's in the same folder as your "main" page too
Now, the javascript in your main page is
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);
So the main page waits for other page to load, and then calls the other page loadText
to load the text