如何在子窗口上启用nodeIntegration

时间:2019-10-09 06:58:25

标签: node.js electron

我正在从事电子项目,该项目必须具有该应用程序的子窗口。我为主窗口设置了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,以便我可以使用电子读写功能?

1 个答案:

答案 0 :(得分:0)

  

功能字符串遵循标准浏览器的格式,但是每个   功能必须是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