executeJavaScript没有给出错误,但不会执行代码 [英] executeJavaScript gives no errors but won't execute code

查看:588
本文介绍了executeJavaScript没有给出错误,但不会执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,据我所知,这应该执行渲染器进程内 executeJavaScript()中指定的代码。当我仅包含要执行的 console.log()时,一切运行正常,并且在开发人员控制台中看到了输出。然后我的问题是,什么原因可能导致此操作无法执行?我曾尝试在有条件的情况外添加 console.log(),但在开发人员控制台中仍然没有任何显示。更重要的是,这取决于我在何处插入 console.log()错误,这表明我有一个未知的标识符。

Okay, as I understand things this should execute the code specified inside executeJavaScript() inside my renderer process. When I include only a console.log() to be executed, things work flawlessly and I see the output inside the developer console. My question then is, what might be causing this to not execute? I've tried adding a console.log() outside of my conditional and still nothing nothing appears inside developer console. What's more depending on where I insert the console.log() errors are thrown telling me there's an unknown identifier.

在不发布我项目的其余部分的情况下,我对此功能的理解是否明显有错误或不完整?

Without posting the rest of my project, is there anything obviously wrong or broken about my understanding of this function? This seems like it should be pretty straight forward.

const electron = require('electron')
const remote = require('electron').remote
const ipc = require('electron').ipcRenderer
const url = require('url')
const path = require('path')
const app = electron.app
const BrowserWindow = electron.BrowserWindow



var MainWindow;
app.on('ready', function()
{
    MainWindow = new BrowserWindow({
        width: 1024, 
        height: 768, 
        backgroundColor : '123355',
        frame: false,
        resizable: true,
        movable: true,
        show: false
    })
    var win = MainWindow.webContents


    MainWindow.on('ready-to-show', () => {
        MainWindow.show()
        console.log('Ready to go!')
    })

    MainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true

    }))


        win.executeJavaScript("document.onreadystatechange = function ()" +
        "{" +
                "document.getElementById('minimize-app').addEventListener('click', function (e) {" +
                    "var window = BrowserWindow.getFocusedWindow();" +
                    "window.minimize();" +
                "});" +
                "document.getElementById('maximize-app').addEventListener('click', function (e) {" +
                    "var window = BrowserWindow.getFocusedWindow();" +
                    "window.maximize();" +
                "});" +
                "document.getElementById('close-app').addEventListener('click', function (e) {" +
                    "var window = BrowserWindow.getFocusedWindow();" +
                    "window.close();" +
                "});" +
        "}"
        );


});


推荐答案

您应该在DOM准备就绪时调用它(即 'dom-ready'

You should call this when DOM is ready (that is 'dom-ready')

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

let mainWindow = null
app.once('ready', () => {
  mainWindow = new BrowserWindow({})
  const executeOnHTML = () => {
    mainWindow.webContents.executeJavaScript(`
      document.getElementById('minimize-button').addEventListener('click', function (e) {
        const { remote } = require('electron')
        var window = remote.BrowserWindow.getFocusedWindow()
        window.minimize()
      })
    `)
  }
  mainWindow.webContents.once('dom-ready', executeOnHTML)
  mainWindow.loadURL(path.join(__dirname, 'index.html'))
})






此外,还有很多不同的方法可以执行相同操作,而不是将所有内容都放入一个文件中。


Also, there are a lot of different ways to do the same instead of putting everything in one file.


  • 您可以定义HTML从HTML(< script src = ...> )引用的js中的行为

  • 您可以使用IPC在Main and Renderer process

  • You can define HTML behavior in a js referred from HTML (<script src=...>)
  • You can use IPC to communicate between Main and Renderer process

我还建议您阅读有关电子过程

这篇关于executeJavaScript没有给出错误,但不会执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆