Electron: Call renderer function from main

前端 未结 2 796
长情又很酷
长情又很酷 2021-02-15 17:27

I have some data in the localstorage that has to be deleted on app.quit(). But I see no way to do so from the main process.

Is there a way to call a

2条回答
  •  没有蜡笔的小新
    2021-02-15 17:48

    You can send messages from the main process to a renderer process via webContents.send as called out in the documentation here: https://github.com/atom/electron/blob/master/docs/api/web-contents.md#webcontentssendchannel-arg1-arg2-.

    Here is how you do it straight from the docs:

    In the main process:

    // In the main process.
    var window = null;
    app.on('ready', function() {
      window = new BrowserWindow({width: 800, height: 600});
      window.loadURL('file://' + __dirname + '/index.html');
      window.webContents.on('did-finish-load', function() {
        window.webContents.send('ping', 'whoooooooh!');
      });
    });
    

    In index.html:

    
    
    
      
    
    
    

    Note it is asynchronous. I am not sure how that affects things with your particular solution, but this should at least get you talking back to the renderer process.

提交回复
热议问题