电子浏览器的JavaScript错误

我对节点,JavaScript和电子都很陌生。 我只是试图编写一个简单的应用程序,在浏览器窗口中打开本地的HTML文件。 本地文件有一些复杂的embedded式JavaScript(tiddlywiki)。 这里是一些示例代码(我没有使用本地文件,但结果是一样的):

const {app, BrowserWindow} = require('electron') const path = require('path') const url = require('url') let win function createWindow () { // Create the browser window. win = new BrowserWindow({width: 800, height: 600}) // and load the index.html of the app. win.loadURL(url.format({ pathname: 'tiddlywiki.com', protocol: 'http:', slashes: true, webPreferences: { nodeIntegration: false, } })) 

当电子应用程序启动时,我在浏览器开发工具中得到以下错误。

 Uncaught TypeError: Cannot read property 'length' of undefined at Object.$tw.boot.startup (tiddlywiki.com/:27506) at tiddlywiki.com/:27765 at Object.$tw.boot.decryptEncryptedTiddlers (tiddlywiki.com/:27053) at Object.$tw.boot.boot (tiddlywiki.com/:27763) at _boot (tiddlywiki.com/:27772) at tiddlywiki.com/:27782 

我认为这是因为node.js对象模型的一些整合? 对不起,缺乏了解。 先谢谢您的帮助。

你在正确的轨道上。 另一种方法是将nodeIntegration设置为false,并预先加载一个js文件,该文件将在BrowserWindow上下文中运行,并在加载事件触发后访问窗口对象。 预加载的JavaScript文件只有自己完整的节点集成。

我用它来制作一个TiddlyFox处理程序,这样我就可以使用我的Electron应用程序中的TiddlyWiki中的TiddlyFox保存程序。 这是它的代码。 其实很简单。

https://github.com/Arlen22/TiddlyWiki-Electron

如果你想直接加载一个TiddlyWiki数据文件夹到Electron中,你可以尝试加载这个HTML文件。 在new BrowserWindow(...)需要将节点集成设置为true

 <!doctype html> <html> <head></head> <body class="tc-body"> <script> global.$tw = global.require("./boot/bootprefix.js").bootprefix(); global.$tw.boot.argv = ['./editions/server']; global.$tw = require("./boot/boot.js").TiddlyWiki(global.$tw); </script> </body> </html> 

你把webPreferences放在错误的地方。

你必须把它放在BrowserWindow的初始化中,而不是在url.format中:

 win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: false } }) 

我最终解决这个问题的方法是在main.js中使用下面的代码:

  win.loadURL(url.format({ pathname: path.join(__dirname, 'index2.html'), protocol: 'file:', slashes: true })) 

那么index2包含以下内容:

 <html> <body> <webview id="webView" src="http://tiddlywiki.com" style="display:inline-flex; width:100%; height:100%" </webview> </body> </html> 

还使用本地文件进行testing来自tiddlywiki.com的empty.html,并且工作正常。 我想这使我能够预加载一个.js文件来控制一些tiddlywiki事件。 将不得不学习更多的testing。