-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstano.hack.js
234 lines (213 loc) · 8.77 KB
/
instano.hack.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* instano.hack.js - Instant NoScript Detection [hackers' edition]
*
* authors: Antony Lau and Zbyszek Tenerowicz
* email: [email protected]
* license : MIT
*/
var instano = (function (settings) {
"use strict";
settings || (settings = {});
settings.interval || (settings.interval = 100); // rejects 0
(settings.displayStyle && settings.displayStyle === "block") || (settings.displayStyle = "inline-block");
settings.indicator || (settings.indicator = false);
//settings.reenabledCallback
//settings.disabledCallback
(settings.disabledCallbackDelay === "setTimeout" || settings.disabledCallbackDelay === "setInterval") || (settings.disabledCallbackDelay = false);
(typeof settings.disabledCallbackDuration === "number" && settings.disabledCallbackDuration >= 0) || (settings.disabledCallbackDuration = false);
/* Testing for CSS animation support
***************************************************************************/
var isAnimationSupported = false,
domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
testel = document.createElement('div');
if(typeof testel.style.animationName !== "undefined") { isAnimationSupported = true; }
if( isAnimationSupported === false ) {
for( var i = 0; i < domPrefixes.length; i++ ) {
if( typeof testel.style[ domPrefixes[i] + 'AnimationName' ] !== "undefined" ) {
isAnimationSupported = true;
break;
}
}
}
/* Detect if JavaScript is disabled and reenabled (not supported by Opera)
***************************************************************************/
//requestAnimationFrame crossbrowser
var reqFrame = (function() {
var lastTime = 0,
vendors = ['ms', 'moz', 'webkit', 'o'],
reqFrame = window.requestAnimationFrame;
for(var x = 0; x < vendors.length && !reqFrame; ++x) {
reqFrame = window[vendors[x]+'RequestAnimationFrame'];
}
if (!reqFrame){
reqFrame = function(callback, element) {
var currTime = Date.now();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
return reqFrame;
})();
var continuousTime = Date.now(), javascriptTime = Date.now(), reportStatus = true,
disabledDuration = 0, executeTimeout = false, executeInterval = false, executeTime;
function step(timestamp) {
continuousTime = Date.now();
if (reportStatus) {
if ((continuousTime - javascriptTime) >= 500) {
disabledDuration++;
if (disabledDuration > 10) {
// JavaScript is disabled
if (typeof settings.disabledCallback === "function") {
if (settings.disabledCallbackDelay !== false && settings.disabledCallbackDuration !== false) {
if (settings.disabledCallbackDelay === "setTimeout") executeTimeout = true;
if (settings.disabledCallbackDelay === "setInterval") executeInterval = true;
executeTime = continuousTime + settings.disabledCallbackDuration;
} else {
settings.disabledCallback();
}
}
reportStatus = false;
}
}
} else {
if ((continuousTime - javascriptTime) < 500) {
// JavaScript is reenabled
if (typeof settings.reenabledCallback === "function") {
settings.reenabledCallback();
}
disabledDuration = 0;
executeTimeout = false;
executeInterval = false;
reportStatus = true;
}
}
if (executeTimeout) {
if (continuousTime >= executeTime) {
settings.disabledCallback();
executeTimeout = false;
}
}
if (executeInterval) {
if (continuousTime >= executeTime) {
settings.disabledCallback();
executeTime = continuousTime + settings.disabledCallbackDuration;
}
}
reqFrame(step);
}
//initialize
reqFrame(step);
// standard reference timestamp
setInterval(function() {
javascriptTime = Date.now();
}, 16);
/* Create the CSS animation class
***************************************************************************/
if (isAnimationSupported) {
var css =
'.nojs_init { position:relative; display:inline-block; vertical-align:top; animation:nojs-animation 0.2s step-end; -moz-animation:nojs-animation 0.2s step-end; -webkit-animation:nojs-animation 0.2s step-end; -o-animation:nojs-animation 0.2s step-end; } ' +
'@keyframes nojs-animation { from {width:0px;height:0px;visibility:hidden;opacity:0;} to {width:1px;height:1px;visibility:visible;opacity:1;} } ' +
'@-moz-keyframes nojs-animation { from {width:0px;height:0px;visibility:hidden;opacity:0;} to {width:1px;height:1px;visibility:visible;opacity:1;} } ' +
'@-webkit-keyframes nojs-animation { from {width:0px;height:0px;visibility:hidden;opacity:0;} to {width:1px;height:1px;visibility:visible;opacity:1;} } ' +
'@-o-keyframes nojs-animation { from {width:0px;height:0px;visibility:hidden;opacity:0;} to {width:1px;height:1px;visibility:visible;opacity:1;} }';
if ('\v' == 'v') /* ie only */ {
document.createStyleSheet().cssText = css;
}
else {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
document.getElementsByTagName('head')[0].appendChild(style);
}
//start working immediately
init(settings.indicator);
}
// Check if object o is a DOM element
function isElement(o){
return (
typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
o && typeof o === "object" && o.nodeType === 1 && typeof o.nodeName==="string"
);
}
//inits
function init(el){
if (!el) {
// Find all the noscript tags
var nos = document.getElementsByTagName("noscript");
} else if (Object.prototype.toString.call(el) === "[object Array]") {
var nos = [];
for (var i = 0; i < el.length; i++) {
if (isElement(el[i])) {
var nosj = el[i].getElementsByTagName("noscript");
for (var j in nosj) {
if (isElement(nosj[j])) nos.push(nosj[j]);
}
} else if (document.getElementById(el[i])) {
var nosj = document.getElementById(el[i]).getElementsByTagName("noscript");
for (var j in nosj) {
if (isElement(nosj[j])) nos.push(nosj[j]);
}
}
}
} else if (isElement(el)) {
// Find the noscript tags within an element
var nos = el.getElementsByTagName("noscript");
} else if (document.getElementById(el)) {
var nos = document.getElementById(el).getElementsByTagName("noscript");
}
var elreplace = {};
if (nos) {
for (var i = 0; i < nos.length; i++) {
var newone = document.createElement('div');
newone.id = "jsdetect_" + i;
newone.className = "nojs_init";
newone.style.display = settings.displayStyle;
newone.innerHTML = nos[i].textContent;
elreplace[i] = {
parent: nos[i].parentNode,
newel: newone,
original: nos[i]
};
}
for (var i in elreplace) {
// Replace noscript with span
elreplace[i].parent.replaceChild(elreplace[i].newel, elreplace[i].original);
}
// Continuously replace element to stop animation
setInterval(function() {
for (var i in elreplace) {
var el = document.getElementById(elreplace[i].newel.id);
var newone = el.cloneNode(true);
el.parentNode.replaceChild(newone, el);
}
}, settings.interval);
// Bug fix for #3
// Stop noscript tags from showing up in Firefox while reloading
window.addEventListener("beforeunload", function() {
// Hide all the noscript tags since in Firefox JavaScript will
// stop running momentarily before the page is reloaded
for (var i in elreplace) {
var el = document.getElementById(elreplace[i].newel.id);
el.style.display = "none";
}
// But in case the user canceled the reload in the onbeforeunload
// prompt, restore the noscript tags so they will work again
// Use two timeouts to detect cancellation. The first timeout will
// not fire until the prompt is dismissed and the second timeout
// will only fire when the prompt is cancelled.
setTimeout(function() {
setTimeout(function() {
for (var i in elreplace) {
var el = document.getElementById(elreplace[i].newel.id);
el.style.display = settings.displayStyle;
}
}, 0);
}, 0);
});
}
}
//init-end
});