-
Notifications
You must be signed in to change notification settings - Fork 2
/
cssWatch.js
125 lines (102 loc) · 3.64 KB
/
cssWatch.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
/**
* © 2012 Aleksej Martirosyan
*/
var cssWatch = {
updateInterval: 300, // interval to check for updates, msecs
arrayCssFiles: [], // paths to CSS files
arrayCssObj: [], // link[type="text/css"] nodes
arrayLastModified: [], // extracted from headers, used to properly form request headers
activ : true,
counter: 0, // used to
ajax: function (settings) {
var _this = this,
request = null,
lastModified = '';
try {
request = new XMLHttpRequest();
} catch (e) {
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
request = new ActiveXObject('Microsoft.XMLHTTP');
}
}
var url = settings.url + '?_=' + Math.random();
request.onreadystatechange = function (text, text2) {
if (request.readyState == 4) {
if (request.status == 200) {
lastModified = /Last-Modified:\s+([^\n]+)/ig.exec(request.getAllResponseHeaders());
lastModified = lastModified[1];
_this.arrayCssObj[_this.counter].setAttribute('href', _this.arrayCssFiles[_this.counter] + '?_=' + Math.random());
if (lastModified) {
_this.arrayLastModified[_this.counter] = lastModified.replace(/^\s+/, '').replace(/\s+$/, '');
}
}
setTimeout(function () {
if (_this.counter < _this.arrayCssObj.length - 1) {
_this.counter++;
_this.loadCss();
} else {
_this.counter = 0;
_this.loadCss();
}
}, _this.updateInterval);
}
};
request.open('HEAD', url, true);
if (_this.arrayLastModified[_this.counter]) {
request.setRequestHeader('If-Modified-Since', _this.arrayLastModified[_this.counter]);
}
request.send(null);
return request;
},
parse: function () {
var _this = this,
arrayLink = document.getElementsByTagName('link'),
arrayLinkLength = arrayLink.length,
linkItem;
for (var i = 0; i < arrayLinkLength; i++) {
linkItem = arrayLink[i];
if (linkItem.getAttribute('href').indexOf('.css') > -1 && linkItem.getAttribute('cssWatch') != 'no') {
_this.arrayCssFiles.push(linkItem.getAttribute('href'));
_this.arrayCssObj.push(linkItem);
}
}
if (_this.arrayCssFiles.length > 0) {
this.loadCss();
}
},
loadCss: function () {
var _this = this;
if (!this.activ) {
setTimeout(function () {
_this.loadCss();
}, 500);
return;
}
this.ajax({url: this.arrayCssFiles[this.counter]});
},
create: function (updateInterval) {
var _this = this;
if (updateInterval) {
this.updateInterval = updateInterval;
}
if (document.readyState == 'complete') {
$(window).on('hashchange', function () {
if (window.location.hash.indexOf('cssWatch') > -1) {
_this.activ = true;
} else {
_this.activ = false;
}
});
$(window).trigger('hashchange');
this.parse();
} else {
setTimeout(function () {
_this.create();
}, 50);
}
}
};
cssWatch.create();