問題描述
我正在嘗試使用 Twitter 編寫一個包裝器.io/" rel="noreferrer">Electron(以前的 Atom Shell).
I'm trying to write a wrapper for Twitter using Electron (formerly Atom Shell).
我的 main.js
文件(它看起來幾乎與Hello World"的例子,我只是在一個地方改了):
My main.js
file (it looks almost identical to the "Hello World" example, I just changed it in one place):
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()
函數,但它沒有執行.
I try to call alert()
function right after mainWindow.loadUrl()
but it does not execute.
我知道 main.js
文件就像我的應用程序的服務器端,但問題是……如何在頁面上調用 JavaScript 函數?我應該在哪里寫代碼?
I understand that main.js
file is like the server side of my app, but the question is... How can I call a JavaScript function on page? Where should I write the code?
例如,我想執行此操作:
For example, I want to perform this:
$(document).ready(function() {
alert("Hurray!");
});
推薦答案
我已經解決了這個問題.下面是示例代碼:
I have solved the problem. Here's the example code:
...
app.on('ready', function() {
...
mainWindow.webContents.on('did-finish-load', function() {
mainWindow.webContents.executeJavaScript("alert('Hello There!');");
});
...
});
這篇關于如何在 Electron 渲染的網頁上調用 JavaScript 函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!