22

我正在尝试使用Electron(以前的 Atom Shell)为Twitter编写一个包装器。

我的main.js文件(它看起来几乎与“ Hello World ”示例相同,我只是在一处进行了更改):

var app = require('app');  // Module to control application life.
var BrowserWindow = require('browser-window');  // Module to create native browser window.

// Report crashes to our server.
require('crash-reporter').start();

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  if (process.platform != 'darwin')
    app.quit();
});

// This method will be called when atom-shell has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {

  // Create the browser window.
  mainWindow = new BrowserWindow ({'width':1000,'height':600});
  // and load the index.html of the app.
  mainWindow.loadUrl('https://twitter.com');

  // Emitted when the window is closed.
  mainWindow.on('closed', function() {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
});

我尝试在alert()之后立即调用函数,mainWindow.loadUrl()但它没有执行。

我知道该main.js文件就像我的应用程序的服务器端,但问题是......如何在页面上调用 JavaScript 函数?我应该在哪里写代码?

例如,我想执行此操作:

$(document).ready(function() {
    alert("Hurray!");
});
4

3 回答 3

31

我已经解决了这个问题。这是示例代码:

...

app.on('ready', function() {

  ...

  mainWindow.webContents.on('did-finish-load', function() {
    mainWindow.webContents.executeJavaScript("alert('Hello There!');");
  });

  ...

});
于 2015-03-08T22:19:23.680 回答
16

首先,您应该清楚地看到 Electron(以前的 Atom Shell)中的进程差异。Electron 将主进程用作一种后端(您可以将其称为“服务器端”)和应用程序的入口点。正如您可能理解的那样,主进程可以产生多个实例BrowserWindow,这些实例实际上是独立的操作系统窗口,每个窗口托管一个 Chromium 渲染的网页,在称为渲染器进程的单独进程中运行。您可以将渲染器进程视为具有潜在扩展功能的简单浏览器窗口,例如访问 Node.js 模块(我写“潜在地”,因为您可以关闭渲染器进程的 Node.js 集成)。

应该提到的是,虽然您有一个带有 GUI 用于渲染器进程的窗口,但您没有用于主进程的窗口。事实上,为应用程序的后端逻辑设置一个并没有多大意义。所以,不能alert()直接在主进程中调用,看到告警窗口。您提出的解决方案确实显示了警报。但重要的是要了解弹出窗口是由渲染器进程而不是主进程本身创建的!主进程只是要求渲染器显示警报(这就是webContents.executeJavaScript函数实际执行的操作)。

其次,据我了解,您在这里通过alert()在主进程中调用函数真正想要实现的是程序执行的跟踪。您可以调用console.log()以将所需的消息输出到控制台。在这种情况下,应用程序本身必须从控制台启动:

/path/to/electron-framework/electron /your/app/folder

现在,更好的是您可以调试主进程。为了做到这一点,必须使用--debug(或--debug-brk)键和分配给它的侦听端口的值来启动应用程序。就像这样:

/path/to/electron-framework/electron --debug=1234 /your/app/folder

您可以使用任何类型的V8 调试器附加到分配的端口并开始调试。这意味着理论上任何 Node.js 调试器都必须工作。看看node-inspector还是WebStorm 调试器。StackOverflow 上有一个关于调试 Node.js 应用程序的热门问题:如何调试 Node.js 应用程序?.

于 2015-04-25T17:04:00.070 回答
4

正确的方法是使用contents.send('some_js_Method','parameters')从网页中调用 javascript 方法main.js

// In the main.js
const {app, BrowserWindow} = require('electron')
let win = null

app.on('ready', () => {
  win = new BrowserWindow({width: 800, height: 600})
  win.loadURL(`file://${__dirname}/index.html`)
  win.webContents.send(some_js_Method', 'window created!') //calling js method (async call)
})


//in index.html
<html>
<body>
  <script>
    require('electron').ipcRenderer.on('some_js_Method', (event, message) => {
      console.log(message)  // Prints 'window created!'
    })
  </script>
</body>
</html>
于 2017-10-28T14:38:19.397 回答