UnhandledPromiseRejectionWarning: E​​rror: Script failed to execute-Electron 执行脚本

Posted

技术标签:

【中文标题】UnhandledPromiseRejectionWarning: E​​rror: Script failed to execute-Electron 执行脚本【英文标题】:UnhandledPromiseRejectionWarning: Error: Script failed to execute- Electron execute script 【发布时间】:2020-02-14 05:54:02 【问题描述】:

我尝试在电子中执行 javascript

function createTestGmail(username, password)
    username = '12344444444444444444444444asdasd44444444'
    testGmail = new BrowserWindow(
        width: 500, 
        height:300, 
        backgroundColor:'#ccc', 
        title:'Kiểm tra Gmail',
        webPreferences: 
            nodeIntegration: true,
            nativeWindowOpen: true,
        
    );
    testGmail.loadURL('https://example.com');
    testGmail.webContents.openDevTools();
    testGmail.webContents.executeJavaScript(`
        console.log(`+username+`)    
    `)

    testGmail.on('closed',()=>
        testGmail = null;
    )

如果用户名是一个数字,它可以正常工作,如果用户名是一个字符串,如下所示,它显示错误代码

(node:3752) UnhandledPromiseRejectionWarning: Error: Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.
    at WebFrame.<computed> (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\api\web-frame.js:64:33)
    at WebFrame.executeJavaScript (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\common\api\deprecate.js:114:32)
    at C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\web-frame-init.js:11:43
    at C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\ipc-renderer-internal-utils.js:7:40
    at new Promise (<anonymous>)
    at EventEmitter.<anonymous> (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\ipc-renderer-internal-utils.js:7:9)
    at EventEmitter.emit (events.js:200:13)
    at Object.onMessage (C:\Users\Vy\Desktop\toolyoutube\node_modules\electron\dist\resources\electron.asar\renderer\init.js:42:16)
(node:3752) 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(). (rejection id: 1)
(node:3752) 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(). (rejection id: 1)
(node:3752) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:3752) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我尝试了标签,它有同样的问题

【问题讨论】:

如果用户名是一个字符串...console.log(+username+ etc 变成console.log(12344444444444444444444444asdasd44444444) 它必须是console.log('12344444444444444444444444asdasd44444444') - 因为这就是javascript的工作原理 【参考方案1】:

我看到@Bravo 通过评论回答了您的问题,但只是为了改进它,因为您使用的是 模板字符串,您可以:

testGmail.webContents.executeJavaScript(`console.log('$username')`) 

这样更简洁(因为这是模板字符串的主要目的,不仅允许多行文本),而且您避免使用“+”运算符进行字符串连接。

【讨论】:

【参考方案2】:

有时你会弄乱单引号和引号。 我做了一个 util 函数来记录任何地方。

function logEverywhere(mainWindow, message) 
  if (mainWindow && mainWindow.webContents) 
    mainWindow.webContents.executeJavaScript(`console.log(\`$message\`)`);
  

【讨论】:

谢谢你,我的问题已经解决了,但你下次给我一个很棒的提示

以上是关于UnhandledPromiseRejectionWarning: E​​rror: Script failed to execute-Electron 执行脚本的主要内容,如果未能解决你的问题,请参考以下文章

[Unhandled promise rejection: TypeError: null is not an object (evaluating '_reactNativeImageCropPic

等待 - 捕获错误 - UnhandledPromiseRejectionWarning

批量删除如何工作?

7月工作知识总计:

未处理的承诺拒绝 |重启命令

未处理的承诺拒绝警告(Cordova Angular)