2017-04-22 836 views
0

我正在測試Electron並專門使用executeJavaScript。我的項目使用POST請求登錄到網站,然後執行一些工作並使用同一會話加載第二個URL。在第二個URL中,我需要執行JS,但我不確定我做錯了什麼。Electron webContents executeJavaScript:無法在loadURL上執行第二個腳本

在這個例子中,我創建了一個虛擬版本,它模擬了兩個URL並在第二個地方執行JS。關於這裏發生了什麼的任何想法?

const {app, BrowserWindow} = require('electron'); 

let win; 

function createWindow() { 

    win = new BrowserWindow({width: 1000, height: 600}) 
    win.openDevTools(); 

    // First URL 
    win.loadURL('https://www.google.com') 

    // Once dom-ready 
    win.webContents.once('dom-ready',() => { 

    // THIS WORKS!!! 
    win.webContents.executeJavaScript(` 
     console.log("This loads no problem!"); 
    `) 

    // Second URL 
    win.loadURL('https://github.com/electron/electron'); 

    // Once did-navigate seems to function fine 
    win.webContents.once('did-navigate',() => { 

     // THIS WORKS!!! So did-navigate is working! 
     console.log("Main view logs this no problem...."); 

     // NOT WORKING!!! Why? 
     win.webContents.executeJavaScript(` 

     console.log("I canot see this nor the affects of the code below..."); 

     const form = document.querySelectorAll('form.js-site-search-form')[0]; 

     const input = form.querySelectorAll('input.header-search-input')[0] 

     input.value = 'docs'; 

     form.submit(); 

     `) 

    }) 
    }) 
} 

app.on('ready', createWindow); 

app.on('window-all-closed',() => { 
    if (process.platform !== 'darwin') { 
    app.quit(); 
    } 
}); 

回答

1

這是因爲您正試圖在dom-ready事件之前運行Javascript。

試試這個事件後,在執行JavaScript是完成像下面

const { app, BrowserWindow } = require('electron'); 

let win; 

function createWindow() { 

    win = new BrowserWindow({ width: 1000, height: 600 }) 
    win.openDevTools(); 

    // First URL 
    win.loadURL('https://www.google.com') 

    // Once dom-ready 
    win.webContents.once('dom-ready',() => { 

     // THIS WORKS!!! 
     win.webContents.executeJavaScript(` 
     console.log("This loads no problem!"); 
    `) 

     // Second URL 
     win.loadURL('https://github.com/electron/electron'); 

     // Once did-navigate seems to function fine 
     win.webContents.once('did-navigate',() => { 

      // THIS WORKS!!! So did-navigate is working! 
      console.log("Main view logs this no problem...."); 
      win.webContents.once('dom-ready',() => { 
       // NOT WORKING!!! Why? 
       win.webContents.executeJavaScript(` 

     console.log("I canot see this nor the affects of the code below..."); 

     const form = document.querySelectorAll('input.js-site-search-form')[0]; 

     const input = form.querySelectorAll('input.header-search-input')[0] 

     input.value = 'docs'; 

     form.submit(); 

     `) 

      }) 
     }); 
    }) 
    } 

    app.on('ready', createWindow); 

    app.on('window-all-closed',() => { 
     if (process.platform !== 'darwin') { 
      app.quit(); 
     } 
    }); 
+0

是的,這似乎解決它。我可以發誓,我也嘗試過'dom-ready'作爲第二次審判。我會檢查我的申請並回復。我希望這不是從一天編碼太多的另一個尷尬:) –

+0

AAAAAnnnd我是一個傻瓜。我爲第二個URL更改了'did-navigate'和'dom-ready',但從未嘗試過'did-navigate',接着是另一個'dom-ready'。謝謝你的快速反應。 –

+0

它發生.. :) –