I want to invoke the variables defined in the preload from the Electron renderer process.
The Electron version is v19.0.4.
We have created the following code according to the tutorial on the official page:
However, undefined
appears.
Where is the cause?
const{app, BrowserWindow, ipcMain}=require("electron");
const path=require("path");
let mainWindow;
const createWindow=()=>{
mainWindow = new BrowserWindow({
width —800,
height —600,
useContentSize: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, "preload.js"),
},
});
mainWindow.loadFile("index.html");
mainWindow.webContents.openDevTools();
mainWindow.on("closed",()=>{
mainWindow = null;
});
};
const{contextBridge}=require('electron')
contextBridge.exoseInMainWorld('myAPI',{
desktop —true
})
...
<script src="./render.js"></script>
...
console.log (window.myAPI)
I solved myself.
It seems that using HTML frames was not working.
(For some reason, javascripts invoked from within the frame cannot be passed)
© 2024 OneMinuteCode. All rights reserved.