微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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;
  });
});

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

我知道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!');");
  });

  ...

});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐