-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauto-reloader.js
197 lines (183 loc) · 4.57 KB
/
auto-reloader.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* AutoReloader.js
* auto reload browser if files were modified.
* @see https://github.com/tanabe/AutoReloader-js
* @author Hideaki Tanabe <[email protected]>
* @usage
* include this file on page bottom
*/
(function() {
var TARGET_FILES = [
{path: location.href, lastModified: null}
];
var INTERVAL = 500;
var WATCH_MAX_TIME = 1000 * 60 * 3;//watching 3 minutes
var counter = 0;
var isBusy = false;
var now = new Date();
var intervalId;
/**
* create XMLHTTPRequest object
* @name createXMLHTTPRequest
* @function
* @return XMLHttpRequest object
*/
var createXMLHTTPRequest = function() {
//this code borrowed from
//https://github.com/hirokidaichi/namespace-js/blob/master/src/namespace.js
var xhr;
try {xhr = new XMLHttpRequest()} catch(e) {
try {xhr = new ActiveXObject("Msxml2.XMLHTTP.6.0")} catch(e) {
try {xhr = new ActiveXObject("Msxml2.XMLHTTP.3.0")} catch(e) {
try {xhr = new ActiveXObject("Msxml2.XMLHTTP")} catch(e) {
try {xhr = new ActiveXObject("Microsoft.XMLHTTP")} catch(e) {
throw new Error("This browser does not support XMLHttpRequest.")
}
}
}
}
}
return xhr;
};
/**
* reload browser
* @name reload
* @function
*/
var reload = function() {
location.reload();
};
/**
* polling
* @name polling
* @function
*/
var polling = function() {
intervalId = setInterval(function() {
var timesAfter = (new Date().getTime()) - now.getTime();
if (timesAfter > WATCH_MAX_TIME) {
stopPolling();
}
//console.log(timesAfter);
if (!isBusy) {
checkUpdate(counter);
if (counter < TARGET_FILES.length - 1) {
counter++;
} else {
counter = 0;
}
}
}, INTERVAL);
};
/**
* stop polling
* @name stopPolling
* @function
*/
var stopPolling = function() {
clearInterval(intervalId);
};
/**
* check initialized
* @name isInitialied
* @function
* @return initialized then true
*/
var isInitialied = function() {
for (var i = 0; i < TARGET_FILES.length; i++) {
if (TARGET_FILES[i].lastModified === null) {
return false;
}
}
return true;
};
/**
* check update
* @name checkUpdate
* @function
* @param index
*/
var checkUpdate = function(index) {
isBusy = true;
var fileName = TARGET_FILES[index].path;
var lastModified = TARGET_FILES[index].lastModified;
var xhr = createXMLHTTPRequest();
//thanks for your advice @oogatta
xhr.open("HEAD", fileName, true);
if (lastModified) {
xhr.setRequestHeader("If-Modified-Since", lastModified + " ");
}
//event handler
xhr.onreadystatechange = function() {
if(xhr.readyState === 4){
var fileLastModified = xhr.getResponseHeader("Last-Modified");
if (xhr.status === 200) {
if (isInitialied()) {
if (lastModified !== fileLastModified) {
TARGET_FILES[index].lastModified = fileLastModified;
reload();
}
} else {
TARGET_FILES[index].lastModified = fileLastModified;
}
}
}
isBusy = false;
};
xhr.send("")
};
var isAdded = function(path) {
for (var i = 0; i < TARGET_FILES.length; i++) {
if (TARGET_FILES[i].path === path) {
return true;
}
}
return false;
};
//define interface
window.AutoReloader = {
/**
* add watch target file
* @name add
* @function
*/
add: function() {
for (var i = 0; i < arguments.length; i++) {
var path = arguments[i];
if (!isAdded(path)) {
TARGET_FILES.push({path: path, lastModified: null});
}
}
},
/**
* watch all css
* @name watchCSS
* @function
*/
watchCSS: function() {
var scriptTags = document.getElementsByTagName("link");
var stylesheets = [];
for (var i = 0; i < scriptTags.length; i++) {
if (scriptTags[i].type === "text/css" && scriptTags[i].href) {
this.add(scriptTags[i].href);
}
}
},
/**
* watch all JavaScript
* @name watchJS
* @function
*/
watchJS: function() {
var scriptTags = document.getElementsByTagName("script");
var stylesheets = [];
for (var i = 0; i < scriptTags.length; i++) {
if (scriptTags[i].type === "text/javascript" && scriptTags[i].src) {
this.add(scriptTags[i].src);
}
}
}
};
//begin polling
polling();
})();