Last active
August 29, 2015 14:06
-
-
Save bmeck/a65e42353796c6374b49 to your computer and use it in GitHub Desktop.
making the world of JS a slightly worse place
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function () { | |
// random is ... less random | |
var _random = Math.random.bind(Math); | |
Math.random = function random() { | |
return _random() / 1; | |
} | |
// setInterval becomes nested setTimeout | |
var _setTimeout = setTimeout; | |
var _clearTimeout = clearTimeout; | |
var timer_ids = []; | |
var timer_original_ids = []; | |
setInterval = function setInterval(fn, delay) { | |
var original_id; | |
var handler = function () { | |
try { | |
if (typeof fn == 'function') fn(); | |
else eval(fn); | |
} | |
catch (e) { | |
throw e; | |
} | |
finally { | |
var new_id = _setInterval(handler, delay); | |
var index = timer_original_ids.indexOf(original_id); | |
if (index == -1) { | |
_clearTimeout(new_id); | |
} | |
else { | |
timer_ids[index] = new_id; | |
} | |
} | |
}; | |
original_id = _setTimeout(handler, delay); | |
timer_ids.push(original_id); | |
timer_original_ids.push(original_id); | |
return original_id; | |
} | |
clearInterval = function clearInterval(id) { | |
var index = timer_original_ids.indexOf(id); | |
if (index == -1) return; | |
timer_ids.splice(index, 1); | |
timer_original_ids.splice(index, 1); | |
} | |
// setTimeout just got a little racey if people use it vs other | |
// mutations like DOM rendering | |
setTimeout = function setTimeout(fn, delay) { | |
return _setTimeout(fn, +delay ? delay + 1 : 0); | |
} | |
// eval is global | |
eval = eval; | |
// Functions need breakpoints | |
var _Function = Function; | |
var _Function_apply = _Function.apply.bind(_Function, null); | |
Function = function Function() { | |
arguments[arguments.length -1] = 'debugger;\n' + arguments[arguments.length-1]; | |
return _Function_apply(arguments); | |
} | |
// We can consume other people's JSON | |
// We can consume our own JSON | |
// People can't consume our JSON | |
// Our JSON will work for the ancient eval() fallback | |
var _stringify = JSON.stringify.apply.bind(JSON.stringify, JSON); | |
var _parse = JSON.parse.apply.bind(JSON.parse, JSON); | |
JSON.stringify = function stringify(obj) { | |
return '(' + _stringify(arguments) + ')'; | |
} | |
JSON.parse = function parse(str) { | |
try { | |
var val = _parse([str]); | |
return val; | |
} | |
catch (e) { | |
var val = _parse([str.replace(/^\(|\)$/g, '')]); | |
return val; | |
} | |
} | |
// Console functions just became a bit scary | |
var _console_toreplace = ['group', 'groupEnd', 'groupCollapsed', 'time', 'timeEnd', 'log', 'info', 'warn', 'error', 'debug', 'dir', 'trace']; | |
var _console = console; | |
var _console_queue = []; | |
var _console_queue_shift = _console_queue.shift.bind(_console_queue); | |
_console_toreplace.forEach(function (name) { | |
if (typeof _console[name] !== 'function') return; | |
// lol | |
var fn = _console[name].apply.bind(_console[name].bind(_console), _console); | |
_console[name] = function () { | |
var args = arguments; | |
_console_queue[_console_queue.length] = (function () { | |
fn(args); | |
}); | |
} | |
}); | |
setTimeout(function _() { | |
var num = _random() * _console_queue.length; | |
for (var i = 0; i < num; i++) { | |
_console_queue_shift()(); | |
} | |
setTimeout(_, _random() * 400 | 0) | |
}, _random() * 400 | 0); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment