1

UPDATED: The reason the console won't log is the site runs this script:

    // Production mode
    // Disable console.log
    console.log = function() {};

I'm guessing there isn't a way to get the function back on the site?

Prior issue:

I'm running a script in my Chrome console that follows everyone on a site. There are 200 people on the page, and I want to iterate through all the follow buttons and click them all by running this script in the console:

javascript:var inputs = document.getElementsByClassName('btn-follow'); 
for(var i=0; i<inputs.length; i++) { 
inputs[i].click();
}
console.log("The btn-follow script has finished");

The script clicks the buttons fine but the log statement returns undefined.

Is there a way to log to the console from within the console?

When I try console.log directly it looks like this:

enter image description here

Here is what gets outputted when I type "console":

enter image description here

YPCrumble
  • 26,610
  • 23
  • 107
  • 172
  • ummmm... what? What's wrong with `console.log`? I'm not sure this makes much sense – tckmn Dec 08 '13 at 23:40
  • Just type "The btn-follow script has finished" – Yuriy Galanter Dec 08 '13 at 23:42
  • 1
    That *will* log to the console. After logging to the console, the console will display the return value (`undefined`). That doesn't prevent you looking at the previous line. – Quentin Dec 08 '13 at 23:42
  • Just added an image - my example is simple but I wanted to use it to debug a much longer script, and if it doesn't log the outputs it's not much help with that. – YPCrumble Dec 08 '13 at 23:50
  • @Quentin see above - the issue was this site disables the console.log function. – YPCrumble Dec 09 '13 at 00:17

3 Answers3

10

You can eliminate shadowed properties with delete:

delete console.log;

The original log function (which exists on console's prototype) was never overwritten -- it has merely been shadowed by a property on the console instance. You can delete the instance function and let the original prototype function shine through.

apsillers
  • 112,806
  • 17
  • 235
  • 239
  • Ha, didn't think about that. Nice! :) – Felix Kling Dec 09 '13 at 00:47
  • 1
    Just for your information (this answer is Chrome oriented, so it is still valid):I've tried in in Firefox 40.0.3 and this solution does not work. The object `console`in Firefox is an Object with its own methods, there isn't a Console prototype to fallback if log is not defined – Pablo Lozano Oct 13 '15 at 12:19
  • 2
    does not work anymore in chrome either. –  Apr 26 '18 at 17:20
2

I'm guessing there isn't a way to get the function back on the site?

Since you are using Chrome and can use Object.getPrototypeOf, you can do:

var original_log = Object.getPrototypeOf(console).log;

The log method is defined on the prototype.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Unfortunately the prototype of console is showing up as an empty object on the latest Chrome. – fny Jan 08 '17 at 08:14
1

This works for me:

screenshot

The log statement I submit (in blue) is followed by it's effect (the message, in black) and the return type of the log expression (in grey).

EDIT

Verify that you haven't replaced the console somehow. You can try the following:

screenshot

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742