如何在Electron呈现的网页上调用JavaScript函数?

时间:2015-03-07 22:11:10

标签: javascript node.js electron

我尝试使用Twitter(以前称为Atom Shell)为Electron编写包装器。

我的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!");
});

3 个答案:

答案 0 :(得分:20)

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

...

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

  ...

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

  ...

});

答案 1 :(得分:12)

首先,您应该清楚地看到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 debugger附加到指定的端口并开始调试。这意味着理论上任何Node.js调试器都必须工作。请查看node-inspectorWebStorm debugger。 StackOverflow上有一个关于调试Node.js应用程序的热门问题:How do I debug Node.js applications?

答案 2 :(得分:2)

正确方式是使用contents.send('some_js_Method','parameters')main.js

调用网页中的javascript方法
// 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>