3

我对节点、javascript 和电子非常陌生。我只是想编写一个在浏览器窗口中打开本地 HTML 文件的简单应用程序。本地文件有一些复杂的嵌入式 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 对象模型的一些集成?很抱歉缺乏理解。在此先感谢您的帮助。

4

3 回答 3

2

你放webPreferences错地方了。

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

win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        nodeIntegration: false
    }
})
于 2017-06-04T13:56:39.180 回答
0

我最终解决这个问题的方法是在 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>

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

于 2017-06-04T16:12:41.457 回答
0

你在正确的轨道上。另一种方法是将 nodeIntegration 设置为 false 并预加载一个 js 文件,该文件将在 BrowserWindow 上下文中运行,并能够在进程加载事件触发后访问窗口对象。预加载 javascript 文件仅针对其自身具有完整的节点集成。

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

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

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

<!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>
于 2017-06-12T23:09:56.427 回答