Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put a value in webkit console from console.log into a variable

If there is an output in the chrome/safari webkit inspector containing an object that prints out such as:

enter image description here

Only much more complicated with loads of nested objects (which is why a copy/paste wont do)

Is there a way to put this in a variable to inspect in further and process it after its been printed on the console (its printed via console.log), just only after its already in the console?

like image 531
Tarang Avatar asked Oct 31 '25 08:10

Tarang


2 Answers

$_ will give you last output of console. So in console you can assign in to a variable.

enter image description here

Note that you can do this only in console and not from your own code.

like image 105
Mohsen Avatar answered Nov 01 '25 21:11

Mohsen


Here's a way to do it without wrapping console.log in a custom log function:

var justLogged;
var oldLog = console.log;

console.log = function () {
    oldLog.apply(console, arguments);
    justLogged = arguments;
};

console.log('test');

// if necessary, restore console.log to its original behavior when you're finished with it
console.log = oldLog;

The value of justLogged will be ['test'], since you just logged it.

like image 29
Emmett Avatar answered Nov 01 '25 22:11

Emmett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!