Electron でレンダリングされた Web ページで JavaScript 関数を呼び出すにはどうすればよいでしょうか? 質問する

Electron でレンダリングされた Web ページで JavaScript 関数を呼び出すにはどうすればよいでしょうか? 質問する

私はラッパーを書こうとしているツイッター使用して電子(旧称 Atom Shell)。

私のmain.jsファイル(「こんにちは世界「たとえば、1か所だけ変更しました)」

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

ベストアンサー1

問題は解決しました。サンプルコードは次のとおりです。

...

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

  ...

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

  ...

});

おすすめ記事