Restore console functions in JS

December 28th, 2016 | Permalink

Naughty boys and girls sometimes replace browser’s console object from window object to eliminate debug messages from scripts (instead of just redefining it inside closure). There IS a way to restore it. I do it like this:

(function(ifr){
  document.body.appendChild(ifr);
  window.console = ifr.contentWindow.console;
  ifr.parentNode.removeChild(ifr);
})(document.createElement('iframe'));

‘course, this should be done after the DOM is ready, I guess. Most cases I use it I’m fiddling in browser’s console and have no access to page source anyway.

Also I was lazy to hard-code the latin alphabet and did this:

Array.apply(0,Array(26)).map(function(a,i){return String.fromCharCode(i+97);});
// ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

And yea, I was bored.