// and load the index.html of the app. mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true }))
// Open the DevTools. // mainWindow.webContents.openDevTools()
// Emitted when the window is closed. mainWindow.on('closed', function () { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. mainWindow = null }) }
接下来的事情就交给这个 HTML 文件了,你可以根据自己的实际需要随意编写,在 base64-convertor 这个应用中,我需要在前端展示一个 “一键转换” 的功能,所以需要一个输入框和一个输出结果的框,以及一个 “转换” 按钮。
然后我们需要在点击转换按钮时,调用 js-base64 的 API 将用户输入的字符串进行转换。首先在 index.html 里面引入 JS 脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!DOCTYPE html> <html> <head> <!-- 省略... --> </head> <body> <!-- 省略... --> <script> // You can also require other files to run in this process require('./renderer.js') </script> </body> </html>
注意到我们可以直接在页面内使用 require 方法,这个和一般的 Web 网页开发有所不同。在 renderer.js 里面我们编写实际的逻辑:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// renderer.js
constBase64 = require('js-base64').Base64
let inputEl = document.getElementById('input') let outputEl = document.getElementById('output') let convertBtn = document.getElementById('convert')
convertBtn.addEventListener('click', () => { const val = inputEl.value const output = Base64.encode(val) // 调用 API