met*_*lah 2 javascript electron
我正在使用https://github.com/maxogden/menubar使用 Electron 创建菜单栏桌面应用程序。然而,我正在努力建立基本的 IPC 通信。知道为什么下面的代码不起作用吗?为了澄清这一点,我希望test在应用程序启动时注销到控制台,但事实并非如此。
应用程序.js
const { app } = require('electron');
const Menubar = require('menubar');
const menubar = Menubar.menubar({
index: `file://${__dirname}/index.html`,
preloadWindow: true,
icon: './assets/img/icon.png',
});
try {
require('electron-reloader')(module)
} catch (_) { }
app.on('ready', () => {
menubar.window.webContents.send('test');
});
Run Code Online (Sandbox Code Playgroud)
渲染器.js
const { ipcRenderer } = require('electron');
ipcRenderer.on('test', () => {
console.log('test');
});
Run Code Online (Sandbox Code Playgroud)
索引.html
<html>
<head>
<title>Test</title>
<script>
require('./renderer')
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这可能是假阴性。
我希望应用程序启动时测试能够注销到控制台,但事实并非如此。
调用的输出位置console.log取决于进行这些调用的位置:
从主线程:查看启动应用程序的终端。
从渲染器线程:查看 Chrome DevTools 控制台。
因此,请确保您在正确的位置寻找预期的输出。
如果这不起作用,那么这里有一个关于如何在主进程和渲染器进程之间设置 IPC 通信的小演示。
main.js
nodeIntegration您会注意到我确实将和 都设置contextIsolation为默认值。这样做的目的是为了明确您不需要降低应用程序的安全栏来允许主进程和渲染器进程之间的消息。
这里发生了什么?
主进程会等待渲染器完成加载,然后再发送“ping”消息。IPC 通信将由预加载脚本处理。
记下该console.log通话并查看它在下面的截屏视频中出现的位置。
const {app, BrowserWindow} = require('electron'); // <-- v15
const path = require('path');
app.whenReady().then(() => {
const win = new BrowserWindow({
webPreferences: {
devTools: true,
preload: path.resolve(__dirname, 'preload.js'),
nodeIntegration: false, // <-- This is the default value
contextIsolation: true // <-- This is the default value
}
});
win.loadFile('index.html');
win.webContents.openDevTools();
win.webContents.on('did-finish-load', () => {
win.webContents.send('ping', '');
});
// This will not show up in the Chrome DevTools Console
// This will show up in the terminal that launched the app
console.log('this is from the main thread');
});
Run Code Online (Sandbox Code Playgroud)
预加载.js
我们正在使用contextBridgeAPI。这允许向渲染器进程公开特权 API,而无需启用nodeIntegration或破坏上下文隔离。
该 API 将在一个非常愚蠢的命名空间 (BURRITO) 下提供,以明确您可以更改此设置。
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('BURRITO', {
whenPing: () => new Promise((res) => {
ipcRenderer.on('ping', (ev, data) => {
res(data);
});
})
});
Run Code Online (Sandbox Code Playgroud)
渲染器.js
使用预加载脚本提供的 API,我们开始监听 ping 消息。当我们获得它时,我们将主进程传递的数据放入渲染器页面中。我们还记录了一条消息,您可以在下面的截屏视频中看到。
BURRITO.whenPing().then(data => {
document.querySelector('div').textContent = data;
// This will show up in the Chrome DevTools Console
console.log(`this is the renderer thread, received ${data} from main thread`);
});
Run Code Online (Sandbox Code Playgroud)
索引.html
const {app, BrowserWindow} = require('electron'); // <-- v15
const path = require('path');
app.whenReady().then(() => {
const win = new BrowserWindow({
webPreferences: {
devTools: true,
preload: path.resolve(__dirname, 'preload.js'),
nodeIntegration: false, // <-- This is the default value
contextIsolation: true // <-- This is the default value
}
});
win.loadFile('index.html');
win.webContents.openDevTools();
win.webContents.on('did-finish-load', () => {
win.webContents.send('ping', '');
});
// This will not show up in the Chrome DevTools Console
// This will show up in the terminal that launched the app
console.log('this is from the main thread');
});
Run Code Online (Sandbox Code Playgroud)
使用以下命令运行应用程序:
npx electron main.js
Run Code Online (Sandbox Code Playgroud)
您可以看到这两个console.log调用在两个不同的位置产生了输出。