-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (49 loc) · 1.36 KB
/
server.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
var Chrome = require('chrome-remote-interface');
var fs = require("fs");
var exec = require('child_process').exec;
var FOLDER = 'public';
var url2scriptId = { };
var onFsChange;
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
exec('./node_modules/http-server/bin/http-server');
fs.watch(FOLDER,function(event,file){
if(onFsChange){
onFsChange(file);
}
});
Chrome(function (chrome) {
chrome.on('Debugger.scriptParsed', function (script) {
url2scriptId[script.url] = script.scriptId;
});
chrome.Debugger.disable();
chrome.Debugger.enable();
onFsChange = function(file){
console.log('file',file);
if(endsWith(file,'.js')){
var scriptId;
for(url in url2scriptId){
console.log(url,url2scriptId[url]);
if(endsWith(url,file)){
scriptId = url2scriptId[url];
}
}
if(typeof scriptId !== 'undefined'){
console.log('try to update script',scriptId);
console.log('readingFIle',FOLDER+'/'+file);
fs.readFile(FOLDER+'/'+file,{encoding:'utf8'},function(err,contents){
console.log('scriptId',scriptId,'contents',contents);
chrome.Debugger.setScriptSource({
scriptId: scriptId,
scriptSource: contents
});
});
}else{
console.log('could not find script id');
}
}
};
}).on('error', function () {
console.error('Cannot connect to Chrome',arguments);
});