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

我正在尝试使用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!");

});

回答:

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

...

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

...

mainWindow.webContents.on('did-finish-load', function() {

mainWindow.webContents.executeJavaScript("alert('Hello There!');");

});

...

});

以上是 如何在Electron呈现的网页上调用JavaScript函数? 的全部内容, 来源链接: utcz.com/qa/434387.html

回到顶部