Initial commit

This commit is contained in:
turuslan 2015-06-29 02:54:41 +05:00
commit 59b77f9893
4 changed files with 96 additions and 0 deletions

64
HackTimer.js Normal file
View File

@ -0,0 +1,64 @@
(function (workerScript) {
var worker,
fakeIdToCallback = {};
if (typeof (Worker) !== 'undefined') {
function getFakeId (prefix) {
return prefix + Math.floor(
Math.random () *
Number.MAX_VALUE
);
}
try {
worker = new Worker (workerScript);
window.setInterval = function (callback, time) {
var fakeId = getFakeId ('i-');
fakeIdToCallback[fakeId] = callback;
worker.postMessage ({
name: 'setInterval',
fakeId: fakeId,
time: time
});
return fakeId;
};
window.clearInterval = function (fakeId) {
delete fakeIdToCallback[fakeId];
worker.postMessage ({
name: 'clearInterval',
fakeId: fakeId
});
};
window.setTimeout = function (callback, time) {
var fakeId = getFakeId ('t-');
fakeIdToCallback[fakeId] = callback;
worker.postMessage ({
name: 'setTimeout',
fakeId: fakeId,
time: time
});
return fakeId;
};
window.clearTimeout = function (fakeId) {
delete fakeIdToCallback[fakeId];
worker.postMessage ({
name: 'clearTimeout',
fakeId: fakeId
});
};
worker.onmessage = function (event) {
var data = event.data,
fakeId = data.fakeId,
callback = fakeIdToCallback[fakeId];
callback.call (window);
};
worker.onerror = function (event) {
console.log(event);
};
console.log('HackTimer.js by turuslan: Initialisation succeeded');
} catch (error) {
console.log('HackTimer.js by turuslan: Initialisation failed');
console.error(error);
}
} else {
console.log('HackTimer.js by turuslan: Initialisation failed - HTML5 Web Worker is not supported');
}
})('HackTimerWorker.js');

1
HackTimer.min.js vendored Normal file
View File

@ -0,0 +1 @@
(function(s){var w,f={},o=window,m=Math;if(typeof(Worker)!=='undefined'){function g(p){return p+m.floor(m.random()*Number.MAX_VALUE);}try{w=new Worker(s);o.setInterval=function(c,t){var i=g('i-');f[i]=c;w.postMessage({n:'a',i:i,t:t});return i;};o.clearInterval=function(i){delete f[i];w.postMessage({n:'b',i:i});};o.setTimeout=function(c,t){var i=g('t-');f[i]=c;w.postMessage({n:'c',i:i,t:t});return i;};o.clearTimeout=function(i){delete f[i];w.postMessage({n:'d',i:i});};w.onmessage=function(e){f[e.data.i].call(o);};w.onerror=function(e){console.log(e);};console.log('HackTimer.js by turuslan: Initialisation succeeded');}catch(e){console.log('HackTimer.js by turuslan: Initialisation failed');console.error(e);}}else{console.log('HackTimer.js by turuslan: Initialisation failed - HTML5 Web Worker is not supported');}})('HackTimerWorker.min.js');

30
HackTimerWorker.js Normal file
View File

@ -0,0 +1,30 @@
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':
clearInterval(fakeIdToId[fakeId]);
delete fakeIdToId[fakeId];
break;
case 'setTimeout':
fakeIdToId[fakeId] = setTimeout(function () {
postMessage({fakeId: fakeId});
}, time);
break;
case 'clearTimeout':
clearTimeout(fakeIdToId[fakeId]);
delete fakeIdToId[fakeId];
break;
}
}

1
HackTimerWorker.min.js vendored Normal file
View File

@ -0,0 +1 @@
var f={};onmessage=function(e){var d=e.data,i=d.i,t=d.hasOwnProperty('t')?d.t:0;switch(d.n){case'a':f[i]=setInterval(function(){postMessage({i:i});},t);break;case'b':clearInterval(f[i]);delete f[i];break;case'c':f[i]=setTimeout(function(){postMessage({i:i});},t);break;case'd':clearTimeout(f[i]);delete f[i];break;}}