简体   繁体   English

我如何使用 webContents.executeJavaScript 执行在 preload.js 中定义的函数

[英]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. /* 我想调用一个在 preload.js 文件中定义的函数,但它得到一个错误。 Instead, I just execute an alert (" aaaaaa "), which does the job correctly */ main process:相反,我只是执行一个警报(“aaaaaa”),它正确地完成了工作 */ 主进程:

 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 /------------------------------------------------- ------------------------------------------------/ 渲染器预加载.js

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

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

but it does not work!但它不起作用! enter image description here

can use contextBridge to export api to mainprocess可以使用contextBridge将api导出到mainprocess

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 webContents.send vs webContents.executeJavaScript - webContents.send vs webContents.executeJavaScript 如何在 Electron 中正确使用 preload.js - How to use preload.js properly in Electron 如何从 electron 的渲染器进程调用 preload.js 中定义的 function - How to call function defined in preload.js from renderer process in electron Electron.js 如何在 preload.js 中调用 function 并在 main.js 中使用 - Electron.js how to invoke a function in preload.js and use in main.js 从禁用了NodeIntegration的Electron的webContents.executeJavaScript获取返回值-可能吗? - Get a return value from Electron's webContents.executeJavaScript with NodeIntegration disabled - Possible? 在 Electron 中,如何通过 preload.js 中定义的全局变量使 ipcRenderer 可用于多个渲染器进程? - In Electron, how to make ipcRenderer available to multiple renderer processes via global variable defined in preload.js? 无法在我的 preload.js 中使用模块“dotenv”,即使我的依赖项中有它 - Cannot use module "dotenv" in my preload.js even if I have it in my dependencies Electron preload.js 无法正常运行 - Electron preload.js doesn't function the way it is suppose to 电子webContents executeJavaScript:无法在loadURL上每秒执行脚本 - Electron webContents executeJavaScript : Cannot execute script on second on loadURL Preload.js 需求问题 - Preload.js require problems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM