Javascript 如何从渲染器进程调用Electron API方法?

Javascript 如何从渲染器进程调用Electron API方法?,javascript,jquery,electron,Javascript,Jquery,Electron,我已经试了一段时间了。我遵循了这个原则,并成功地使用Bootstrap和Jquery制作了一个“应用程序” 但现在,我尝试使用Electron API方法,但没有成功 我创建了一个浏览器窗口,并在该窗口中添加了一个新的JS文件。在该文件中,我尝试在此处调用printToPDF方法: 它就是不工作,控制台会记录以下内容: 未捕获引用错误:未定义主窗口 代码如下: main.js const electron = require('electron') const app = electron.ap

我已经试了一段时间了。我遵循了这个原则,并成功地使用Bootstrap和Jquery制作了一个“应用程序”

但现在,我尝试使用Electron API方法,但没有成功

我创建了一个浏览器窗口,并在该窗口中添加了一个新的JS文件。在该文件中,我尝试在此处调用printToPDF方法:

它就是不工作,控制台会记录以下内容:

未捕获引用错误:未定义主窗口

代码如下:

main.js

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
 mainWindow = new BrowserWindow({width: 800, height: 600})
 mainWindow.loadURL(`file://${__dirname}/index.html`)
 mainWindow.webContents.openDevTools()

 mainWindow.on('closed', function () {
  mainWindow = null
 })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
 if (process.platform !== 'darwin') {
  app.quit()
 }
})

app.on('activate', function () {
 if (mainWindow === null) {
  createWindow()
 }
})
index.html

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Hello World!</title>
 <link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
</head>
<body>
</body>

<script>window.$ = window.jQuery = require('jquery');</script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script>
 require('./app.js');
</script>
</html>

查看仪表板组合仪表(ipc)模块和。ipc模块允许您在主进程和渲染进程之间发送和接收同步和异步消息

下面是应用程序中的打印到PDF示例

渲染器进程

const ipc = require('electron').ipcRenderer

const printPDFBtn = document.getElementById('print-pdf')

printPDFBtn.addEventListener('click', function (event) {
  ipc.send('print-to-pdf')
})

ipc.on('wrote-pdf', function (event, path) {
  const message = `Wrote PDF to: ${path}`
  document.getElementById('pdf-path').innerHTML = message
})
const fs = require('fs')
const os = require('os')
const path = require('path')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipc = electron.ipcMain
const shell = electron.shell

ipc.on('print-to-pdf', function (event) {
  const pdfPath = path.join(os.tmpdir(), 'print.pdf')
  const win = BrowserWindow.fromWebContents(event.sender)
  // Use default printing options
  win.webContents.printToPDF({}, function (error, data) {
    if (error) throw error
    fs.writeFile(pdfPath, data, function (error) {
      if (error) {
        throw error
      }
      shell.openExternal('file://' + pdfPath)
      event.sender.send('wrote-pdf', pdfPath)
    })
  })
})
主流程

const ipc = require('electron').ipcRenderer

const printPDFBtn = document.getElementById('print-pdf')

printPDFBtn.addEventListener('click', function (event) {
  ipc.send('print-to-pdf')
})

ipc.on('wrote-pdf', function (event, path) {
  const message = `Wrote PDF to: ${path}`
  document.getElementById('pdf-path').innerHTML = message
})
const fs = require('fs')
const os = require('os')
const path = require('path')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipc = electron.ipcMain
const shell = electron.shell

ipc.on('print-to-pdf', function (event) {
  const pdfPath = path.join(os.tmpdir(), 'print.pdf')
  const win = BrowserWindow.fromWebContents(event.sender)
  // Use default printing options
  win.webContents.printToPDF({}, function (error, data) {
    if (error) throw error
    fs.writeFile(pdfPath, data, function (error) {
      if (error) {
        throw error
      }
      shell.openExternal('file://' + pdfPath)
      event.sender.send('wrote-pdf', pdfPath)
    })
  })
})

查看仪表板组合仪表(ipc)模块和。ipc模块允许您在主进程和渲染进程之间发送和接收同步和异步消息

下面是应用程序中的打印到PDF示例

渲染器进程

const ipc = require('electron').ipcRenderer

const printPDFBtn = document.getElementById('print-pdf')

printPDFBtn.addEventListener('click', function (event) {
  ipc.send('print-to-pdf')
})

ipc.on('wrote-pdf', function (event, path) {
  const message = `Wrote PDF to: ${path}`
  document.getElementById('pdf-path').innerHTML = message
})
const fs = require('fs')
const os = require('os')
const path = require('path')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipc = electron.ipcMain
const shell = electron.shell

ipc.on('print-to-pdf', function (event) {
  const pdfPath = path.join(os.tmpdir(), 'print.pdf')
  const win = BrowserWindow.fromWebContents(event.sender)
  // Use default printing options
  win.webContents.printToPDF({}, function (error, data) {
    if (error) throw error
    fs.writeFile(pdfPath, data, function (error) {
      if (error) {
        throw error
      }
      shell.openExternal('file://' + pdfPath)
      event.sender.send('wrote-pdf', pdfPath)
    })
  })
})
主流程

const ipc = require('electron').ipcRenderer

const printPDFBtn = document.getElementById('print-pdf')

printPDFBtn.addEventListener('click', function (event) {
  ipc.send('print-to-pdf')
})

ipc.on('wrote-pdf', function (event, path) {
  const message = `Wrote PDF to: ${path}`
  document.getElementById('pdf-path').innerHTML = message
})
const fs = require('fs')
const os = require('os')
const path = require('path')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipc = electron.ipcMain
const shell = electron.shell

ipc.on('print-to-pdf', function (event) {
  const pdfPath = path.join(os.tmpdir(), 'print.pdf')
  const win = BrowserWindow.fromWebContents(event.sender)
  // Use default printing options
  win.webContents.printToPDF({}, function (error, data) {
    if (error) throw error
    fs.writeFile(pdfPath, data, function (error) {
      if (error) {
        throw error
      }
      shell.openExternal('file://' + pdfPath)
      event.sender.send('wrote-pdf', pdfPath)
    })
  })
})

在提供的html/js中,您无法访问
main.js
中的任何内容。您可以在
app.js
中创建一个函数来发出事件,然后在
main.js
中侦听该事件并调用
mainWindow.webContents.printToPDF()会出现在那里。在您的html/js中,您无法访问
main.js
中的任何内容。您可以在
app.js
中创建一个函数来发出事件,然后在
main.js
中侦听该事件并调用
mainWindow.webContents.printToPDF()当事件被触发时,将显示在那里。谢谢。这正是我想要的。我仍在尝试找出这个“模块”模式。谢谢。这正是我想要的。我仍在试图找出这种“模块”模式。