【问题标题】:How to enable nodeIntegration on child window如何在子窗口上启用 nodeIntegration
【发布时间】:2020-02-06 11:10:41
【问题描述】:

我正在开发必须具有应用程序子窗口的电子项目。我为主窗口设置了nodeIntegration: true,我也想将它应用到子窗口,但由于某种原因我不能。我需要启用 nodeIntegration 因为我想在子窗口上使用它。

为了创建子窗口,我使用了电子中的window.open 函数并将 nodeIntegration 设置为 true,但子窗口上的状态仍然为 false。

这是我的代码:

if(content === 'main'){
    let mainSheet = './sheets/main_sheet.html';

    //open in proxy window
    let readerWin   = window.open(mainSheet, '', `
    maxWidth=2048,
    maxHeight=2048,
    width=1920,
    height=1080,
    fullscreenable=true,
    contextIsolation=1,
    nodeIntegration=true,
    frame: false`)
}

这是子窗口的错误 image

我想在子窗口中创建读写文件系统。当我将另一个文件加载到main_sheet.html 时,出现上述错误。

这个 main_sheet.html 文件:

<!DOCTYPE html>
<html>
  <head>
    ....
    <script src="../fileSystem/filesystem.js"></script>
    ...
  </head>
  <body>
      .....
  </body>
</html>

和 filesystem.js 文件:

const fs = require("fs");

let saveBTN = document.getElementById('save-data');

saveBTN.addEventListener('click', (e) => {
    fs.writeFile("temp.txt", data, (err) => {
        if (err) console.log(err);
        console.log("Successfully Written to File.");
    });
})

如何使用window.open 函数在子窗口上启用nodeIntegration,以便我可以在电子中使用读写?

【问题讨论】:

标签: node.js electron


【解决方案1】:

特征字符串遵循标准浏览器的格式,但每个 feature 必须是 BrowserWindow 的选项字段。

试试,(将 contextIsolation 设置为 no

   `maxWidth=2048,
    maxHeight=2048,
    width=1920,
    height=1080,
    fullscreenable=yes,
    contextIsolation=no,
    nodeIntegration=yes,
    frame=no`

参考https://electronjs.org/docs/api/window-open

参考https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features

【讨论】:

  • 我已经尝试了上面的代码,但结果仍然相同:(错误仍然相同:(
  • @AriefBudiPrasetyo 请在此处发布您的错误消息
  • 错误信息是:未捕获的ReferenceError:require is not defined image
  • 我在帖子中添加了更多信息。对不起,如果它让你感到困惑。
  • @AriefBudiPrasetyo 嗨,找到更新的答案。它工作正常,我已验证
猜你喜欢
  • 2022-08-17
  • 2017-11-09
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2020-07-14
相关资源
最近更新 更多