How to access DOM elements in electron?

后端 未结 3 985
我在风中等你
我在风中等你 2020-11-27 03:17

I am trying to add functionality to a button in index.html file is as follows: I have a button element in index.html

相关标签:
3条回答
  • 2020-11-27 03:58

    As stated in this tutorial:

    In Electron, we have several ways to communicate between the main process and renderer processes, such as ipcRenderer and ipcMain modules for sending messages, and the remote module for RPC style communication.

    So you can follow the example in https://github.com/electron/electron-api-demos. You should have a js file for each html. In that js file, you can use require anytime you want.

    Code in renderer.js:

    const ipc = require('electron').ipcRenderer
    
    const asyncMsgBtn = document.getElementById('async-msg')
    
    asyncMsgBtn.addEventListener('click', function () {
      ipc.send('asynchronous-message', 'ping')
    })
    
    ipc.on('asynchronous-reply', function (event, arg) {
      const message = `Asynchronous message reply: ${arg}`
      document.getElementById('async-reply').innerHTML = message
    })
    

    Code in ipc.html:

    <script type="text/javascript">
      require('./renderer-process/communication/sync-msg')
      require('./renderer-process/communication/async-msg')
      require('./renderer-process/communication/invisible-msg')
    </script>
    
    0 讨论(0)
  • 2020-11-27 04:06

    DOM can not be accessed in the main process, only in the renderer that it belongs to.

    There is an ipc module, available on main process as well as the renderer process that allows the communication between these two via sync/async messages.

    You also can use the remote module to call main process API from the renderer, but there's nothing that would allow you to do it the other way around.

    If you need to run something in the main process as a response to user action, use the ipc module to invoke the function, then you can return a result to the renderer, also using ipc.

    Code updated to reflect actual (v0.37.8) API, as @Wolfgang suggested in comment, see edit history for deprecated API, if you are stuck with older version of Electron.

    Example script in index.html:

    var ipc = require('electron').ipcRenderer;
    var authButton = document.getElementById('auth-button');
    authButton.addEventListener('click', function(){
        ipc.once('actionReply', function(event, response){
            processResponse(response);
        })
        ipc.send('invokeAction', 'someData');
    });
    

    And in the main process:

    var ipc = require('electron').ipcMain;
    
    ipc.on('invokeAction', function(event, data){
        var result = processData(data);
        event.sender.send('actionReply', result);
    });
    
    0 讨论(0)
  • 2020-11-27 04:20

    You can use webContents.executeJavaScript(code[, userGesture, callback]) API to execute JavaScript code.

    for example:

    mainWindow.loadUrl('file://' + __dirname + '/index.html');
    mainWindow.webContents.on('did-finish-load', ()=>{
        let code = `var authButton = document.getElementById("auth-button");
                authButton.addEventListener("click",function(){alert("clicked!");});`;
        mainWindow.webContents.executeJavaScript(code);
    });
    
    0 讨论(0)
提交回复
热议问题