我将尝试用简短的段落描述一个最小化的问题.
I'll try to describe a minimized question in short paragraphs.
简而言之,我想在我的电子应用程序 in 的网页中使用我的电子应用程序中的一些逻辑或调用一些函数(我实际上正在为我的网页包装一个电子应用程序外壳").
In short, I want to use some logic or call some functions in my Electron App from the webpage that is in my Electron App (I am actually wrapping an electron app 'shell' for my webpage).
假设我想在我的 Electron 应用程序中公开一个函数.说,
Suppose I want to expose a function in my Electron app. Say,
function printNumbers () {
console.log(1)
}
注意它应该位于我的 Electron 代码中.
notice that it should be located in my Electron code.
然后在运行我的应用程序后,我想从我的网页调用此函数(单击从网站加载的网页中的按钮,然后在我的 Electron 应用程序中打开一个新窗口).现在,我想我可以使用开发人员控制台检查 printNumber 是否有效.
Then after running my app, I'd like to call this function from my webpage(clicking a button in my webpage which is loaded from a website, then open a new window in my Electron App). For now, I think I can check whether printNumber works by using the developer console.
我已经检查了如何使用 remote
模块来调用电子内部的函数/模块.但是我没有找到一种方法来调用我在电子代码库中编写的函数.
I have checked how to use remote
module to call functions/modules inside electron. But I didn't find a way to call a function I write in my electron code base.
顺便说一句:我可以启用 nodeIntegration
选项.
BTW:
I can enable the nodeIntegration
option.
渲染器进程和主进程之间的通信主要有两种方式.
There are two main ways to communicate between the renderer process and the main process.
1. 一种方法是使用 remote 模块需要你想从主进程中获取的代码.该对象将包含您从主流程代码中导出的任何内容.
1. One way would be to use the remote module to require the code you want to take from the main process. This object will contain anything you export from your main process code.
// main process, for example app/main.js
exports.test = () => console.log('Yay');
// renderer process, for example app/renderer.js
const { remote } = require('electron');
const mainProcess = remote.require('./main.js');
mainProcess.test(); // 'Yay'
2. 另一种方法是使用 进程间通信 在主进程和渲染进程之间发送/接收事件:
2. Another way would be to use Inter Process Communication to send/receive events between the main process and the renderer process:
// main process, for example app/main.js
myWindow.webContents.send('my-cool-log-event', 'Yay');
// renderer process, for example app/renderer.js
const { ipcRenderer } = require('electron');
ipcRenderer.on('my-cool-log-event', (evt, msg) => console.log(msg)); // 'Yay'
如果您想在渲染器进程中触发点击事件时从主进程调用函数,您可以使用任一方法.
If you want to call a function from the main process when a click event fires in a renderer process, you can use either approach.
1.
// main process, for example app/main.js
exports.onClick = () => console.log('Yay');
// renderer process, for example app/renderer.js
const { remote } = require('electron');
const mainProcess = remote.require('./main.js');
document
.querySelector('#elem')
.addEventListener('click', () => {
mainProcess.onClick();
});
2.
// main process, for example app/main.js
const { ipcMain } = require('electron')
ipcMain.on('click', () => console.log('do something'));
// renderer process, for example app/renderer.js
const { ipcRenderer } = require('electron');
document
.querySelector('#elem')
.addEventListener('click', () => {
ipcRenderer.send('click');
});
这篇关于如何从我的网页调用 Electron 中的函数/模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!