以下のリポジトリに対して、作業します。
git clone https://github.com/atom/electron-quick-start
index.htmlにJavaScriptのtestFunc関数を定義します。
関数はグローバルに定義されている必要があります。
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<script>
var testFunc = function(){
alert("Hello");
}
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
main.jsの冒頭にconst Menu = electron.Menu;
を追記し、
app.on('ready', function() {
の下に以下の処理を追加。
main.js
var template = [
{
label: 'Test',
submenu: [
{
label: 'TestFunc',
accelerator: 'CmdOrCtrl+S',
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.webContents.executeJavaScript('testFunc()');
}
}
]
}
]
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
実行結果
メニューに「TestFunc」が表示されます。
JavaScriptのtestFunc関数が実行され、"Hello"とalertされます。
参考サイト
http://electron.atom.io/docs/v0.36.0/api/menu/
http://electron.atom.io/docs/v0.27.0/api/browser-window/#webcontents-executejavascript-code