-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (88 loc) · 2.69 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var path = require('path');
var bodyParser = require('body-parser');
var Monitor = require('page-monitor');
var opn = require('opn');
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var activelyMonitoring = false;
var monitor;
var url;
var pingSite;
//allow parsing and referencing postback data
app.use(bodyParser.urlencoded({
extended: true
}));
//allow express to use files in the public folder
app.use(express.static('public'));
//initiate express
app.listen(3000, function () {
console.log("express is listening on port 3000");
});
app.route('/')
.get(function (req, res) {
res.render(path.join(__dirname + '/public/index.ejs'));
console.log(__dirname + '/public/index.ejs');
});
app.route('/monitor')
.post(function (req, res) {
url = req.body.userInput;
url = fixUrl(url);
monitorSite();
res.json(url);
});
//when user navigates away from page
app.route('/stop')
.get(function (req, res) {
if (activelyMonitoring) {
console.log("stopping interval");
clearInterval(pingSite);
console.log("interval stopped");
activelyMonitoring = false;
}
});
function fixUrl(urlToFix){
const prefix = "http";
if (urlToFix.substr(0, prefix.length) !== prefix) {
urlToFix = prefix + "://" + urlToFix;
}
if (!urlToFix.includes("."))
{
urlToFix += ".com";
}
return urlToFix;
}
function monitorSite() {
monitor = new Monitor(url);
activelyMonitoring = true;
io.once('connection', function(socket){
console.log("socket.io connected on server-side");
socket.on('disconnect', function(){
console.log("socket disconnected");
});
pingSite = setInterval(function () {
monitor.capture(function (code) {
console.log(monitor.log.info);
//open webpage if changes are found
if (monitor.log.info.length != 0 && activelyMonitoring) {
//opn(url);
socket.emit('site change', {url: url});
}
//console.log('[DONE] exit [' + code + ']');
});
}, 2000);
});
server.listen(3001, function(){
console.log("socket.io listening on port 3001");
});
monitor.on('debug', function (data) {
//console.log(data);
});
monitor.on('error', function (data) {
console.error('[ERROR] ' + data);
});
};
server.listen(3001, function(){
console.log("socket.io listening on port 3001");
});