javascript - ContextBridge在preload.js中仍然未定义,给出错误: Cannot read property 'exposeInMainWorld' of undefined in packaged Electron app?

标签 javascript electron electron-builder

我有一个包裹在Electron中的角度应用程序。我已经使用 Electron 生成器生成了安装程序。根据与渲染器进程通信的建议,我已将preload.js用作preload脚本。
该脚本在开发环境中可以正常工作。但是,一旦打包并安装了应用程序,它将显示错误无法读取未定义的属性'exposeInMainWorld'
这是我的preload.js

window.onload = () => {
    const {
        contextBridge,
        ipcRenderer
    } = require("electron");

    const validChannels = ['event-1', 'event-2', 'event-3'];

    // Expose protected methods that allow the renderer process to use the ipcRenderer without exposing the entire object
    contextBridge.exposeInMainWorld(
        "api", {
        send: (channel, data) => {
            // whitelist channels To Main Process
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        receive: (channel, func) => {
            // From Main Process
            if (validChannels.includes(channel)) {
                console.log('receive: ' + channel);
                ipcRenderer.on(channel, (event, ...args) => func(...args));
            }
        }
    }
    );
};

我的Main.js
        this._win = new BrowserWindow({
            width: 1024,
            height: 768,
            webPreferences: {
                nodeIntegration: false,
                webSecurity: true,
                allowEval: false,
                allowRunningInsecureContent: false,
                contextIsolation: true, // protect against prototype pollution
                enableRemoteModule: false, // turn off remote
                preload: path.join(__dirname, "./preload.js") // use a preload script
            },
            title: this._appTitle,
            autoHideMenuBar: true,
            icon: path.join(__dirname, '/../dist/some-path/favicon.ico')
        });
我将main.jspreload.js都保留在根目录中的文件夹 Electron 文件中,该文件夹可以使用package.json。如果您需要检查我的builder-config.yaml,它位于this link
请提出建议。
P.S. :在开发人员模式下,preload.js绝对可以正常工作。问题仅在将应用程序与 Electron 生成器打包在一起之后

最佳答案

我写的时候遇到了这样的错误:

getPlatform: () => window.remote.getPlatform(),
然后我得到Uncaught Error: Uncaught TypeError: Cannot read property 'getPlatform' of undefined
console.log('window.remote.getPlatform()', window.remote.getPlatform && window.remote?.getPlatform())
当我将其更改为
getPlatform: () => process.platform,
一切正常。

关于javascript - ContextBridge在preload.js中仍然未定义,给出错误: Cannot read property 'exposeInMainWorld' of undefined in packaged Electron app?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63023636/

相关文章:

javascript - JavaScript 中最快的阶乘函数是什么?

window - Electron 应用始终始终以全角放在顶部,其他窗口应放在其下方

electron - 在使用 Electron 更新程序更新之前请参阅发行说明

windows-installer - 为 windows 创建应用程序安装程序 - Electron (electron-packager, electron-builder)

javascript - 仅使用 Electron (Atom shell) 和 PHP+mysql 服务器的身份验证表单

node.js - package Electron 产生空白页,没有内容

Javascript 提示符 : best practice?

javascript - 将 onclick 附加到除一个链接之外的所有 jquery

javascript - 为什么 Facebook、Disqus、Google、Twitter 等服务告诉我们

javascript - 打包的 Electron 应用程序找不到本地模块