0

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

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

4

2 回答 2

1

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

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

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

于 2019-10-17T16:44:49.363 回答
1

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

function logEverywhere(mainWindow, message) {
  if (mainWindow && mainWindow.webContents) {
    mainWindow.webContents.executeJavaScript(`console.log(\`${message}\`)`);
  }
}
于 2020-03-19T08:40:15.910 回答