Electron:如何获取 webContents(检查 backgroundThrottling)?

Electron: How to get webContents (to check on backgroundThrottling)?

要检查 backgroundThrottling 的状态,我想访问事件处理程序中的 webContents:

window.addEventListener('blur', ()=>{
    if(window.webContents.backgroundThrottling)
        ...
});

但是当事件发生时,我收到错误消息:“无法读取未定义的 属性 'backgroundThrottling'”。我尝试了一些东西,但我永远无法获得定义的 webContents。

根据要求我添加: 这在 index.html 中运行,所以渲染器进程? window“刚刚可用”。我不知道它是在哪里定义的(但既不在 index.html 中也不在 renderer.js 中)。 (我是 JS 的新手,甚至是 Electron 的新手。)

当我在 console.log 中打印 window 时,我得到:

global {window: global, self: global, document: ...

所以也许这不是浏览器 window,而是电子浏览器?但是我如何从 index.html 中获取浏览器 window(不是创建一个新的,而是获取当前的浏览器)?

编辑:但是因为它包含我文档中的所有内容,所以它应该是浏览器 window,不是吗?

TL;DR: 出于安全原因,window 全局变量不是 Electron BrowserWindow 而是纯粹的 window 全局变量'll 在任何给定的(实际)浏览器中找到。您需要在主进程上执行您的代码。


webContents 是 Electron 的附加 class,因此仅在主进程中可用(参见 its docs)。因此,渲染器进程(您打开的window)无法访问它。

window.webContents 仅当 windowBrowserWindow class 的变量时才有效,这意味着您需要在打开时在主进程中创建的相同对象window.

要解决您的问题,请通过 IPC(进程间通信)向事件侦听器中的主进程传递消息,并根据 ipcMain 侦听器中的 webContents.backgroundThrottling 执行代码。可以这样做:

// renderer process (index.html)
const { ipcRenderer } = require ("electron");

window.addEventListener ("blur", () => { ipcRenderer.send ("window-blur", null); });
// main process (app.js or main.js or whatever you call it)
const { ipcMain } = require ("electron");

ipcMain.on ("window-blur", (event, args) => {
    // args === null, but event.sender is the webContents of the BrowserWindow
    // which sent the event
    if (event.sender.backgroundThrottling) {
        // ...
    }
});

然而,阅读 Electron 中的 IPC 可能是值得的(有更优雅的方法来解决这个问题,特别是如果你只需要来自主进程的 webContents.backgroundThrottling)以及双重 -进程模型(主进程和渲染进程),因为您似乎缺乏对这个概念的理解,这个概念不仅对 Electron 编程很重要,而且对 Electron 应用程序的安全性也很重要。

这些文档页面可能是有用的资源:


注意:以上代码只有在创建时在 BrowserWindow 的设置中将 nodeIntegration 设置为 true 时才有效。然而,这不在这个问题的范围内,之前已经在 SO 上回答过无数次了。

我希望post这里的最终解决方案是可以的,这是我通过之前的回答找到的?

任务是在后台节流处于活动状态时暂停时钟。由于反应必须在渲染器进程中发生,因此需要将渲染器进程事件从主镜像反射回渲染器,但前提是后台节流处于活动状态:

//-------------------------------------
// renderer process (index.html):

const { ipcRenderer }   = require ("electron")
let boolClockPausing    = 0 // stores the user's setting, not the pausing because of throttling

window.addEventListener ( "blur",  ()=>{
    if(!boolClockPausing) ipcRenderer.send ("window-focus", 0) })   // tell main about blur

window.addEventListener ( "focus", ()=>{
    if(!boolClockPausing) ipcRenderer.send ("window-focus", 1) })   // tell main about focus

// main tells us our previous blur/focus event happened with bgThrottling:
ipcRenderer.on ("window-focus-throttling", (event, boolFocus)=>{
    ClockOff( boolFocus ? 0 : 1 ) })

//-------------------------------------
// main process (main.js):

const { ipcMain } = require ("electron");

ipcMain.on ("window-focus", (event, boolFocus) => {
    const webContents = event.sender
    if (webContents.backgroundThrottling)
        webContents.send ("window-focus-throttling", boolFocus)
})

通过一个工作示例,我发现从 Electron 文档中查找和理解更多信息要容易得多。