Electron webContents executeJavaScript:无法在第二个 loadURL 上执行脚本

Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:无法在第二个 loadURL 上执行脚本)
本文介绍了Electron webContents executeJavaScript:无法在第二个 loadURL 上执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在测试 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 上执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)