简体   繁体   English

如何使用外部内容将jQuery注入Electron BrowserWindow

[英]How to inject jQuery into Electron BrowserWindow with external content

My main problem is that I can't figure out a way to inject jQuery into a external web loaded in Electron BrowserWindow 我的主要问题是我无法找到将jQuery注入到Electron BrowserWindow中加载的外部Web中的方法

So far I have been able to inject a local script into the page by using "preload" property of WebPreferences in BrowserWindow 到目前为止,我已经能够通过在BrowserWindow中使用WebPreferences的“ preload”属性将本地脚本注入页面中

webPreferences: {
            preload: path.join(__dirname, 'js/myscript.js')
        },

That works so far and having no problems. 到目前为止,该方法没有问题。 However when I try to load jQuery it doesn't work as apparently the "document" isn't ready when that is called. 但是,当我尝试加载jQuery时,它不起作用,因为调用该文件时“文档”显然还没有准备好。

I tried among other supposed solutions (I don't doubt they are, maybe I'm just doing something wrong or thinking they work in a way different to what I need) 我尝试了其他假定的解决方案(我不怀疑它们是,也许我只是做错了什么,或者认为它们以与我需要的方式不同的方式工作)

Example: 例:

window.onload = () => {
    const $ = require('jquery');

    if (typeof jQuery === 'undefined') {
        console.log("No jQuery");

    } else {
        console.log("jQuery");
    }  
}

That on my "myscript.js" file 在我的“ myscript.js”文件中

** Also tried jsdom with no success, again maybe I was doing something wrong, or out of place. **还尝试了jsdom,但没有成功,再次可能是我做错了什么,或者不合适。

What I'm doing wrong here. 我在这里做错了。 I'm lost and dunno what to do. 我迷路了,不知道该怎么办。

我是一个.....没关系,我在网上看到的示例中正在检查jQuery ==='undefined'的问题,我忘记测试$了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM