简体   繁体   中英

how I use webContents.executeJavaScript execute a function that which be defined in preload.js

/* I want to call a function which defined in the preload.js file, but it getting an error. Instead, I just execute an alert (" aaaaaa "), which does the job correctly */ main process:

 const winViewBrowser = new BrowserView({ backgroundColor: '#ffffff', webPreferences: { contextIsolation:true, preload: path.join(__dirname, 'preload.js'), } }); win.setBrowserView(winViewBrowser); winViewBrowser.webContents.loadURL(url_local); winViewBrowser.webContents.on('dom-ready', () => { winViewBrowser.webContents.executeJavaScript(aa();, true).then((result) => { console.log(result); }) });

/-------------------------------------------------------------------------------------------------/ renderer preload.js

 aa = () => { alert(123123123); }

/-------------------------------------------------------------------------------------------------/

but it does not work! enter image description here

can use contextBridge to export api to mainprocess

https://www.electronjs.org/docs/api/context-bridge

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM