問題描述
我正在測試 Electron 并專門使用 executeJavaScript.我的項目使用 POST 請求登錄網站,然后做一些工作并使用同一會話加載第二個 URL.在第二個 URL 中,我需要執行 JS,但我不確定我做錯了什么.
I am testing out Electron and specifically working with executeJavaScript. My project uses a POST request to sign into a website, then does a bit of work and loads a second URL using the same session. In this second URL, I need to execute JS but I am not sure what I am doing wrong.
在此示例中,我創建了一個簡化版本,模擬訪問兩個 URL 并在第二個 URL 上執行 JS.關于這里發生了什么的任何想法?
In this example I created a dumbed down version that simulates going to two URL's and executing JS on the second. Any ideas on what is going on here?
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();
}
});
推薦答案
這是因為你試圖在 dom-ready 事件之前運行 Javascript.
It is because you are trying to run the Javascript before the dom-ready event.
在此事件完成后嘗試執行您的 javascript,如下所示
Try executing your javascript after this event is completed like below
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();
}
});
這篇關于Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!