Electron: Call renderer function from main

前端 未结 2 737
长情又很酷
长情又很酷 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:56

    You might use BrowserWindow.webContents.executeJavaScript like so in your main process:

    // will print "whoooooooh!" in the dev console
    window.webContents.executeJavaScript('console.log("whoooooooh!")');
    

    Although you might consider it a kinda messy/dirty approach, it works. And it does not require setting up anything in the renderer process which greatly simplified things for me.
    If you just want to call a particular method it would probably be quicker to write this way.

提交回复
热议问题