forked from Garethp/Screeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
141 lines (132 loc) · 3.9 KB
/
sync.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict";
var http = require('http');
var path = require('path');
var fs = require('fs');
var URL = require('url');
// This all runs in the browser
var clientSide = function() {
// Grab reference to the commit button
var buttons = Array.prototype.slice.call(document.body.getElementsByTagName('button')).filter(function(el) {
return el.getAttribute('ng:disabled') === '!Script.dirty';
});
var commitButton = buttons[0];
// Override lodash's cloneDeep which is called from inside the internal reset method
var modules;
_.cloneDeep = function(cloneDeep) {
return function(obj) {
if (typeof obj.main === 'string' && modules) {
// Monkey patch!
return modules;
}
return cloneDeep.apply(this, arguments);
};
}(_.cloneDeep);
// Wait for changes to local filesystem
function update(now) {
var req = new XMLHttpRequest;
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
modules = JSON.parse(req.responseText);
commitButton.disabled = false;
commitButton.click();
}
setTimeout(update.bind(this, false), req.status === 200 ? 0 : 1000);
}
};
req.open('GET', 'http://localhost:9090/'+ (now ? 'get' : 'wait'), true);
req.send();
};
update(true);
// Look for console messages
var sconsole = document.body.getElementsByClassName('console-messages-list')[0];
var lastMessage;
setInterval(function() {
var nodes = sconsole.getElementsByClassName('console-message');
var messages = [];
var found = false;
for (var ii = 0; ii < nodes.length; ++ii) {
var el = nodes[ii];
if (el.innerHTML === lastMessage) {
found = true;
} else if (found) {
var ts = el.getElementsByClassName('timestamp')[0];
ts = ts && ts.firstChild.nodeValue;
var msg = el.getElementsByTagName('span')[0].childNodes;
var txt = '';
for (var jj = 0; jj < msg.length; ++jj) {
if (msg[jj].tagName === 'BR') {
txt += '\n';
} else {
txt += msg[jj].nodeValue;
}
}
messages.push([ts, txt]);
}
}
if (messages.length) {
var req = new XMLHttpRequest;
req.open('GET', 'http://localhost:9090/log?log='+ encodeURIComponent(JSON.stringify(messages)), true);
req.send();
}
lastMessage = nodes.length && nodes[nodes.length - 1].innerHTML;
}, 100);
};
// Set up watch on directory changes
var modules = {};
var writeListener;
fs.readdirSync('.').forEach(function(file) {
if (file !== 'sync.js' && /\.js$/.test(file)) {
modules[file.replace(/\.js$/, '')] = fs.readFileSync(file, 'utf8');
}
});
fs.watch(__dirname, function(ev, file) {
if (file !== 'sync.js' && /\.js$/.test(file)) {
modules[file.replace(/\.js$/, '')] = fs.readFileSync(file, 'utf8');
if (writeListener) {
process.nextTick(writeListener);
writeListener = undefined;
}
}
});
// Localhost HTTP server
var server = http.createServer(function(req, res) {
var path = URL.parse(req.url, true);
switch (path.pathname) {
case '/inject':
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.end('~'+ clientSide.toString()+ '()');
break;
case '/get':
case '/wait':
if (writeListener) {
writeListener();
}
writeListener = function() {
res.writeHead(200, { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });
res.end(JSON.stringify(modules));
};
if (req.url === '/get') {
writeListener();
}
break;
case '/log':
res.writeHead(200, { 'Access-Control-Allow-Origin': '*' });
res.end();
var messages = JSON.parse(path.query.log);
for (var ii = 0; ii < messages.length; ++ii) {
console.log(messages[ii][0], messages[ii][1]);
}
break;
default:
res.writeHead(400);
res.end();
break;
}
});
server.timeout = 0;
server.listen(9090);
console.log(
"Paste this into JS debug console in Screeps (*not* the Screeps console):\n"+
"var s = document.createElement('script');s.src='http://localhost:9090/inject';document.body.appendChild(s);"
);