UnhandledPromiseRejectionWarning: Error: Script failed to execute- Electron execute script

UnhandledPromiseRejectionWarning: Error: Script failed to execute- Electron execute script

我尝试在 electron 中执行 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.

我试过使用标签,它有同样的问题

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

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

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

有时候你会把单引号和引号弄乱。 我做了一个 util 函数来到处记录。

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