node-chromeãä¸æ¥ããããåãã¦ããã£ããã¨
ä»ææ¹
http://www.moongift.jp/2013/02/20130216-2/
ã¨ããè¨äºãè¦ã¤ããã
ç°¡åã«ããã°node.jsã®UIé¨åãchromeã«ä»»ãããããªãã®ãä½ã£ã¡ãããï¼ã£ã¦ä»£ç©
éå»ã«ãããããã¨ããããã¨ãã¦chrome-extensionãä½ã£ã¦æ«æãããlocalhostã®æ¹ããã¯ããã¦ããè²ã ãã£ã¦ãèªåã«ã¯æå ±ã ã£ãã®ã§æ©énode-chromeã触ã£ã¦ã¿ãã
ã¾ãã¯æ®éã«ã¤ã³ã¹ãã¼ã«
$mkdir node-chrome-sample $cd node-chrome-sample $npm init $npm install node-chrome
windowsã ã¨ã¤ã³ã¹ãã¼ã«æã«ã¨ã©ã¼ãåºããããã«ã¤ãã¦ã¯å¾è¿°ããããç°¡åã«è¨ãã¨ã¨ã©ã¼åºã¦ãåé¡ãªãåãã£ã½ãã®ã§ãã®å ´ã§ã¯ã²ã¨ã¾ãæ°ã«ããé²ããã
ã½ã¼ã¹ãè¦ã¦ã¿ã
https://github.com/hij1nx/node-chrome
ã¡ã¤ã³ã¨ãªãã®ã¯lib/index.jsã ãã§ã¨ã¦ãç°¡ç´ ã
èã«ãªã£ã¦ããã®ã¯
var args = [ '--app=http://localhost:' + (opts.port || 8080) + opts.index, '--force-app-mode', '--app-window-size=' + (opts.width || 1024) + ',' + (opts.height || 760), '--enable-crxless-web-apps', '--user-data-dir=' + __dirname ];
ãã®ãããã
ãªãã»ã©ãappã¢ã¼ãã¨ãã¦èµ·åããã®ãããã®å¾ã®ã»ãã¯ãµã¼ãã¼èµ·åããã¦ãã¿ããã
user-data-dirãå¥ã«ãªã£ã¦ããã®ã§chromeé常起åæã¨ã¯ç¬ç«ããç¶æ
ã«ãªã£ã¦ããã
ãã¢ãåãã
node-chrome/exampleã«ãµã³ãã«ãããã®ã§ã¨ãããããµã³ãã«ãèµ·å
$cd node_modules/node-chrome/example $node demo
ãï¼åããã
ã¨ã½ã¼ã¹ãè¦ãã¨
: var opts = { runtime: "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome", ãã :
ãã¼ã»ã»ã»ã¯ããããã¾ãããmacãªãã¦é«è²´ãªãã®æã£ã¦ãªããã§ããã§ãè¦åãããåºãã¦ããã¦ãã»ã»ã»
ãªã®ã§ä¸è¨ã®ããã«æ¸ãæã
: var opts = { runtime: process.env.LOCALAPPDATA+"\\Google\\Chrome\\Application\\chrome.exe", } ãã:
å度起åã
åãããããåé¡ãªãã
runtimeã決ãã¦ãããªãã®ã¯ãªããªãçããããã±ã¼ã¸ã§é
å¸ããã¨ãã«å°ãããã
ã¨ããããèªåã®ä½ããããã¦ã¿ãã
demoãåé¡ãªãã£ãã®ã§ã¡ãã£ã¨ç°¡æã«ä½ã£ã¦ã¿ã
index.js
var opts = { runtime: process.env.LOCALAPPDATA+"\\Google\\Chrome\\Application\\chrome.exe", files: "./view", port: port, index: "/index.html", }; var fs = require("fs") var client = require(".server.js"); cn(opts, function(websocket, chrome) { console.log("foo") chrome.on('exit', function (code) { process.exit(0); }); });
view/index.html
<html> <body> foo </body> </html>
ã§ãèµ·å
$node index.js
ã»ã»ã»ã»ããï¼console.log(foo)ã¨ãã¦ããã®ã§ã³ã³ã½ã¼ã«ä¸ã«ãªã«ãããåºåããããããªã®ã«é»ããããããããªãã§ã»ã»ã»ï¼
ã³ã¼ãããã£ããè¦ã¦ã¿ã
var wss = new WebSocketServer({ server: httpserver }); wss.on('connection', function(ws) { callback.call(this, ws, chrome); });
ããï¼callbackãwebsocketã³ãã¯ã·ã§ã³ç¢ºç«å¾ã«æ¥ã¦ããï¼ãªãã¦ãã£ãï¼*1ã*2
確ãã«demoãwebsocketã³ãã¯ã·ã§ã³ãã£ã¨ãã
ãªã®ã§websocketã¸ã®æ¥ç¶ã追å ã
<html> <script> var ws = new WebSocket("ws://localhost:8080"); </script> <body> foo </body> </html>
ããã§å度起åãã¦ã¿ããåãããããåããã»ã»ã»
è¨ããããã¨ã¯ãããã©ã¾ã¨ãã«ã¦å¾è¿°ã
å°ãã¿ã¨ãã
è²ã ãã£ã¦ã¿ã¦æ°ã¥ãããã¨ãããã®ã§ã¡ã¢ã
ã¦ã£ã³ãã¦ãµã¤ãº
ã¦ã£ã³ãã¦ç»é¢ã®ãµã¤ãºãèµ·åæã«åºå®ããã¦ããã®ã§
ç»é¢ããµã¤ãºãå¤ãã¦éããâåèµ·å ã¨ããã¨å
ã®å¤§ããã«æ»ã£ã¦ãã¾ã
'--app-window-size=' + (opts.width || 1024) + ',' + (opts.height || 760),
ã¨ããé¨åãã³ã¡ã³ãã¢ã¦ããã¦ããã¨ä¿åãããããã«ãªãã®ã§ããã¯ãã²ãªãã·ã§ã³ã欲ããã¨ããã*3
ãã¼ã
ãã£ã¨ã¯ã©ã¤ã¢ã³ããµã¤ãã§è¤æ°ç«ã¡ä¸ããããããã¨ãããã®ã§ãã¼ããåºå®ã ã¨å°ã£ã¡ããã
こういうやり方ã§ç©ºããã¼ãè¦ã¤ãã¦æ±ºãããï¼
ãã©ãã¼ããå¤ãã£ããå¤ãã£ãã§clientãµã¤ãã§websocketã«ã©ããã£ã¦æ¸¡ãã°ããã®ï¼ï¼
ã£ã¦ãªãããããªæã¯
<script> var ws = new WebSocket("ws://"+location.host); </script>
ãããã¦ããã°OKããã¼ãã
ã¡ãªã¿ã«hoståã¯ä»ã®ã¨ããnode-chromeå´ã§localhostã«åºå®ããã¦ããã®ã§å¤ããããªã
ã¾ã¨ã
- æçµçã«èµ·åãããã¨ãã®æãã¯ããããããç´ æµã
- ãã çµæ§ä¸æºãããããã»ã»ã»
- ã¢ã¸ã¥ã¼ã«ã®æ¹éã¨ãã¦chromeããã¾ããã¨èµ·åããã ãã«ãã¦ã¯runtimeå¿
é ã¨ãã§ã¡ãã£ã¨æ°ãå¹ãã¦ãããããµã¼ãã¼å¨ãã«ã¤ãã¦ã¯websocketé¨åã«é¢ãã¦ã¯wså¼·å¶ã§èªç±åº¦å¹ããªãã¦è¾ãæãããã£ã¦ã¡ãã£ã¨ãã®ã¾ã¾ä½¿ãã®ã¯è¾ãã
- ã©ãããã¢ã¸ã¥ã¼ã«ã¨ãã¦æ¯ãããã®ãå³ããããã®ã§ä½ãpull requestæãã¥ããæå¼·ãã
- å人çã«ã¯chromeèµ·åã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦ç¹åãã¦ãããããããããã
- ã©ãããã¢ã¸ã¥ã¼ã«ã¨ãã¦æ¯ãããã®ãå³ããããã®ã§ä½ãpull requestæãã¥ããæå¼·ãã
- runtimeã®æå®ããã£ããééãã¨ç¡è¨ã§é»ãããããã¦æããªãã
- wsãwindowsã§ã¨ã©ã¼åºããããã¾ã使ããããªããã©ãããã¼ã«ã«ãªãã ãsocket.ioã¨ãã§ãè¯ãããªãæã*4
- callbackã¤ãã³ããwebsocketã®connection確ç«å¾ã«çºåãã¦ãã®ã§chromeã®èµ·åã«æåãããã©ãµã¼ãã¼èµ·åã«å¤±æããæã¯chromeçµäºããã¨ããåºæ¥ãªãã®è¾ãã
- ã¢ã¸ã¥ã¼ã«ã®æ¹éã¨ãã¦chromeããã¾ããã¨èµ·åããã ãã«ãã¦ã¯runtimeå¿
é ã¨ãã§ã¡ãã£ã¨æ°ãå¹ãã¦ãããããµã¼ãã¼å¨ãã«ã¤ãã¦ã¯websocketé¨åã«é¢ãã¦ã¯wså¼·å¶ã§èªç±åº¦å¹ããªãã¦è¾ãæãããã£ã¦ã¡ãã£ã¨ãã®ã¾ã¾ä½¿ãã®ã¯è¾ãã
- ã³ã¼ãéã大ãããã¨ãªãã®ã§ä»ã®ã¨ããprocess.spawnãã¦èªåããã¨ããé¸æè¢ãããããã
- å ã«ãè§ã«ãchromeã®èµ·åé¨åã¯è¶ æç¨ãªã®ã§ã¬ã³ã¬ã³ä½¿ã£ã¦è¡ãããã
*1:ããã«æ°ã¥ãã®ã«çµæ§ãªæéãè¦ãã
*2:ã¡ãªã¿ã«ã³ã¼ã«ããã¯ã®ç¬¬ä¸å¼æ°ã¯errorã«ãã¹ãã£ã¦åã人ãè¨ã£ã¦ãæ°ããããã©ã©ããªãã ãã
*3:ã¾ãåºå®ããã»ããè¯ãäºã®ã»ããå¤ããã ãã©
*4:è±èªèªä¿¡ç¡ããã©wsã®ã¨ã©ã¼ã¯å¤åãã®[https://github.com/einaros/ws/issues/155:title=ssue]ã«ã²ã£ããã£ã¦ãã