0

Suddenly, console.log() stoped working (tested in several browsers) as from a .js file and directly from dev tools console at the specific website (preview link): enter image description here

And works at the rest websites:

enter image description here

What can influece or block console.logs ?

Max
  • 33
  • 7
  • 1
    you can redefine the prototype of any object and even attach setter/getter to property.. for example: https://stackoverflow.com/questions/7042611/override-console-log-for-production – Diego D Jul 27 '22 at 11:39
  • So it there any way to reassign console.log back to its initial logic ? And how I can make this script have more priority than the script which redefined console.log ? – Max Jul 27 '22 at 11:50
  • You could store `window.alert` in a variable on the start of page, and restore it on end of page (or every 5 seconds) – IT goldman Jul 27 '22 at 11:55
  • 1
    as far as I know there's no way to restore the previous state unless you just reload the object from scratch but in the case of console, and any other object I knew in advance was going to be screwed, I would load a js as the very first thing (with no defer and put on top of the page) that will save the original prototype so that would be easier to restore on demand... but that's far from being a clear solution. Differently as it was suggested above.. if you save the reference of an object that will be changed, won't save you. You need a deep clone instead – Diego D Jul 27 '22 at 11:56
  • to make the last statement more clear: you can easily discard the window.alert hint that was totally out of topic – Diego D Jul 27 '22 at 11:59
  • 2
    anyway to keep it simple you could really do something like `const original = console.log;` at the very beginning and then in any moment after the function was screwed, just riassign it to `console.log = original;` or just `original('logthiswiththenewfunctionname')` – Diego D Jul 27 '22 at 12:05
  • Thanks @Diego De Vita! This really works as a nice workaround! But I'm still trying to find the script which brokes `console.log` logic – Max Jul 27 '22 at 12:08
  • the link you shared hosts a page that loads forever doing only god knows what. The console.log keeps working even after a while.. I waited max 30 seconds. You are not going to find the answer you are looking for here – Diego D Jul 27 '22 at 12:15
  • 1
    @DiegoDeVita [This approach](https://stackoverflow.com/q/8580431/1048572) (also [here](https://stackoverflow.com/q/29884379/1048572) and [there](https://stackoverflow.com/q/13990187/1048572)) should work for overwritten `console` methods as well. – Bergi Jul 27 '22 at 13:31
  • @Max "*I'm still trying to find the script which broke `console.log`*" - just break it first. `[console, Object.getPrototypeOf(console)].forEach(o => { const original = o.log; Object.defineProperty(o, 'log', {get() { return original; }, set() { throw new Error("Don't mess with the console, idiot!"); }}); });` or just `Object.freeze(console); Object.freeze(Object.getPrototypeOf(console));` might do it. – Bergi Jul 27 '22 at 13:35

0 Answers0