Go to Twitter's login page and type the following in the console:
window.addEventListener('keypress', function(e){console.log('hello')}, true)
(NOTE: how the third parameter is set to true
which enables event capture. This causes events to be intercepted first by the window before being consumed by a child element.)
Try pressing some keys. Notice how hello
isn't output to the console. Adding an event listener for keydown
or keyup
doesn't change anything.
hello
will get output on most websites, but not on sites like Twitter or Gmail.
Why? What's stopping the event listener?
EDIT: Seems to work as expected on Firefox. But not Chrome. Why isn't Chrome firing the event listener as expected?
EDIT 2: As deduced by a few people below, console.log
is an empty function on Chrome for sites like Twitter and Gmail. Why is that?
To restore the console in new tab go to console and type delete window. console it will return true after that restart the chrome and you are good to go.
Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console. On MacOS you can use Command + K button.
The reason for that is that the console prints the value that is returned by the last statement. console. log(...) returns undefined (well, returns nothing, void, for that matter), so you get undefined. If the last statement returns something else (or has a value), that would be displayed.
Console Logs in Chrome: In Google Chrome, the Console Logs are available as a part of Chrome Dev Tools. To open the dedicated Console panel, either: Press Ctrl + Shift + J (Windows / Linux) or Cmd + Opt + J (Mac).
Because those sites have specifically set (for webkit apparently):
console.log = function(){};
However, in Chrome you can restore the original log()
functionality by issuing this command in console:
console.log = console.__proto__.log
Then you can do this:
window.addEventListener('keypress', function(e){console.log('hello')}, true)
And it should work as expected.
Get it from an iframe:
function setConsole() {
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
console = iframe.contentWindow.console;
window.console = console;
}
(and then call it)
setConsole()
source: https://gist.github.com/cowlicks/a3bc662b38c36483b35f74b2b54e37c0
Developers like to put console.log()
statements in their code for troubleshooting/debugging. Sometimes, they forget to take them all out when checking in production code. A simple safeguard against this is to redefine console.log()
in production code to be an empty function that does nothing so even if a developer accidentally leaves one in, it won't cause any output or throw an exception when there is no console
object when not running the debugger (like in IE).
Some browsers may protect against the replacing of that function by making it read-only such that it can't be overwritten in this way, but if they do that, then the console object must exist so there's still no harm from an occasional console.log()
statement. The main harm of these leftover console.log()
statements is in IE where it causes an exception to be thrown because the console
object doesn't exist unless the debugger is being run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With