electron中mainWindow.webContents.executeJavaScript()执行出错

在electron中执行

mainWindow.webContents.executeJavaScript(`alert(${params.selectionText})`)

出错:

 UnhandledPromiseRejectionWarning: Error: Script failed to execute, this normally means an error was thrown. Check the renderer console for 
the error.
    at node:electron/js2c/renderer_init:2:16766
    at EventEmitter.<anonymous> (node:electron/js2c/renderer_init:2:10978)
    at EventEmitter.emit (node:events:517:28)
    at Object.onMessage (node:electron/js2c/renderer_init:2:9092)
(Use `electron --trace-warnings ...` to show where the warning was created)
(node:12992) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without
 a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI
 flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

因为你使用的是模板字符串,所以代码修改为:

mainWindow.webContents.executeJavaScript(`alert('${params.selectionText}')`)

就可以了

复制内容


评论


乖,登录后才可以留言! 登录

Copyright © 2020-2023 春藤技术,春藤建站 All Rights Reserved
备案号:豫ICP备20020705号 公网安备 51LA统计
0.205250s