├── example
├── ui
│ ├── styles
│ │ └── main.css
│ ├── index.html
│ └── index.js
└── demo
├── .gitignore
├── package.json
├── README.md
└── lib
└── index.js
/example/ui/styles/main.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0px;
3 | }
--------------------------------------------------------------------------------
/example/ui/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Hello, World
5 |
6 |
7 |
8 | Hello, World
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/example/ui/index.js:
--------------------------------------------------------------------------------
1 |
2 | var ws = new WebSocket("ws://localhost:8080");
3 |
4 | ws.onopen = function() {
5 |
6 | ws.send('test');
7 | };
8 |
9 | ws.onmessage = function (evt) {
10 |
11 | var received_msg = evt.data;
12 | };
13 |
14 | ws.onclose = function() {
15 |
16 | };
17 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | lib-cov
2 | *.seed
3 | *.log
4 | *.csv
5 | *.dat
6 | *.out
7 | *.pid
8 | *.gz
9 |
10 | *.swp
11 | *.swo
12 |
13 | pids
14 | logs
15 | results
16 |
17 | data/main
18 |
19 | .DS_Store
20 | node_modules
21 | npm-debug.log
22 |
23 | lib/chrome*
24 | lib/Default
25 | lib/PepperFlash/
26 | node_modules
27 | lib/Local*
28 | lib/Safe*
29 | lib/SingletonLock
30 | lib/Cert*
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node-chrome",
3 | "version": "0.0.3",
4 | "description": "use chrome to make desktop apps in node.js",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": "",
10 | "author": "",
11 | "dependencies": {
12 | "mime": "1.2.9",
13 | "ws": "0.4.25"
14 | },
15 | "license": "MIT"
16 | }
--------------------------------------------------------------------------------
/example/demo:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | var cn = require('../lib');
4 |
5 | var opts = {
6 | runtime: "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome",
7 | files: "./ui",
8 | port: 8080,
9 | index: "/index.html",
10 | width: 1024,
11 | height: 760
12 | };
13 |
14 | cn(opts, function(websocket, chrome) {
15 |
16 | websocket.on('message', function(message) {
17 | console.log(message);
18 | });
19 |
20 | chrome.stdout.on('data', function (data) {
21 |
22 | });
23 |
24 | chrome.stderr.on('data', function (data) {
25 |
26 | });
27 |
28 | chrome.on('exit', function (code) {
29 | process.exit(0);
30 | });
31 | });
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # NAME
3 | node-chrome(3)
4 |
5 | # SYNOPSIS
6 | Make apps with Node.js and Chrome
7 |
8 | # DESCRIPTION
9 | Use chrome to make desktop apps in node.js
10 |
11 | # EXAMPLES
12 | There are two ways you can do this. Use an existing chrome executable or bundle
13 | a version of it. If you bundle a version of it, you can be sure it works on the
14 | target platform, as well as your own icon.
15 |
16 | ```js
17 | #!/usr/bin/env node
18 |
19 | var cn = require('../lib');
20 |
21 | var opts = {
22 | runtime: "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome",
23 | files: "./ui",
24 | port: 8080,
25 | index: "/index.html",
26 | width: 1024,
27 | height: 760
28 | };
29 |
30 | cn(opts, function(websocket, chrome) {
31 |
32 | websocket.on('message', function(message) {
33 | console.log(message);
34 | });
35 |
36 | chrome.stdout.on('data', function (data) {
37 |
38 | });
39 |
40 | chrome.stderr.on('data', function (data) {
41 |
42 | });
43 |
44 | chrome.on('exit', function (code) {
45 | process.exit(0);
46 | });
47 | });
48 |
49 | ```
50 |
--------------------------------------------------------------------------------
/lib/index.js:
--------------------------------------------------------------------------------
1 |
2 | var spawn = require('child_process').spawn;
3 | var http = require('http');
4 | var url = require('url');
5 | var fs = require('fs');
6 | var path = require('path');
7 | var mime = require('mime');
8 |
9 | module.exports = function(opts, callback) {
10 |
11 | var args = [
12 |
13 | '--app=http://localhost:' + (opts.port || 8080) + opts.index,
14 | '--force-app-mode',
15 | '--app-window-size=' + (opts.width || 1024) + ',' + (opts.height || 760),
16 | '--enable-crxless-web-apps',
17 | '--user-data-dir=' + __dirname
18 | ];
19 |
20 | var chrome;
21 |
22 | var httpserver = http.createServer(function (req, res) {
23 |
24 | var rawurl = url.parse(req.url);
25 | var pathname = decodeURI(rawurl.pathname);
26 | var base = path.join(process.cwd(), opts.files);
27 | var filepath = path.normalize(path.join(base, pathname));
28 |
29 | var p = path.extname(filepath).slice(1);
30 | var mimetype = mime.lookup(p);
31 |
32 | if (!mimetype) {
33 | return;
34 | }
35 |
36 | res.writeHeader('Content-Type', mimetype);
37 |
38 | fs.stat(filepath, function (err, stat) {
39 |
40 | if (err && err.code === 'ENOENT') {
41 | res.writeHead(404, { 'Content-Type': 'plain/text' });
42 | res.end('not found');
43 | }
44 | else {
45 |
46 | if (!stat.isDirectory()) {
47 | res.writeHead(200, { 'Content-Type': mimetype });
48 | fs.createReadStream(filepath).pipe(res);
49 | }
50 | }
51 | });
52 | });
53 |
54 | httpserver.listen(opts.port, function() {
55 |
56 | chrome = spawn(opts.runtime, args);
57 |
58 | var WebSocketServer = require('ws').Server;
59 | var wss = new WebSocketServer({ server: httpserver });
60 |
61 | wss.on('connection', function(ws) {
62 |
63 | callback.call(this, ws, chrome);
64 | });
65 | });
66 | };
67 |
--------------------------------------------------------------------------------