【问题标题】:How can create subwindow send customer id in electron如何在电子中创建子窗口发送客户 ID
【发布时间】:2022-03-30 18:23:35
【问题描述】:

在电子应用中打开新窗口时如何发送客户ID

const popupLogin = (htmlFile, parentWindow, width, height) => {
    let modal = new BrowserWindow({
        width: 600,
        height: 500,
        modal: true,
        resizable: false,
        icon: path.join(__dirname + '/public/auxwall/logos/favicon.png'),
        parent: MainWindow,
        frame: false,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            contextIsolation: false,
            nodeIntegration: true,

        }
    })

    modal.loadFile(`${__dirname}/pages/Company/login.html`)

    return modal;
}

我尝试过 modal.loadFile(${__dirname}/pages/Company/login.html?id=1234) 但出现错误 电子:无法加载 URL:file:///Users/excelgraphics/Documents/Regular%20Jobs/OT/Aux%20service/Aux%20Services/pages/newService.html%3Fid=1234 错误:ERR_FILE_NOT_FOUND

我尝试这样做How to send a variable from the mainWindow to a childWindow in Electron JS?

但模态窗口无法从主进程访问消息

谁能帮我解决这个问题?

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:

    请记住,您的 modal 只是一个没有框架的窗口,可以使用 Inter-Process Communication 来回传递数据。

    要开始保护您的 Electron 应用程序,您应该认真阅读 Context Isolation

    要将id 传递给新创建的窗口,请使用{window}.webContents.send 方法。

    加载login.html 页面后,立即将id 发送到窗口。

    在你的主线程脚本中...

    const popupLogin = (htmlFile, parentWindow, width, height) => {
    
        // Get the customers id from whatever source necessary.
        let id = 99;
    
        // Create the window.
        let modal = new BrowserWindow({
            width: 600,
            height: 500,
            modal: true,
            resizable: false,
            icon: path.join(__dirname + 
     '/public/auxwall/logos/favicon.png'),
            parent: MainWindow,
            frame: false,
            show: false, // Hide possible flash and DOM update(s).
            webPreferences: {
                preload: path.join(__dirname, 'preload.js'),
                contextIsolation: true, // Set to true
                nodeIntegration: true,  // Node integration (removed in Electron v12)
            }
        })
    
        // Load the window.
        // Note: The loadFile() method returns a promise.
        modal.loadFile(`${__dirname}/pages/Company/login.html`)
            .then(() => { modal.webContents.send('login:id', id); })
            .then(() => { modal.show(); }) // Now show the modal
    
        return modal;
    }
    

    preload.js(主线程)

    const contextBridge = require('electron').contextBridge;
    const ipcRenderer = require('electron').ipcRenderer;
    
    // White-listed channels.
    const ipc = {
        'render': {
            // From render to main.
            'send': [],
            // From main to render.
            'receive': [
                'login:id'
            ],
            // From render to main and back again.
            'sendReceive': []
        }
    };
    
    contextBridge.exposeInMainWorld(
        // Allowed 'ipcRenderer' methods.
        'ipcRender', {
            send: (channel, args) => {
                let validChannels = ipc.render.send;
                if (validChannels.includes(channel)) {
                    ipcRenderer.send(channel, args);
                }
            },
            receive: (channel, listener) => {
                let validChannels = ipc.render.receive;
                if (validChannels.includes(channel)) {
                    // Deliberately strip event as it includes `sender`
                    ipcRenderer.on(channel, (event, ...args) => listener(...args));
                }
            },
            invoke: (channel, args) => {
                let validChannels = ipc.render.sendReceive;
                if (validChannels.includes(channel)) {
                    return ipcRenderer.invoke(channel, args);
                }
            }
        }
    );
    

    login.html(渲染线程)

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Title</title>
            <link rel="stylesheet" type="text/css" href="style.css">
            <script type="module" src="login.js"></script>
        </head>
        <body>
            ....
        </body>
    </html>
    

    login.js(渲染线程)

    (function() => {
            window.ipcRender.receive('login:id', (event, id) => { customerId(id); });
    })();
    
    function customerId(id) {
       console.log(id);
    }
    

    【讨论】:

    • 感谢您的帮助!我的应用程序没有使用任何外部网站 API,您认为启用上下文隔离是否好?因为当我设置真正的上下文隔离时我的一些脚本没有运行
    • 你可以通过设置contextIsolation: false 逃脱,但它确实会降低你的 Electron 应用程序的推荐安全性。即使您没有加载外部网站或使用他们的 API,恶意用户也可能会尝试通过您的 UI 表单字段或 DevTools(如果可访问)破坏您的应用程序。虽然这可能不是问题,但您需要确信不会通过任何跨上下文使用造成损害。 contextIsolation: 设置默认为 true,因此如果您想要(并且有时间)在 truefalse 之间切换并一一解决任何问题。如果它只有您使用你的应用程序就没有问题了......
    • 兄弟,你能帮我解决这个问题吗...问题链接在下面stackoverflow.com/questions/71265088/…
    猜你喜欢
    • 2015-02-20
    • 1970-01-01
    • 2013-04-03
    • 2017-06-05
    • 1970-01-01
    • 2021-05-23
    • 2012-07-07
    • 2022-01-13
    相关资源
    最近更新 更多