Skip to content
This repository was archived by the owner on Jun 3, 2018. It is now read-only.

Commit 102cd84

Browse files
committed
removing duplicated files
1 parent 74bcd1a commit 102cd84

17 files changed

+438
-941
lines changed

host.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
var spawn = require('child_process').spawn;
4+
5+
var config = {
6+
version: '0.1.0',
7+
isWin: /^win/.test(process.platform)
8+
};
9+
10+
function observe (msg, push, done) {
11+
if (msg.cmd === 'version') {
12+
push({
13+
version: config.version
14+
});
15+
done();
16+
}
17+
else if (msg.cmd === 'echo') {
18+
push(msg);
19+
done();
20+
}
21+
else if (msg.cmd === 'spawn') {
22+
let sp = spawn(msg.command, msg.arguments);
23+
sp.stdout.on('data', stdout => push({stdout}));
24+
sp.stderr.on('data', stderr => push({stderr}));
25+
sp.on('close', (code) => {
26+
push({code});
27+
done();
28+
});
29+
}
30+
else if (msg.cmd === 'exec') {
31+
let sp = spawn(msg.command, msg.arguments);
32+
let stderr = '', stdout = '';
33+
sp.stdout.on('data', data => stdout += data);
34+
sp.stderr.on('data', data => stderr += data);
35+
sp.on('close', (code) => {
36+
push({
37+
code,
38+
stderr,
39+
stdout
40+
});
41+
done();
42+
});
43+
}
44+
else if (msg.cmd === 'env') {
45+
push({
46+
env: process.env
47+
});
48+
done();
49+
}
50+
else {
51+
push({
52+
error: 'cmd is unknown',
53+
cmd: msg.cmd
54+
});
55+
done();
56+
}
57+
}
58+
/* message passing */
59+
var nativeMessage = require('./messaging');
60+
61+
var input = new nativeMessage.Input();
62+
var transform = new nativeMessage.Transform(observe);
63+
var output = new nativeMessage.Output();
64+
65+
process.stdin
66+
.pipe(input)
67+
.pipe(transform)
68+
.pipe(output)
69+
.pipe(process.stdout);

install-linux.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
function exists (directory, callback) {
7+
let root = '/';
8+
let dirs = directory.split('/');
9+
function one () {
10+
root = path.join(root, dirs.shift());
11+
fs.stat(root, (e) => {
12+
if (!e && dirs.length) {
13+
one();
14+
}
15+
else if (e && e.code === 'ENOENT') {
16+
fs.mkdir(root, (e) => {
17+
if (e) {
18+
callback(e);
19+
}
20+
else if (dirs.length) {
21+
one();
22+
}
23+
else {
24+
callback();
25+
}
26+
});
27+
}
28+
else {
29+
callback(e);
30+
}
31+
});
32+
}
33+
one();
34+
}
35+
36+
var dir = path.join('/usr/share', 'com.add0n.node');
37+
var name = 'com.add0n.node';
38+
var ids = {
39+
chrome: [
40+
'lmeddoobegbaiopohmpmmobpnpjifpii', // open in Firefox (Chrome)
41+
'agaecbnjediafcdopcfidcdiponjlmnk', // open in Explorer (Chrome)
42+
],
43+
firefox: [
44+
'{5610edea-88c1-4370-b93d-86aa131971d1}', // open in Explorer
45+
]
46+
};
47+
function manifest (root, type, callback) {
48+
exists(root, (e) => {
49+
if (e) {
50+
throw e;
51+
}
52+
let origins;
53+
if (type === 'chrome') {
54+
origins = '"allowed_origins": ' + JSON.stringify(ids.chrome.map(id => 'chrome-extension://' + id + '/'));
55+
}
56+
else {
57+
origins = '"allowed_extensions": ' + JSON.stringify(ids.firefox);
58+
}
59+
fs.writeFile(path.join(root, name + '.json'), `{
60+
"name": "${name}",
61+
"description": "Node Host for Native Messaging",
62+
"path": "${path.join(dir, 'run.sh')}",
63+
"type": "stdio",
64+
${origins}
65+
}`, (e) => {
66+
if (e) {
67+
throw e;
68+
}
69+
callback();
70+
});
71+
72+
});
73+
}
74+
function application (callback) {
75+
exists(dir, (e) => {
76+
if (e) {
77+
throw e;
78+
}
79+
let isNode = process.argv[2] !== '--add_node';
80+
let run = isNode ? `#!/bin/bash\n${process.argv[2]} host.js` : '#!/bin/bash\n./node host.js';
81+
fs.writeFile(path.join(dir, 'run.sh'), run, (e) => {
82+
if (e) {
83+
throw e;
84+
}
85+
fs.chmodSync(path.join(dir, 'run.sh'), '0755');
86+
if (!isNode) {
87+
fs.createReadStream('../node').pipe(fs.createWriteStream(path.join(dir, 'node')));
88+
fs.chmodSync(path.join(dir, 'node'), '0755');
89+
}
90+
fs.createReadStream('host.js').pipe(fs.createWriteStream(path.join(dir, 'host.js')));
91+
fs.createReadStream('messaging.js').pipe(fs.createWriteStream(path.join(dir, 'messaging.js')));
92+
callback();
93+
});
94+
});
95+
}
96+
function chrome (callback) {
97+
if (ids.chrome.length) {
98+
manifest('/etc/opt/chrome/native-messaging-hosts', 'chrome', callback);
99+
console.error('Chrome Browser is supported');
100+
}
101+
else {
102+
callback();
103+
}
104+
}
105+
function firefox (callback) {
106+
if (ids.firefox.length) {
107+
manifest('/usr/lib/mozilla/native-messaging-hosts', 'firefox', callback);
108+
console.error('Firefox Browser is supported');
109+
}
110+
else {
111+
callback();
112+
}
113+
}
114+
chrome(() => firefox(() => {
115+
application(() => {
116+
console.error('Native Host is installed in', dir);
117+
console.error('>> Application is ready to use <<');
118+
});
119+
}));

install-windows.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
function exists (directory, callback) {
7+
fs.stat(directory, (e) => {
8+
9+
if (e && e.code === 'ENOENT') {
10+
fs.mkdir(directory, callback);
11+
}
12+
else {
13+
callback(e);
14+
}
15+
});
16+
}
17+
18+
var dir = path.join(process.argv[2], 'com.add0n.node');
19+
var name = 'com.add0n.node';
20+
var ids = {
21+
chrome: [
22+
'lmeddoobegbaiopohmpmmobpnpjifpii', // open in Firefox (Chrome)
23+
'agaecbnjediafcdopcfidcdiponjlmnk', // open in Explorer (Chrome)
24+
],
25+
firefox: [
26+
'{5610edea-88c1-4370-b93d-86aa131971d1}', // open in Explorer
27+
]
28+
};
29+
30+
function manifest (type, callback) {
31+
exists(dir, (e) => {
32+
if (e) {
33+
throw e;
34+
}
35+
let origins;
36+
if (type === 'chrome') {
37+
origins = '"allowed_origins": ' + JSON.stringify(ids.chrome.map(id => 'chrome-extension://' + id + '/'));
38+
}
39+
else {
40+
origins = '"allowed_extensions": ' + JSON.stringify(ids.firefox);
41+
}
42+
fs.writeFile(path.join(dir, 'manifest-' + type + '.json'), `{
43+
"name": "${name}",
44+
"description": "Node Host for Native Messaging",
45+
"path": "run.bat",
46+
"type": "stdio",
47+
${origins}
48+
}`, (e) => {
49+
if (e) {
50+
throw e;
51+
}
52+
callback();
53+
});
54+
});
55+
}
56+
function application (callback) {
57+
fs.writeFile(path.join(dir, 'run.bat'), `@echo off\n\n"%~dp0node.exe" "%~dp0host.js"`, (e) => {
58+
if (e) {
59+
throw e;
60+
}
61+
fs.createReadStream('..\\node.exe').pipe(fs.createWriteStream(path.join(dir, 'node.exe')));
62+
fs.createReadStream('host.js').pipe(fs.createWriteStream(path.join(dir, 'host.js')));
63+
fs.createReadStream('messaging.js').pipe(fs.createWriteStream(path.join(dir, 'messaging.js')));
64+
callback();
65+
});
66+
}
67+
68+
function chrome (callback) {
69+
if (ids.chrome.length) {
70+
manifest('chrome', callback);
71+
console.error('Chrome Browser is supported');
72+
}
73+
else {
74+
callback();
75+
}
76+
}
77+
function firefox (callback) {
78+
if (ids.firefox.length) {
79+
manifest('firefox', callback);
80+
console.error('Firefox Browser is supported');
81+
}
82+
else {
83+
callback();
84+
}
85+
}
86+
chrome(() => firefox(() => {
87+
application(() => {
88+
console.error('Native Host is installed in', dir);
89+
console.error('>> Application is ready to use <<');
90+
});
91+
}));

install.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
cd ./app
4+
5+
if type node 2>/dev/null; then
6+
echo Installer is using your system node.js
7+
sudo node install.js `which node`
8+
else
9+
echo Installer is using the attached node.js
10+
sudo ../node install.js --add_node
11+
fi
12+

linux/app/host.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

linux/app/host.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../host.js

0 commit comments

Comments
 (0)