Copy Javascript Object from Safari Browser Console

Copy Javascript Object from Safari Browser Console




How to Share Console Logs from Safari as JSON

There are times when we need to test something specifically on Safari and share console logs with other developers. However, Safari often renders these logs as objects that can’t be copied directly as text JSON strings. I’ve found a workaround for this and wanted to share it.



Logging Objects in Safari

When you log an object in Safari, you might see something like this:

Right-clicking on the object gives you a few options. Choose “Log Value” to re-log the output in the console.

Safari Log Value Option



Getting the Variable Name

This does two things:

  1. It prints a variable name, which appears just above the newly printed log.
  2. It prints the actual object again.

If the variable name isn’t something like $1 or $2, try clicking “Log Value” again on the newly printed object. Now you should see a variable name with a $.

Variable Name in Safari Console



Copying the Object

Use the copy(<variable_name>) method to copy this object to the clipboard.

copy($2)
Enter fullscreen mode

Exit fullscreen mode

Copy Command in Safari Console

Note: Once the copy() command is executed, it will print undefined—this is just the return value of the copy() method.



Pasting the Copied Object

Now, open any text-editable input field or editor and paste the object you just copied.

Pasting the Copied Object

And voilà! You’ve successfully copied and pasted your Safari console log as a JSON string.

Happy coding!



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.