42

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?

siamii
  • 23,374
  • 28
  • 93
  • 143
  • This link has amazing descripion: https://scottwhittaker.net/chrome-devtools/2016/02/29/chrome-devtools-copy-object.html Steps: 1) Right click variable and select "Add as Global Variable" 2) In the console, write copy(temp1) 3) Open any editor and paste – Soham Mehta Oct 22 '19 at 02:48

6 Answers6

50

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.

uraimo
  • 19,081
  • 8
  • 48
  • 55
  • 11
    I think you could further improve this by using `copy()` instead of `console.log()`, which copies a string straight to your clipboard. – hdgarrood May 17 '15 at 11:51
  • Nice!, would be even better if it could format the copied pieces at little to add line break between each properties of the variable. I was able to format it using notepad ++. Replace all "," with ",\r\n". – AXMIM Nov 03 '15 at 20:36
  • 2
    `copy()` also worked for me in Firefox. :) – cxw Jun 07 '16 at 15:37
  • @AXMIM you can do this using `stringify`, e.g. `JSON.stringify(my_watched_var, null, 2)` – morloch Oct 03 '16 at 21:16
  • this works! I find it very upsetting that you cannot get a JSON object as JSON from a pure JavaScript language/debugger without calling JSON.stringify in the console, but – Chaim Eliyah Mar 08 '18 at 00:11
38

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.

emackey
  • 11,818
  • 2
  • 38
  • 58
27

Show the console, then type the expression to be displayed and press . You'll see whole value and you'll be able to select and copy it.

While the debugger is paused, this works even with expressions that involve local variables that are in scope at the current point of execution.

Timwi
  • 65,159
  • 33
  • 165
  • 230
Łukasz Nojek
  • 1,511
  • 11
  • 17
13

Here's what I do.

Edit: This works in the Scope Variables which is below the Watch Expressions panel.

  1. Double click on the value to switch to edit mode.

  2. Ctrl+A (windows) or Cmd+A (mac) to select the entire value.

  3. Ctrl+C (or Cmd+C) to copy.

Ben
  • 20,737
  • 12
  • 71
  • 115
  • 2
    Double clicking edits the watched expression variable, i.e. url, not the value – siamii Mar 22 '12 at 03:57
  • 3
    aaaah, sorry, mis-read your post.. I do that in the Scope Variables section. Works there.. – Ben Mar 22 '12 at 03:59
  • 1
    Thanks, that's good enough, it works in the scope variables section – siamii Mar 22 '12 at 04:01
  • 3
    I think the correct answer is the one that @L.N. gave, because in the Scope Variables you can only copy the values of the variables, so if you have to copy values of expressions you must use the console as he said. – victorvartan Sep 14 '12 at 14:02
  • This link has amazing descripion: https://scottwhittaker.net/chrome-devtools/2016/02/29/chrome-devtools-copy-object.html Steps: 1) Right click variable and select "Add as Global Variable" 2) In the console, write copy(temp1) 3) Open any editor and paste – Soham Mehta Oct 22 '19 at 02:48
1

Look this answer "Is there a way to auto expand objects in Chrome Dev Tools?" , it iterates trough all the properties of an object and it shows the full hierarchy including datatype and values.

Useful if you need to compare two states of an application.

Community
  • 1
  • 1
Matteo Conta
  • 1,423
  • 13
  • 17
1

This link has amazing descripion: https://scottwhittaker.net/chrome-devtools/2016/02/29/chrome-devtools-copy-object.html

Steps: 1) Right click variable and select "Add as Global Variable" 2) In the console, write copy(temp1) 3) Open any editor and paste

Soham Mehta
  • 576
  • 5
  • 4