简体   繁体   中英

Why doesn't "console.log()" work on this website?

I can open the Chrome DevTools console for this URL: https://www.google.com/ and enter the following command to print "Hello world:" to the console:

console.log("Hello world!")

However, when I attempt to use the same command for this URL: https://svc.mt.gov/dor/property/prc my message isn't printed in the console. Why is that?

Is there any way to force the console to work for this MT website?

I've tried using python/selenium to open the page and execute_script() to issue the command, but that hasn't worked either.

If you do

console.dir(console.log)

and click on the [[FunctionLocation]] , you'll see the following in the site's source code:

"Debug" != e && (window.console.log = function() {}
    )

Seems like a cheap (but lazy?) way to suppress logging on production.

One way to fix it, kinda, would be to use one of the many other console commands . ( console.dir , or console.error , if you want)

If you really want the original console.log to work again, the only way to do that would be to run code before the page code runs, so that you can save a reference to the function. For example, with a userscript:

// <metadata block...>
// @grant none
// ==/UserScript==

const origConsoleLog = console.log;
// set original window.console.log back to its original
// after the page loads
setTimeout(() => {
  window.console.log = origConsoleLog;
}, 2000);

// there are more elegant but more complicated approaches too

CertainPerformance already explained how this is possible and what the reason for suppressing the log seems to be. I'm going to show here another method to get back console.log after the original has been lost.

document.body.appendChild(document.createElement('IFRAME')).onload = function () {
    this.contentWindow.console.log('Hello, World!');
    this.remove();
};

This basically uses an old method of restoring global objects from a temporary iframe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM