-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoreload.js
executable file
·32 lines (27 loc) · 970 Bytes
/
autoreload.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
// Autoreload Injected by microprojects
if (!window.frameElement) {
var lastChange = 0
var xhttp = new XMLHttpRequest()
// find root of project
var urlSplit = window.location.pathname.split('/')
var urlRoot = urlSplit.slice(0, urlSplit.indexOf('microprojects') + 1).join('/')
var reloadLocation = urlRoot + '/reload.json'
// var reloadFileLocation = window.location.pathname.
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText)
if(lastChange && data.changed !== lastChange){
console.log('Changes Detected. Reloading Project...')
location.reload()
return
}
lastChange = data.changed
setTimeout(function() {
xhttp.open("GET", reloadLocation, true)
xhttp.send()
}, 500)
}
}
xhttp.open("GET", reloadLocation, true)
xhttp.send();
}