首页 文章

ejs-electron不使用电子伪造

提问于
浏览
0

在我的电子伪造应用程序中,在Windows 10上运行,虽然没有可见的错误,但不会渲染ejs模板 . 我可以使用创建的应用程序重现该问题

electron-forge init ejs-test

我正在使用electronic-forge 5.2.2和ejs-electron 2.03这是我的index.js文件:

import { app, BrowserWindow } from 'electron'

import * as ejse from 'ejs-electron'

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit()
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
  })

  ejse.data('testdata', 'Generated throuh EJS')
  // and load the index.html of the app.
  mainWindow.loadFile(`${__dirname}/index.ejs`)

  // Open the DevTools.
  mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', () => {
    // 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
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

这是我的index.ejs文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <%= testdata %>
  </body>
</html>

当我用npm start运行时

我没有错误,但<%= testdata%>按原样显示,而不是被替换 . 在加载我的ejs文件之前调用ejse.listening()会返回true .

相同的代码适用于非伪造电子应用程序 .

你能帮我吗?

做了一些调查,我发现了ejs-electron中的关键线

protocol.interceptBufferProtocol('file', protocolListener)

返回以下错误:错误:该方案已被截获

1 回答

  • 1

    这与https://github.com/electron-userland/electron-forge/issues/291的问题相同 . 鉴于目前缺乏文档,升级到electron-forge v6太难了,所以我找到了一个解决方法 .

    我需要使用ejs wasto的原因是基于多种语言的消息文件动态生成我的html代码 . 我首先尝试动态生成它并使用loadURL和'data:text / html'协议 . 但是,这会产生很多问题,因为html与文件路径无关,因此任何引用都是如此

    所以我转而在构建时为每种语言生成一个html文件,并在运行时选择根据语言加载的文件 .

    所以有两个步骤:我在package.json中添加了以下内容:

    "scripts": {
        "build" : "node build.js"
      },
    

    而build.js是:

    const ejs = require('ejs')
    const fs = require('fs')
    let langs = ['en', 'fr']
    langs.forEach((lang) => {
      let msgdata = JSON.parse(fs.readFileSync(`${__dirname}/src/messages/${lang}.json`, 'utf8'))
    
      ejs.renderFile(`${__dirname}/src/views/pages/mindmap.ejs`,
        {
          'messages': msgdata,
          'otherlanguage': 'fr',
        },
        {},
        (err, str) => {
          fs.writeFileSync(`${__dirname}/src/mindmap-${lang}.html`, str, 'utf8')
        })
    })
    

    因此,npm run build会生成与我有语言文件一样多的html文件 .

    然后我的应用程序的index.js文件几乎是锅炉板电子伪造:

    const { app, BrowserWindow, ipcMain } = require('electron')
    const ejs = require('ejs')
    const fs = require('fs')
    
    // Handle creating/removing shortcuts on Windows when installing/uninstalling.
    if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
      app.quit()
    }
    
    // Keep a global reference of the window object, if you don't, the window will
    // be closed automatically when the JavaScript object is garbage collected.
    let mainWindow
    
    const createWindow = () => {
      // Create the browser window.
      mainWindow = new BrowserWindow()
    
      // and load the index.html of the app.
      mainWindow.loadURL(`file://${__dirname}/mindmap-${lang}.html`)
    
      // Emitted when the window is closed.
      mainWindow.on('closed', () => {
        // 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
      })
    }
    
    // This method will be called when Electron has finished
    // initialization and is ready to create browser windows.
    // Some APIs can only be used after this event occurs.
    app.on('ready', createWindow)
    
    // Quit when all windows are closed.
    app.on('window-all-closed', () => {
      // On OS X it is common for applications and their menu bar
      // to stay active until the user quits explicitly with Cmd + Q
      if (process.platform !== 'darwin') {
        app.quit()
      }
    })
    
    app.on('activate', () => {
      // On OS X it's common to re-create a window in the app when the
      // dock icon is clicked and there are no other windows open.
      if (mainWindow === null) {
        createWindow()
      }
    })
    
    // In this file you can include the rest of your app's specific main process
    // code. You can also put them in separate files and import them here.
    ipcMain.on('request-messages', (event, arg) => {
      mainWindow.webContents.send('messages', msgdata)
    })
    

相关问题