2015-07-24 16:18:53 +00:00
|
|
|
(function (workerScript) {
|
2016-07-23 10:43:39 +00:00
|
|
|
if (!/MSIE 10/i.test (navigator.userAgent)) {
|
|
|
|
try {
|
|
|
|
var blob = new Blob (["\
|
2015-07-24 07:30:34 +00:00
|
|
|
var fakeIdToId = {};\
|
|
|
|
onmessage = function (event) {\
|
|
|
|
var data = event.data,\
|
|
|
|
name = data.name,\
|
|
|
|
fakeId = data.fakeId,\
|
|
|
|
time;\
|
|
|
|
if(data.hasOwnProperty('time')) {\
|
|
|
|
time = data.time;\
|
|
|
|
}\
|
|
|
|
switch (name) {\
|
|
|
|
case 'setInterval':\
|
|
|
|
fakeIdToId[fakeId] = setInterval(function () {\
|
|
|
|
postMessage({fakeId: fakeId});\
|
|
|
|
}, time);\
|
|
|
|
break;\
|
|
|
|
case 'clearInterval':\
|
|
|
|
if (fakeIdToId.hasOwnProperty (fakeId)) {\
|
|
|
|
clearInterval(fakeIdToId[fakeId]);\
|
|
|
|
delete fakeIdToId[fakeId];\
|
|
|
|
}\
|
|
|
|
break;\
|
|
|
|
case 'setTimeout':\
|
|
|
|
fakeIdToId[fakeId] = setTimeout(function () {\
|
|
|
|
postMessage({fakeId: fakeId});\
|
|
|
|
if (fakeIdToId.hasOwnProperty (fakeId)) {\
|
|
|
|
delete fakeIdToId[fakeId];\
|
|
|
|
}\
|
|
|
|
}, time);\
|
|
|
|
break;\
|
|
|
|
case 'clearTimeout':\
|
|
|
|
if (fakeIdToId.hasOwnProperty (fakeId)) {\
|
|
|
|
clearTimeout(fakeIdToId[fakeId]);\
|
|
|
|
delete fakeIdToId[fakeId];\
|
|
|
|
}\
|
|
|
|
break;\
|
|
|
|
}\
|
|
|
|
}\
|
|
|
|
"]);
|
2016-07-23 10:43:39 +00:00
|
|
|
// Obtain a blob URL reference to our worker 'file'.
|
|
|
|
workerScript = window.URL.createObjectURL(blob);
|
|
|
|
} catch (error) {
|
|
|
|
/* Blob is not supported, use external script instead */
|
|
|
|
}
|
2015-07-24 16:18:53 +00:00
|
|
|
}
|
2015-06-28 21:54:41 +00:00
|
|
|
var worker,
|
2015-07-10 14:34:14 +00:00
|
|
|
fakeIdToCallback = {},
|
2015-07-10 15:11:09 +00:00
|
|
|
lastFakeId = 0,
|
2015-12-03 19:08:54 +00:00
|
|
|
maxFakeId = 0x7FFFFFFF, // 2 ^ 31 - 1, 31 bit, positive values of signed 32 bit integer
|
2015-07-10 15:11:09 +00:00
|
|
|
logPrefix = 'HackTimer.js by turuslan: ';
|
2015-06-28 21:54:41 +00:00
|
|
|
if (typeof (Worker) !== 'undefined') {
|
2015-07-10 14:34:14 +00:00
|
|
|
function getFakeId () {
|
2016-01-18 06:15:54 +00:00
|
|
|
do {
|
2015-12-03 19:08:54 +00:00
|
|
|
if (lastFakeId == maxFakeId) {
|
|
|
|
lastFakeId = 0;
|
|
|
|
} else {
|
|
|
|
lastFakeId ++;
|
|
|
|
}
|
2016-01-18 06:15:54 +00:00
|
|
|
} while (fakeIdToCallback.hasOwnProperty (lastFakeId));
|
2015-07-10 14:34:14 +00:00
|
|
|
return lastFakeId;
|
2015-06-28 21:54:41 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
worker = new Worker (workerScript);
|
2015-07-10 17:00:57 +00:00
|
|
|
window.setInterval = function (callback, time /* , parameters */) {
|
2015-07-10 14:34:14 +00:00
|
|
|
var fakeId = getFakeId ();
|
2015-07-10 17:00:57 +00:00
|
|
|
fakeIdToCallback[fakeId] = {
|
|
|
|
callback: callback,
|
|
|
|
parameters: Array.prototype.slice.call(arguments, 2)
|
|
|
|
};
|
2015-06-28 21:54:41 +00:00
|
|
|
worker.postMessage ({
|
|
|
|
name: 'setInterval',
|
|
|
|
fakeId: fakeId,
|
|
|
|
time: time
|
|
|
|
});
|
|
|
|
return fakeId;
|
|
|
|
};
|
|
|
|
window.clearInterval = function (fakeId) {
|
2015-07-10 14:19:19 +00:00
|
|
|
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
|
|
|
delete fakeIdToCallback[fakeId];
|
|
|
|
worker.postMessage ({
|
|
|
|
name: 'clearInterval',
|
|
|
|
fakeId: fakeId
|
|
|
|
});
|
|
|
|
}
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
2015-07-10 17:00:57 +00:00
|
|
|
window.setTimeout = function (callback, time /* , parameters */) {
|
2015-07-10 14:34:14 +00:00
|
|
|
var fakeId = getFakeId ();
|
2015-07-10 17:00:57 +00:00
|
|
|
fakeIdToCallback[fakeId] = {
|
|
|
|
callback: callback,
|
2015-12-03 19:08:54 +00:00
|
|
|
parameters: Array.prototype.slice.call(arguments, 2),
|
|
|
|
isTimeout: true
|
2015-07-10 17:00:57 +00:00
|
|
|
};
|
2015-06-28 21:54:41 +00:00
|
|
|
worker.postMessage ({
|
|
|
|
name: 'setTimeout',
|
|
|
|
fakeId: fakeId,
|
|
|
|
time: time
|
|
|
|
});
|
|
|
|
return fakeId;
|
|
|
|
};
|
|
|
|
window.clearTimeout = function (fakeId) {
|
2015-07-10 14:19:19 +00:00
|
|
|
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
|
|
|
delete fakeIdToCallback[fakeId];
|
|
|
|
worker.postMessage ({
|
|
|
|
name: 'clearTimeout',
|
|
|
|
fakeId: fakeId
|
|
|
|
});
|
|
|
|
}
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
|
|
|
worker.onmessage = function (event) {
|
|
|
|
var data = event.data,
|
|
|
|
fakeId = data.fakeId,
|
2015-07-10 17:00:57 +00:00
|
|
|
request,
|
|
|
|
parameters,
|
2015-07-10 14:19:19 +00:00
|
|
|
callback;
|
|
|
|
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
2015-07-10 17:00:57 +00:00
|
|
|
request = fakeIdToCallback[fakeId];
|
|
|
|
callback = request.callback;
|
|
|
|
parameters = request.parameters;
|
2015-12-03 19:08:54 +00:00
|
|
|
if (request.hasOwnProperty ('isTimeout') && request.isTimeout) {
|
|
|
|
delete fakeIdToCallback[fakeId];
|
|
|
|
}
|
2015-07-10 14:19:19 +00:00
|
|
|
}
|
2015-07-10 14:34:14 +00:00
|
|
|
if (typeof (callback) === 'string') {
|
|
|
|
try {
|
|
|
|
callback = new Function (callback);
|
2015-07-10 15:11:09 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log (logPrefix + 'Error parsing callback code string: ', error);
|
2015-07-10 14:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-10 14:19:19 +00:00
|
|
|
if (typeof (callback) === 'function') {
|
2015-07-10 17:00:57 +00:00
|
|
|
callback.apply (window, parameters);
|
2015-07-10 14:19:19 +00:00
|
|
|
}
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
|
|
|
worker.onerror = function (event) {
|
2015-07-10 15:11:09 +00:00
|
|
|
console.log (event);
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
|
|
|
} catch (error) {
|
2015-07-10 15:11:09 +00:00
|
|
|
console.log (logPrefix + 'Initialisation failed');
|
|
|
|
console.error (error);
|
2015-06-28 21:54:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-07-10 15:11:09 +00:00
|
|
|
console.log (logPrefix + 'Initialisation failed - HTML5 Web Worker is not supported');
|
2015-06-28 21:54:41 +00:00
|
|
|
}
|
2017-05-28 15:03:53 +00:00
|
|
|
}) ('HackTimerWorker.js');
|