gpt4 book ai didi

javascript - 如何在 Electron 渲染的网页上调用 JavaScript 函数?

转载 作者:IT老高 更新时间:2023-10-28 23:06:51 26 4
gpt4 key购买 nike

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

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

...

});

关于javascript - 如何在 Electron 渲染的网页上调用 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28920621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com