Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy watched javascript variable in Chrome?

I put a breakpoint in javascript and am testing some code with Chrome. I also added a watch expression for the value.

Chrome breaks at the breakpoint and shows the value. However the value is very long, and it doesn't display it all. I move the window separator to the left but it stops at mid screen. When I double click on the watched variable it wants to edit the expression. When I single click and drag on it, it selects the visible text, but not all. Right clicking does nothing.

This is what I see

url: "m=mtgoxUSD&SubmitButton=Draw&r=&i=&c=0&s=&e=&Prev=&Next=&t=S&b=&a1=&m1=10&a2=&m2=25&x=0... 

I want to copy the whole expression without the ... in the end. How can I do that?

like image 530
siamii Avatar asked Mar 22 '12 03:03

siamii


People also ask

How do I view Javascript variables in Chrome?

To view any variable in chrome, go to "Sources", and then "Watch" and add it. If you add the "window" variable here then you can expand it and explore.


2 Answers

I'm adding a late answer after nearly 3 years because with the current Chrome Dev Tools, neither approach work if you have an Array or even just a nested Object property in that variable, following both answers you'll just end up copying a string with a lot of Array[size] or Object strings interleaved in the actual object value, completely useless for complex object hierarchies.

The suggested approaches are ok if you just need to manually navigate through the value but not if you need to copy it as requested in the question.

What i recommend instead, especially if you need to copy the watched value to use it as the content of a new variable, is to dump it to console after it has been stringified.

Show the Javascript console and type:

console.log(JSON.stringify(my_watched_var)) 

This way the complete structure will be displayed in pure Javascript, a fully reusable/copyable way.

like image 164
uraimo Avatar answered Sep 30 '22 01:09

uraimo


Chrome DevTools' console command-line has a built-in "copy" function:

copy(my_variable) 

If the value of my_variable is not a string, it will automatically be converted to JSON. The resulting string is left on the system clipboard for pasting.

Here's the reference doc.

like image 36
emackey Avatar answered Sep 30 '22 03:09

emackey