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

5k Views Asked by At

i try to excuteJavaScript in electron

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;
    })
}

if username is a number it work correctly, if username is a string, like below, it show error code

(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.

i have try with tag and it have the same problem

3

There are 3 best solutions below

0
On BEST ANSWER

I saw @Bravo answered your question through a comment, but just to improve it, since you're using a template string, you could just:

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

It's cleaner that way (since this is the main purpose of a template string, not only allowing to have a multiline text) and you avoid doing string concatenation with the "+" operator.

0
On

I was taking a UDEMY course called Master Electron: Desktop Apps with HTML, JavaScript & CSS, by Ray Viljoen

And in Lesson 3.17. Session: DownloadItem, I got this same error:

(node:9552) UnhandledPromiseRejectionWarning: Error: Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.
    at WebFrame.e.startsWith.e.startsWith.WebFrame.<computed> [as _executeJavaScript] (electron/js2c/renderer_init.js:87:1542)
    at electron/js2c/renderer_init.js:139:429
    at electron/js2c/renderer_init.js:123:361
    at EventEmitter.<anonymous> (electron/js2c/renderer_init.js:127:872)
    at EventEmitter.emit (events.js:223:5)
    at Object.onMessage (electron/js2c/renderer_init.js:115:818)
(node:9552) 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:9552) [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.

In my case I tracked the bug/problem down to some code that had a divide by zero in the event handler for DownloadItem.on. The code snippet in question is:

  ses.on('will-download', (e, downloadItem, webContents) => {

    let fileName = downloadItem.getFilename()
    let fileSize = downloadItem.getTotalBytes() // this was zero (which caused the bug)
    downloadItem.on('updated', (e, state) => {
      let received = downloadItem.getReceivedBytes()
      if (state === 'progressing' && received) {
// THE NEXT LINE CAUSED the divide by zero exception because fileSize was zero.
          let progress = Math.round((received/fileSize)*100)
          webContents.executeJavaScript(`window.progress.value = ${progress}`)
      }
    })
  })

So my takeaway is that Electron's stack didn't help me identify where the bug is but the but was a normal coding error. Not having an exception that included a line number of my code made this particularly difficult to debug/isolate.

The underlying problem was caused by the URL being used when the course was written no longer working. From site: https://file-examples.com/.

1
On

Sometimes you will mess with single quote and quote mark. I made an util function to log everywhere.

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