javascript - Electron 弹出窗口(未定义$);控制台日志中没有错误

标签 javascript html jquery electron

简而言之:我正在使用Electron实现一个单页网站,并且遇到了jQuery不能在全局范围内访问的常见问题。因此,为了解决这个问题,我在下面使用了快速入门示例来简化了该问题,但我无法理解。

如您所见,我只是将以下代码添加到了main.js文件的末尾,以检查问题是否得到解决:

$(document).ready(function () {
    console.log("hi");
});

这是我的main.js文件的完整代码:

// main.js
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require("electron");
const path = require("path");

function createWindow() {
    // Create the browser window.
    const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        preload: path.join(__dirname, "preload.js"),
        nodeIntegration: true,

        webSecurity: true,
    },
    });

    // and load the index.html of the app.
    mainWindow.loadFile("index.html");

    // Open the DevTools.
    // mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
    createWindow();

    app.on("activate", function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
    });
});

// Quit when all windows are closed.
app.on("window-all-closed", function () {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== "darwin") app.quit();
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

$(document).ready(function () {
    console.log("hi");
});

我正在将以下HTML代码加载到我的BrowserWindow中:

<!DOCTYPE html>
<html>
    <head>

    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->

    <!--This was added otherwise "window.jQuery = window.$ = require('jquery');" will not be executed-->
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' http://* 'unsafe-inline'; script-src 'self' http://* 'unsafe-inline' 'unsafe-eval'" />

    <title>Hello World!</title>
    </head>
    <body>
    <h1>Hello World!</h1>
    We are using Node.js <span id="node-version"></span>,
    Chromium <span id="chrome-version"></span>,
    and Electron <span id="electron-version"></span>.

    <script src="node_modules/jquery/dist/jquery.min.js"></script>
    <script>window.jQuery = window.$ = require('jquery');</script>
    <script src="./renderer.js"></script>

    </body>
</html>

根据Electron文档的建议,以下脚本作为BrowserWindow脚本附加到preload:

// Preload.js
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener("DOMContentLoaded", () => {
    const replaceText = (selector, text) => {
    const element = document.getElementById(selector);
    if (element) element.innerText = text;
    };

    for (const type of ["chrome", "node", "electron"]) {
    replaceText(`${type}-version`, process.versions[type]);
    }
});

最后但并非最不重要的是,这是快速启动项目的package.json配置文件:
{
    "name": "electron-quick-start",
    "version": "1.0.0",
    "description": "A minimal Electron application",
    "main": "main.js",
    "scripts": {
        "start": "electron ."
    },
    "repository": "https://github.com/electron/electron-quick-start",
    "keywords": [
        "Electron",
        "quick",
        "start",
        "tutorial",
        "demo"
    ],
    "author": "GitHub",
    "license": "CC0-1.0",
    "devDependencies": {
        "electron": "^9.0.2"
    },
    "dependencies": {
        "jquery": "^3.5.1"
    }
}

但是,每次我运行该应用程序时,都会出现以下错误提示:

我的应用程序的Powershell输出中也显示了此错误(我只编辑了完整的目录路径):

App threw an error during load
ReferenceError: $ is not defined
  at Object.<anonymous> (...directory\0masterproject\main.js:48:1)
  at Module._compile (internal/modules/cjs/loader.js:967:30)
  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1004:10)
  at Module.load (internal/modules/cjs/loader.js:815:32)
  at Module._load (internal/modules/cjs/loader.js:727:14)
  at Function.Module._load (electron/js2c/asar.js:738:28)
  at loadApplicationPackage (...directory\0masterproject\node_modules\electron\dist\resources\default_app.asar\main.js:109:16)    at Object.<anonymous> (...directory\0masterproject\node_modules\electron\dist\resources\default_app.asar\main.js:155:9)
  at Module._compile (internal/modules/cjs/loader.js:967:30)
  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1004:10)
PS ...directory\0masterproject> npm start


但是,当我关闭此窗口并检查BrowserWindow的控制台时,我没有看到任何错误,但是也没有“hi”输出。

在遇到此问题之前,我曾遇到未定义require ()的问题,已通过添加解决了该问题

<script src="node_modules/jquery/dist/jquery.min.js"></script>

在我的jQuery代码之前:

<script>window.jQuery = window.$ = require('jquery');</script>

我在this article中找到了这个解决方案;我已经尝试过this example in Electron's documentation,但无济于事。

请让我知道缺少任何信息。

最佳答案

您混淆了main and renderer processes。使用Electron,负责您的UI的所有代码(即,附加到BrowserWindow中加载的HTML的JavaScript)都在渲染器进程中运行,而所有其他代码(例如,负责打开窗口或设置app)都可以运行在主要过程中运行。

在这个主要过程中,您不能使用特定于DOM的全局变量,因为主要过程是纯粹的Node.js。您想使用jQuery做什么,即

$(document).ready(function () {
  console.log("hi");
});

尽管如此,仍然无法正常工作,因为在主进程中未定义document。另外,主进程的环境与渲染器进程是分开的(这就是为什么必须使用IPC在它们之间进行通信的原因),因此$中未定义main.js,因为您是在渲染器HTML中定义的。

如果您将以下内容放在HTML文件的末尾(甚至将renderer.js包括在HTML的末尾)放在文件的末尾,则可能会得到想要的结果:

<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script>
    window.jQuery = window.$ = require('jquery');
    const $ = window.jQuery;
    $(document).ready(function () {
        console.log("hi");
    });
</script>
<script src="./renderer.js"></script>

毕竟,我建议您通过阅读Electron的文档(例如this article)来熟悉Electron的体系结构,因为当不熟悉main/renderer设计原理时,没有太多会出错的问题,只需简单地解决即可了解此设计。

关于javascript - Electron 弹出窗口(未定义$);控制台日志中没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62204274/

相关文章:

javascript - 具有 n 级的 Highcharts 堆叠钻取

javascript - 当复选框= true时

javascript - html/javascript Canvas 高度/宽度坐标不一样?

javascript - 如何在 Angular 6 中添加计数器?

html - 如何使用 em 而不是 px 更改 <h1> 到 <h6> 以具有与 <p> 相同的填充,同时保持它们完全相同?

javascript - 向后滚动时 Jquery 不工作

jquery - 如何向莫里斯条形图添加动态数据

php - 更改图像颜色

html - 如何去除表格边框的阴影?

jquery - 循环使用按数字顺序排列的 id 名称的 div?