Electron ã¢ããªãã¤ããæã«ä¾¿å©ãªããã±ã¼ã¸
ãã®è¨äºã¯ Electron ã¢ããã³ãã«ã¬ã³ãã¼2016 ã®13æ¥ç®ã®è¨äºã§ãï¼
æ¬è¨äºã§ã¯ï¼åã Electron ã¢ããªãã¤ããä¸ã§ä¾¿å©ã ã£ããï¼ã»ããã£ãã®ã§ã¤ãã£ããããããã±ã¼ã¸ã7ã¤ã»ã©ç´¹ä»ãã¾ãï¼
- electron-about-window
- electron-dl
- electron-in-page-search
- electron-window-state
- menubar
- node-auto-launch
- electron-mocha
electron-about-window
electron-about-window 㯠'ãã®ã¢ããªã«ã¤ãã¦' ã¦ã£ã³ãã¦ãç°¡åã«ã¯ãã¹ãã©ãããã©ã¼ã ã«ã¤ããããã®ããã±ã¼ã¸ã§ãï¼ä¸è¨ã®ããã«é¢æ°ã1ã¤ã¤ã³ãã¼ããã¦å¼ã³åºãã ãã§ããã®ã¢ããªã«ã¤ãã¦ãã¦ã£ã³ãã¦ãçæãããã¨ãã§ãã¾ãï¼ï¼exampleï¼
import openAboutWindow from 'about-window'; openAboutWindow({ icon_path: 'path/to/icon.png' });
ã¢ã¤ã³ã³ãã¡ã¤ã«ã¸ã®ãã¹ã¯ã©ããã¦ãå¿ è¦ã«ãªã£ã¦ãã¾ãããæã§æå®ããå¿ è¦ãããã¾ããï¼ãã®ä»ã®ãã¼ã¸ã§ã³ããã¹ã¯ãªãã·ã§ã³ã¯ package.json ããå¼ã£å¼µã£ã¦ãã¦ãããã®ã§ç¹ã«æå®ããå¿ è¦ã¯ããã¾ããï¼
ä¾ãã° Shiba ã ã¨ä¸è¨ã®ãããªã¦ã£ã³ãã¦ãçæããã¾ãï¼
ã¦ã£ã³ãã¦ã®ã©ã¤ããµã¤ã¯ã«ãªã©ã¯ãã¹ã¦ã©ã¤ãã©ãªå´ã§ãã³ãã«ãããã®ã§ï¼ç¹ã«ã©ã¤ãã©ãªã¦ã¼ã¶å´ãæ°ã«ããå¿ è¦ã¯ããã¾ããï¼
TypeScript ã対å¿æ¸ã¿ã§ãï¼
electron-dl
Electron ã¢ããªä¸ã§ä½ãã®ãã¡ã¤ã«ããã¼ã«ã«ã«ãã¦ã³ãã¼ããããæï¼ããæ¹ã¯2éãããã¾ãï¼
- Chrome ã®ãã¤ãã£ããªãã¦ã³ãã¼ãæ©è½ã Electron ãæä¾ãã API ã§å©ç¨ãã
- ãã¤ã¢ãã°ã®ã¿ãã¤ãã£ã㪠API (
openSaveDialog
) ã使ã£ã¦ï¼ãã¦ã³ãã¼ããä¿å㯠Node.js ã® API ã§é å¼µã
ã§ããã° 1. ã§ããããã¨ããã§ããï¼Electron ã®ãã¦ã³ãã¼ãé¢é£ã® API ã¯è²ã
ãªã¨ããã« API ãåæ£ãã¦ãã¦ä½¿ãã«ããã§ãï¼ä¾ãã°ãã¦ã³ãã¼ããéå§ãã downloadURL
㯠WebContents
ã«ï¼ãã¦ã³ãã¼ãã®åé㯠Session
ã«ï¼ãã¦ã³ãã¼ãå¾ã® Dock ã®æåï¼macOSï¼ã¯ app.dock
ã«ãã£ãããã¾ãï¼ã¾ãï¼Chrome ã®ãã¦ã³ãã¼ãã¯ãã¤ã¢ãã°ãåºããã©ããã®å¶å¾¡ãä¸æä¸æã»åéï¼ãªã©ä¸éãã®ãã¨ãã§ããã®ã§è¤éã§ãï¼ãã㯠Electron ã Chromium ã® C++ API ãå²ã¨ãã®ã¾ã¾ JavaScript ã® API ã¨ãã¦è¦ãã¦ããããã§ãï¼
electron-dl ã¯ãããã®ä½¿ãã¥ãã API ãè£ã«é ãï¼ç°¡åã«ãã¡ã¤ã«ã®ãã¦ã³ãã¼ããè¡ããããã«ãã¦ããã¾ãï¼
import {download} from 'electron-dl'; const win = new BrowserWindow(); const opts = { // ä¿åå ãã£ã¬ã¯ããªï¼ãã¤ã¢ãã°ãéãå ´åã¯ãã®ãã£ã¬ã¯ããªãåæå¤ã«ãªã directory: '/path/to/some/direcotry', // 'save as' ãã¤ã¢ãã°ãåºããã©ããï¼åºããªãå ´åã¯ä¸è¨ã® 'directory' ãããã㣠// ã§æå®ããï¼ä½ãæå®ããªããã°ããã©ã«ãã®ãã¦ã³ãã¼ããã£ã¬ã¯ããªï¼~/Downloads ãªã©ï¼ // ã使ãããï¼ saveAs: true }; // https://example.com/some/file ããã¦ã³ãã¼ãããï¼opts ã¯çç¥å¯ï¼ download(win, 'https://example.com/some/file', opts) .then(dl => { console.log('Download successfully completed', dl.getSavePath()); }) .catch(err => { console.error('Download failed', err.message); });
ããã ãã§ãã¦ã³ãã¼ããä¸æãããå ´åã®é¢åãªã©ãå«ãããã¡ã¤ã«ã®ãã¦ã³ãã¼ãããã¹ã¦è¡ã£ã¦ããã¾ãï¼ã¾ãï¼macOS ã®ãã¦ã³ãã¼ãä¸ã®æåï¼Dock ã¢ã¤ã³ã³ã¸ã®ããã°ã¬ã¹ãã¼ã®è¡¨ç¤ºãå®äºæã® Dock ã¢ã¤ã³ã³ã®ãã¦ã³ã¹ï¼ããã£ã¦ããã¾ãï¼ãªããã¦ã³ãã¼ãå®äºå¾ã« return
ãããå¤ï¼ä¸è¨ã³ã¼ãã® dl
ï¼ã¯ DownloadItem
ã¤ã³ã¹ã¿ã³ã¹ ã§ãï¼
electron-in-page-search
Electron ã«ã¯ Chrome ã®ãã¼ã¸å
æ¤ç´¢ãå®è¡ãã API ãããï¼WebContents
ã¤ã³ã¹ã¿ã³ã¹ã® .findInPage()
ã¡ã½ããã found-in-page
ã¤ãã³ããçµã¿åããã¦ä½¿ãã¾ãï¼ã§ããï¼æ¤ç´¢çªãå¥ã®ãã¤ãã£ããªã¦ã£ã³ãã¦ã§å®è£
ããã¦ãããã¨åæã«ãªã£ã¦ãããï¼BrowserWindow
㨠<webview>
ã§æåãéã£ããã¨è²ã
è½ã¨ãç©´ãããã¾ãï¼ããã¯ãã¦ã³ãã¼ãå¨ãã¨åæ§ã« Electron ã Chromium ã® C++ API ãå²ã¨ãã®ã¾ã¾ JavaScript ã® API ã¨ãã¦è¦ãã¦ããããã§ãï¼ãã®è¾ºã¯ä»¥åã¡ã¢ããããã¾ããï¼
Electron 㧠Chrome ã®ãã¼ã¸å æ¤ç´¢æ©è½ã使ã
ããã§ï¼ãããã®è½ã¨ãç©´ãæ¤ç´¢ã®ç¶æ ãæ°ã«ãããã¼ã¸å æ¤ç´¢ãã¢ããªã«çµã¿è¾¼ãããã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦ã¤ããããã®ã electron-in-page-search ã§ãï¼
ãã㯠example ã®ã¹ã¯ãªã¼ã³ã·ã§ããã§ãï¼ãã¿ã³ãæ¼ãã¨æ¤ç´¢çªãç¾ãï¼ããã«æ¤ç´¢ã¯ã¼ããå ¥ã㦠Enter ãã¼ã 次/å æ¤ç´¢ãã¿ã³ã§æ¤ç´¢ã§ãã¾ãï¼æ¤ç´¢çªã®ã¹ã¿ã¤ã«ã¯ CSS ãªã©ã§ç´°ããå¶å¾¡ã§ãã¾ãï¼
import searchInPage from 'electron-in-page-search'; import {remote} from 'electron'; const inPageSearch = searchInPage(remote.getCurrentWebContents()); document.getElementById('some-button').addEventListener('click', () => { inPageSearch.openSearchWindow(); });
åæ¥ã® joe-re ããã®è¨äºã«ããã£ãããã«ï¼Electron ã®ãã¤ãã£ã㪠API ã®ãã¹ãã¯è²ã é¢åã§ããï¼electron-in-page-search ã¯ãã§ã«åãã©ãããã©ã¼ã ï¼OS X, Linux, Windowsï¼ã§ãã¹ãæ¸ã¿ãªã®ã§å®å¿ã§ãã¾ãï¼
TypeScript ã対å¿æ¸ã¿ã§ãï¼
electron-window-state
ã¢ããªãã¦ã£ã³ãã¦ãçæããæã«ï¼æ¯ååããµã¤ãºã»ä½ç½®ã§çæããã®ã§ã¯ãªã以åã¦ã¼ã¶ãä½æããã¦ã£ã³ãã¦ã®ä½ç½®ãè¦ãã¦ããã¦ã»ããäºãããã¾ãï¼ãã©ã¦ã¶ã¦ã£ã³ãã¦ã®ãµã¤ãºã¯ã¬ã³ãã©ããã»ã¹ä½æåã«ç¥ãå¿ è¦ãããã®ã§ï¼ã¢ããªãã£ã¬ã¯ããªã« JSON ãã¡ã¤ã«ãªã©ã§ä¿åãã¦ããå¿ è¦ãããã¾ãï¼
Electron ã¢ããªã®ã¦ã£ã³ãã¦ãµã¤ãºï¼ãã¸ã·ã§ã³ã復å ãã
electron-window-state ã¯ãã®è¾ºãããã£ã¦ãããããã±ã¼ã¸ã§ãï¼
const windowStateKeeper = require('electron-window-state'); let win; app.on('ready', () => { // Load the previous state with fallback to defaults const state = windowStateKeeper({ defaultWidth: 1000, defaultHeight: 800 }); win = new BrowserWindow({ x: state.x, y: state.y, width: state.width, height: state.height }); state.manage(win); });
ã¦ã£ã³ãã¦ã®ç¶æ ãä¿åã»å¾©å¸°ãããï¼åæå¤ããã³ãã«ãã¦ãããã®ã§ï¼æ°ã«ããå¿ è¦ããªããªãã¾ãï¼
menubar
menubar ã¯ã¡ãã¥ã¼ãã¼ã«å¸¸é§ãã¦ï¼ã¡ãã¥ã¼ã¢ã¤ãã ãã¯ãªãã¯ãããæã«ããã¦ã£ã³ãã¦ã表示ãããããªã¢ããªãã¤ããããã® BrowserWindow
ã® wrapper ã§ãï¼
ã¡ãã¥ã¼ã¢ã¤ãã ã®è¡¨ç¤ºãã¦ã£ã³ãã¦ä½ç½®ã®å¶å¾¡ï¼è¡¨ç¤ºã®ãã°ã«ãªã©ã管çãã¦ãããã®ã§ï¼åæ°è¡ã§ã¡ãã¥ã¼ã¦ã£ã³ãã¦ãæ±ãã¢ããªãä½ãã¾ãï¼
import * as menubar from 'menubar'; const mb = menubar({ index: '/path/to/index.html', icon: '/path/to/icon.png' }); mb.on('ready', () => { // app.on('ready') ç¸å½ï¼ã¢ããªãç«ã¡ä¸ãã£ãå¾ã®å¦ç mb.showWindow(); }); mb.on('after-create-window', () => { // ã¦ã£ã³ãã¦ãä½æãããå¾ã®å¦ç });
OS X, Linux, Windows ã«å¯¾å¿ãã¦ãã¾ããï¼Linux 㯠Ubuntu ãªã©ã®ã¡ã¸ã£ã¼ãªãã£ã¹ããªãã¥ã¼ã·ã§ã³åãã§ãï¼ã¾ãï¼Windows ã§ã¿ã¹ã¯ãã¼ã®ä½ç½®ãå·¦ã«ãããªã©ã®è¨å®ããã¦ããå ´åã¯å¯¾å¿ãã¦ããªãããã§ãï¼
node-auto-launch
ä¸è¨ã®ã¡ãã¥ã¼ãã¼ã«å¸¸é§ããã¢ããªãªã©ãå®è£ ããã¨ï¼OS èµ·åæã«ã¢ããªãä¸ç·ã«èµ·åãã¦ã»ããã¨æãã¾ãï¼èµ·åæã«èªåã§ã¢ããªãã¹ã¿ã¼ãããã«ã¯ããããã® OS ãã¨ã«è¨å®ããï¼OS X ãªã Launch Agent, Windows ãªãã¬ã¸ã¹ããªç»é²ãªã©ï¼å¿ è¦ãããã¾ããï¼node-auto-launch ã§ã¯ãããã®å¦çã wrap ãã¦ï¼ã¯ãã¹ãã©ãããã©ã¼ã ã« OS èµ·åæã®ã¢ããªèªåå®è¡ãè¡ã£ã¦ããã¾ãï¼
const AutoLaunch = require('auto-launch'); const launcher = new AutoLaunch({ name: 'Your App Name', path: '/path/to/YourApp', }); launcher.isEnabled().then(enabled => { if (enabled) { // ãã§ã«èªåèµ·åã¢ããªã¨ãã¦ç»é²æ¸ã¿ return; } launcher.enable(); });
GitHub ã®éç¥ãæµãã¦ãããã¡ãã¥ã¼ãã¼ã¢ã㪠gitify ãªã©ãå©ç¨ãã¦ãã¾ãï¼
electron-mocha
Electron ã¢ããªã®åä½ãã¹ããæ¸ãæï¼ãã¹ã対象ã Electron ã®ã¢ã¸ã¥ã¼ã«ã«ä¾åãã¦ãã㨠Node.js ã§ã¯åä½ãã¹ããå®è¡ã§ãã¾ããï¼
ããã§ï¼Electron ä¸ã§ mocha ã§åä½ãã¹ããå®è¡ã§ããã®ã electron-mocha ã§ãï¼ã¡ã¤ã³ããã»ã¹å´ã§ãã¬ã³ãã©ããã»ã¹å´ã§ããã¹ããå®è¡ã§ãã¾ãï¼
Electron ã¢ããªã ãã§ãªãï¼DOM API ã«ä¾åããåä½ãã¹ããªãããã¬ã³ãã©ããã»ã¹å´ã§ã¯å®è¡ã§ããã®ã§ï¼DOM API ã«ä¾åãããã¹ããè¡ãããæä¸è¬ã«ã使ãã¾ãï¼
describe('', function () { before(function () { this.elem = document.querySelector('.target'); }); it('', function () { this.elem.click(); assert.equal(this.elem.innerText, 'Clicked'); }); });
ã®ãããªãã¹ããç¹ã« jsdom ãªã©ã使ããã«æ®éã«åãã¾ãï¼
ã¾ã¨ã
Electron 1.0 ãåºã¦ããã ãã¶æéãçµã¡ï¼ãããªãã«ã¨ã³ã·ã¹ãã ãè²ã£ã¦ãã¾ããï¼ä»åã¯ç§ãå®éã«å°å ¥ãã¦ã¿ã¦ä¾¿å©ã ã£ããã®ãç´¹ä»ãã¾ãããï¼awesome-electron ã«ã¯ããã«ããããã®ãã¼ã«ãã©ã¤ãã©ãªãæ²è¼ããã¦ãã¾ãï¼