Javascript / Chrome - How to copy an object from the webkit inspector as code
β‘TLDR
To clone an object from Chrome's DevTools, use the copy()
function:
- Press F12 to open your DevTools.
- Go to the Console.
- Run the command
copy(myObject)
. - Now, paste this snippet wherever you need.
Example:
Taming circular references
If you've got circular references (an object referring to itself), JSON.stringify()
might let you down. Not to worry, a custom replacer function can help you avoid these recursive nightmares.
Example:
The global variable trick
For quick access, park your object as a global variable in Chrome's DevTools:
- Find your object in the Elements or Console panel.
- Right-click and opt for "Store as global variable".
- Access the object with the automatically assigned name (like
temp1
).
Network tab: AJAX & API responses
To copy AJAX-generated data or API responses, follow these steps:
- Open DevTools (F12) and switch to the Network tab.
- Perform the action that triggers the request.
- Click the request and find the Response or Preview tab.
- Right-click and select "Copy" β "Copy response".
Example:
Chrome 89+: The "Copy Object" command
If you're on Chrome 89 or later, you can use the "Copy Object" command to directly copy an object formatted as JS code.
Paste the data
To deploy the copied object, CTRL+V (Windows) or CMD+V (Mac) is all it takes. Paste it into your Code Editor or another Console.
ξ’Linked
ξ’Linked
Was this article helpful?