如何从我的网页调用 Electron 中的函数/模块?

How to call a function/module in Electron from my webpage?(如何从我的网页调用 Electron 中的函数/模块?)
本文介绍了如何从我的网页调用 Electron 中的函数/模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我将尝试用简短的段落描述一个最小化的问题.

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 中的函数/模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)