2017-04-22 3 views
0

私はElectronをテストしており、特にexecuteJavaScriptを使用しています。私のプロジェクトはPOSTリクエストを使ってWebサイトにサインインし、その後少しの作業をして、同じセッションを使って2番目のURLを読み込みます。この2番目のURLでは、私はJSを実行する必要がありますが、私は何が間違っているのか分かりません。電子webContents executeJavaScript:loadURLの2番目のスクリプトを実行できません

この例では、2つのURLに移動し、2つ目の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を実行しようとしているからです。

は、このイベントは、はい、これはそれを修正するように見えるん

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

以下のように完了した後に、あなたのJavaScriptを実行してみます。私は誓ったことができました。私は2回目の試行でも 'ドム・レディー'を試みました。私は申請書に確認し、返答します。私はこれが一日であまりにも多くのコーディングから別の恥ずかしさではないことを願っています:) –

+0

AAAAAnnnd私はばかだった。私は2番目のURLに対して 'did-navigate'と' dom-ready'を変更しましたが、 'did-navigate'の後に別の' dom-ready'を試したことはありませんでした。速やかなご返信ありがとうございます。 –

+0

それは起こる.. :) –

関連する問題