在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}')`)
就可以了