-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbrowser_connection.js
82 lines (69 loc) · 2.79 KB
/
browser_connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var sockjs = require('sockjs'),
colorize = require('colorize'),
spawn = require("child_process").spawn;
function runChild(command, args, callback) {
var child = spawn(command, args, {customFds:[0,1,2]}),
data = [];
child.on("exit",function(code, signal){
if(callback) {
callback(code);
}
});
}
(function () {
var timeoutValue = 10*60*1000;
function Browser(verbose) {
this.cconsole = verbose ? colorize.console : {log:function(){}};
this.connections = [];
this.browser_connection = sockjs.createServer();
this.browser_connection.on('connection', this.newConnection.bind(this));
return this;
}
Browser.prototype.newConnection = function(conn) {
conn.on('data', this.messageReceived.bind(this, conn));
conn.on('close', this.connectionClosed.bind(this, conn));
};
Browser.prototype.messageReceived = function(conn, message) {
var that = this,
msgObj = null,
curDate = new Date().getTime();
if (!conn.lastUsed || conn.lastUsed - curDate < timeoutValue) {
conn.lastUsed = curDate;
if (conn.timer) {
clearTimeout(conn.timer);
}
conn.timer = setTimeout(function() {
conn.close();
}, timeoutValue + 100);
}
try {
msgObj = JSON.parse(message);
} catch(e) {}
if (msgObj && msgObj.status === "ready" && msgObj.windowName) {
conn.windowName = msgObj.windowName;
conn.id = msgObj.id;
conn.windowLoc = msgObj.windowLoc;
conn.windowType = msgObj.windowType;
conn.backgroundSupported = msgObj.backgroundSupported;
this.cconsole.log('#yellow[Browser ' + conn.id + ' connected]');
this.connections.push(conn);
if(msgObj.backgroundSupported && /application/i.test(msgObj.windowType)) {
this.cconsole.log('#yellow[Trying to close background modal dialog]');
runChild("powershell.exe", [__dirname + '\\helpers\\simulate_keyboard.ps1 KeyPress TAB']);
runChild("powershell.exe", [__dirname + '\\helpers\\simulate_keyboard.ps1 KeyPress RETURN']);
conn.write(JSON.stringify({
internalCommand: 'resetBackgroundSupported'
}));
}
}
if(conn.id) {
this.cconsole.log('#red[ < Browser ' + conn.id + ' message]');
this.cconsole.log('\t' + message);
}
};
Browser.prototype.connectionClosed = function(conn) {
this.cconsole.log('#yellow[Browser ' + conn.id + ' disconnected]');
this.connections.splice(this.connections.indexOf(conn), 1);
};
exports.Browser = Browser;
})();