Problem
Hello, I'm an Electron beginner.
I understand that using contextBridge is a good way to make a secure app in Electron.
I want to implement a contextMenu(right-click menu) in contextBridge to specify what kind of process to do to the Main process in the Renderer process, but I can't do that.
Could you give me some advice?
My code
main.js(Main process)(part of initialization)
function createWindow() {
mainWindow = new BrowserWindow({
width: 960,
height: 540,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: __dirname + "/preload.js"
}
});
mainWindow.loadFile(__dirname + "/index.html");
}
preload.js
const contextBridge = require("electron");
const remote = require("electron").remote;
const Menu =remote.Menu;
const MenuItem=remote.MenuItem;
contextBridge.exposeInMainWorld(
"api", {
menu:()=>{
const menu = new Menu();
// label definition
menu.append(new MenuItem({
label: 'MenuItem1',
click() {
console.log('item 1 clicked');
}
}));
// right clicked
window.addEventListener('contextmenu', (e) => {
e.preventDefault();
menu.popup({
window: remote.getCurrentWindow()
});
}, false);
}
}
);
index.html(Renderer process)
<!DOCTYPE html>
<html>
<head>
<script>
window.api.menu();
</script>
</head>
<body>
</body>
</html>
What I tried
In the beginning, this was a failure.
Changing webreferences in main.js to nodeIntegration:true only (without using contextBridge) and putting the menu of preload.js to <script> in index.html, I could implement the contextMenu.
However I know this way can't make a secure app because not using contextBridge.
main.js
function createWindow() {
mainWindow = new BrowserWindow({
width: 960,
height: 540,
webPreferences: {
nodeIntegration: true // changing false to true, deleting the others
}
});
mainWindow.loadFile(__dirname + "/index.html");
}
preload.js
No changes(because didn't load)
index.html
<!DOCTYPE html>
<html>
<head>
<script>
// add the following
const remote = require("electron").remote;
const Menu =remote.Menu;
const MenuItem=remote.MenuItem;
const menu = new Menu();
// label definition
menu.append(new MenuItem({
label: 'MenuItem1',
click() {
console.log('item 1 clicked');
}
}));
// right cliced
window.addEventListener('contextmenu', (e) => {
e.preventDefault();
menu.popup({
window: remote.getCurrentWindow()
});
}, false);
</script>
</head>
<body>
</body>
</html>
Thank you for your reading this to the end.