webviewは……簡単にいうとElectronのインラインブラウザーです。
electron.atom.io
実際のところ
前回のスクリプトを基準に
***main.js
'use strict'; const electron = require('electron'); const app = electron.app; // Module to control application life. const BrowserWindow = electron.BrowserWindow; // Module to create native browser window. // Report crashes to our server. electron.crashReporter.start(); // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow; // Quit when all windows are closed. app.on('window-all-closed', function() { // On OS X it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform != 'darwin') { app.quit(); } }); // This method will be called when Electron has finished // initialization and is ready to create browser windows. app.on('ready', function() { // Create the browser window. mainWindow = new BrowserWindow({width: 640, height: 480}); mainWindow.loadURL(`file://${__dirname}/index.html`); // 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; }); });
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <script> onload = function() { var webview = document.getElementById("foo"); var indicator = document.querySelector(".indicator"); var loadstart = function() { indicator.innerText = "loading..."; } var loadstop = function() { indicator.innerText = ""; } webview.addEventListener("did-start-loading", loadstart); webview.addEventListener("did-stop-loading", loadstop); } </script> <webview id="foo" src="https://duckduckgo.com/" style="display:inline-block; width:320px; height:240px">...loading</webview> <form> <input type="button" value="Green" onClick="document.bgColor='green'"> </form> </body> </html>
見え方
duckduckgoを表示しています。*1
試しに検索ワードを突っ込むと、ちゃんと動いてるのが確認できます。
検索結果もこの通り。
ただし、何もしなければ「戻る」ボタンすらないため実装が必要です。
実際にはWEBページなりjsonなりをレンダリングする機能として使うもののようです。
*1:背景が緑色なのはJavascriptの実験の名残ですスミマセン