I’ve started a desktop project with electron. But my button is not working, no any error. Here is the HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="assets/bootstrap.min.css">
</head>
<body>
<h1>gangway app</h1>
<button class="btn btn-primary" id="sendBtn">Send</button>
<script>
const electron = require("electron")
const {ipcRenderer} = electron
let sendBtn = document.querySelector("#sendBtn");
sendBtn.addEventListener('click',()=>{
alert("Alert")
ipcRenderer.send("key","Gangway Test")
})
</script>
</body>
</html>
So when I start my project I have just one button and it’s not working. I dont get any alert. Also I try to post data with ipcRenderer and catch it in main.js with ipcMain but also no any data came. Do I have something wrong with button id?
2
Answers
You cannot just use require("electron") inside an HTML file, that has been deprecated. You must have been following an old tutorial.
Please take a look at the Electron quick start guide.
EDIT: direct access to the Electron API from the renderer process has been deprecated in newer versions of Electron due to security concerns.
Electron has introduced Context Isolation instead. As mentioned in the documentaton, Context Isolation is a feature that ensures that both your preload scripts and Electron’s internal logic run in a separate context to the website you load in a webContents. This is important for security purposes as it helps prevent the website from accessing Electron internals or the powerful APIs your preload script has access to.
You need to create a preload script to expose the ipcRenderer send method to your renderer process.
You need to pass this script to the renderer process:
You should now be able to access the method in your renderer:
Please note that I haven’t tested this, but this is the rough idea of what you need to do based on the documentation.
the issue might be related to the context isolation in Electron.
Context Isolation is a security feature of Electron that allows scripts running in the renderer to make changes to its JavaScript environment without worrying about conflicting with the scripts in the Electron API or the preload script.
Starting from Electron 12, context isolation is enabled by default. This means that the
ipcRenderer
cannot be accessed directly from the renderer process because it’s only available in the preload script.You can create a preload script and expose specific IPC methods to your renderer process. Here’s an example of how you can do it:
Then in your HTML file, you can use these methods like this:
And in your main process:
Remember to set the path of your preload script in your
BrowserWindow
options:This way, you’re not disabling any security features.