-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivephp.js
More file actions
122 lines (105 loc) · 4.26 KB
/
livephp.js
File metadata and controls
122 lines (105 loc) · 4.26 KB
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
/**
* Live.php
* @author Bence Meszaros
* @link http://bencemeszaros.com
* @version 1.2
*/
var LivePhp = {
interval: 1000,
url: '',
start: 0,
errors: 0,
errorLimit: 10,
currentLinkElements: {},
/** Initializes the start time and the query cicle */
init: function() {
// get the url for our php script (which is just beside this js file)
LivePhp.url = LivePhp.scriptSource().replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, '') + '/livephpmonitor.php';
if (0 === LivePhp.start) {
LivePhp.start = new Date() * 1;
setTimeout(LivePhp.heartbeat, LivePhp.interval);
}
},
/** Reload all local css files */
reloadCss: function() {
// helper method to check if a given url is local
function isLocal(url) {
var loc = document.location,
reg = new RegExp("^\\.|^\/(?!\/)|^[\\w]((?!://).)*$|" + loc.protocol + "//" + loc.host);
return url.match(reg);
}
var links = document.getElementsByTagName("link");
for (var i = 0; i < links.length; i++) {
var link = links[i], rel = link.getAttribute("rel"), href = link.getAttribute("href");
if (href && rel && rel.match(new RegExp("stylesheet", "i")) && isLocal(href)) {
// remove any url params
var res = href.match(/(.*)\?.*/);
href = res && res[1] ? res[1] : href;
LivePhp.currentLinkElements[href] = link;
}
}
for (var url in LivePhp.currentLinkElements) {
var head = LivePhp.currentLinkElements[url].parentNode,
newLink = document.createElement("link"),
oldLink = LivePhp.currentLinkElements[url];
newLink.setAttribute("href", url + "?timestamp=" + new Date() * 1);
newLink.setAttribute("rel", "stylesheet");
newLink.addEventListener('load', function() {
setTimeout((function(node){return function() {
node.parentNode.removeChild(node);
};})(oldLink), 100);
}(oldLink), false);
head.appendChild(newLink);
LivePhp.currentLinkElements[url] = newLink;
}
},
scriptSource: function(scripts) {
var scripts = document.getElementsByTagName('script'),
script = scripts[scripts.length - 1];
if (script.getAttribute.length !== undefined) {
return script.src;
}
return script.getAttribute('src', -1);
},
/** performs a cycle per interval */
heartbeat: function() {
if (document.body) {
LivePhp.ask();
}
},
/** Queries the server for changes, and reloads the page on positive answer */
ask: function() {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XmlHttp");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var restart = new Date() * 1;
if (xhr.responseText != '' && xhr.status == 200) {
var re = JSON.parse(xhr.responseText);
if (re === true) {
location.reload();
return true;
}
else {
// we got a number, meaning we should reload all local css files
restart = typeof re == "number" ? re : restart;
LivePhp.reloadCss();
}
}
// Error, or no response at all
else if (xhr.status >= 400 || xhr.status == 0) {
// After 10 errors we stop asking
LivePhp.errors ++;
if (LivePhp.errors >= LivePhp.errorLimit) {
return false;
}
}
// reset the start time
LivePhp.start = restart;
setTimeout(LivePhp.heartbeat, LivePhp.interval);
}
};
xhr.open("GET", LivePhp.url + '?s=' + LivePhp.start, true);
xhr.send();
}
};
LivePhp.init();