Как получить доступ к DOM BrowserWindow из удаленного в Electron - PullRequest
0 голосов
/ 28 июня 2019

Я пытаюсь создать электронное приложение, содержащее кнопку основного процесса, которая загружает новый BrowserWindow с удаленным модулем при нажатии.

Мне нужна вторая кнопка в этом новом BrowserWindow использовать команду exec из child_process.

Я пытаюсь addEventListener ко второй кнопке, но она не может получить доступ к DOM (только для getElementByID index.html)

фрагмент main.js

const {app, BrowserWindow} = require('electron')
require('electron-reload')(__dirname)
.
.
.
mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration:true
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

index.html

<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
   <button id="1" onclick="render()">Primary Click</button>
    <script src="./renderer.js"></script>
     <script src="./main.js"></script>
  </body>

скрипт рендерера (в main.js)

function render(){
  const { BrowserWindow } = require('electron').remote
  const { exec } = require("child_process")
  let win = new BrowserWindow({ width: 800, height: 600 })
  win.loadFile('index2.html')
  win.webContents.openDevTools()
  win.webContents.once("dom-ready", ()=>{
     document.getElementById("2").addEventListener("click", function(){
      exec("echo hello >> hello.txt") 
    })
  })
}

index2.html

<head>
    <meta charset="UTF-8">
    <title>Hello World2!</title>
  </head>
  <body>
    <button id="2">Secondary Click</button>
    <script src="./renderer.js"></script>
  </body>

Я получаюошибка:

Cannot read property 'addEventListener' of null

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...