How to copy the objects from chrome console window?
Asked Answered
S

6

82

I have tried to copy the objects as text, but it show just [object object]. Before this I had tried with copy commend it was success but not now.Is that chrome issue?

What I tried? Just Right click on the object and store as global variable from chrome console window, then next just used copy(temp6) command and tried to paste in notepad++.


enter image description here

Singles answered 8/12, 2016 at 5:45 Comment(1)
FYI I'm also experiencing this issue. In my limited testing though, it seems that the issue is present when I try to copy an object with a method defined. If I remove the method, then issue is gone. I know that's not a solution, but it maybe helps others figure out what's going on.Hoatzin
R
71

It should ideally copy the object with the copy command that you wrote. I just tried it and worked for me.
Something else that you can try to do is to stringify that object and then copy it.
Ex.

copy(JSON.stringify(temp6))
Rigid answered 8/12, 2016 at 5:49 Comment(4)
It shows Uncaught TypeError: Converting circular structure to JSONSingles
@Jay How does that help? Is the user supposed to write a custom JSON function into their devTools every time they want to stringify an object?Noiseless
You can right mouse click an object and store it as a global variable. it will most likely be called "temp1", afterwards you can use the code snippet as follows: JSON.stringify(temp1). Hope this clarifies it a bit.Armorer
It shows the "circular structure" warning because your object has cycles, which makes it impossible to serialize to JSON. For example object A has property pointing to object B, and B has a property pointing back to A.Resolvent
C
53

If the object already logged

  • Right-click on the object in console and click Store as a global
  • variable the output will be something like temp1
  • Copy and paste below code in chrome console and hit enter

    (function(console){
        console.save = function(data, filename){
    
        if(!data) {
            console.error('Console.save: No data')
            return;
        }
    
        if(!filename) filename = 'console.json'
    
        if(typeof data === "object"){
            data = JSON.stringify(data, undefined, 4)
        }
    
        var blob = new Blob([data], {type: 'text/json'}),
            e    = document.createEvent('MouseEvents'),
            a    = document.createElement('a')
    
        a.download = filename
        a.href = window.URL.createObjectURL(blob)
        a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
        e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
        a.dispatchEvent(e)
     }
    })(console)
    
    • Then you can use the function for downloading,

console.save(temp1);

-If it shows Uncaught TypeError: Converting circular structure to JSON

then you need decycle JSON object and paste below code in chrome browser console and hit enter

if (typeof JSON.decycle !== "function") {
    JSON.decycle = function decycle(object, replacer) {
        "use strict";

        var objects = new WeakMap();     // object to path mappings

        return (function derez(value, path) {


            var old_path;  
            var nu;  

            if (replacer !== undefined) {
                value = replacer(value);
            }

            if (
                typeof value === "object" && value !== null &&
                !(value instanceof Boolean) &&
                !(value instanceof Date) &&
                !(value instanceof Number) &&
                !(value instanceof RegExp) &&
                !(value instanceof String)
            ) {


                old_path = objects.get(value);
                if (old_path !== undefined) {
                    return {$ref: old_path};
                }

                objects.set(value, path);

                if (Array.isArray(value)) {
                    nu = [];
                    value.forEach(function (element, i) {
                        nu[i] = derez(element, path + "[" + i + "]");
                    });
                } else {

                    nu = {};
                    Object.keys(value).forEach(function (name) {
                        nu[name] = derez(
                            value[name],
                            path + "[" + JSON.stringify(name) + "]"
                        );
                    });
                }
                return nu;
            }
            return value;
        }(object, "$"));
    };
}


if (typeof JSON.retrocycle !== "function") {
    JSON.retrocycle = function retrocycle($) {
        "use strict";

        var px = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\([\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*")\])*$/;

        (function rez(value) {



            if (value && typeof value === "object") {
                if (Array.isArray(value)) {
                    value.forEach(function (element, i) {
                        if (typeof element === "object" && element !== null) {
                            var path = element.$ref;
                            if (typeof path === "string" && px.test(path)) {
                                value[i] = eval(path);
                            } else {
                                rez(element);
                            }
                        }
                    });
                } else {
                    Object.keys(value).forEach(function (name) {
                        var item = value[name];
                        if (typeof item === "object" && item !== null) {
                            var path = item.$ref;
                            if (typeof path === "string" && px.test(path)) {
                                value[name] = eval(path);
                            } else {
                                rez(item);
                            }
                        }
                    });
                }
            }
        }($));
        return $;
    };
}
  • Then finally execute code for downloading.

console.save(JSON.decycle(temp1));

Carder answered 2/4, 2018 at 6:12 Comment(6)
actually, objects are not copying correctly, only the [0] is correct the rest are notIncompetent
It's worked for me, thanks so much. Oh, after all, instead of using save, you can use copy(JSON.decycle(temp1)) to save temp1 to clipboardBogor
Commenting to 'favourite' this answer for later due to lack of feature. meta.stackexchange.com/questions/2588/…Chimere
console.save(temp1); worked for me , i dint find the need to decycle it. Thanks for your snippet this saved timeUnreserved
Legend! :heart:Maxama
Thank you, was stuck on this for a while.Ithnan
E
10

You can use command in console as follows: Let say our object is:

  var object = {x:"xyz"}

Now use below command in console -

 copy(JSON.stringify(object))

object is now available to clipboard.You can now use Ctrl + v to use this object.

Eurasian answered 8/12, 2016 at 5:51 Comment(1)
It shows Uncaught TypeError: Converting circular structure to JSONSingles
E
5

You should check thecount object to avoid circular reference, before using copy(JSON.stringify(count)), please see here

Erysipelas answered 22/3, 2017 at 5:19 Comment(1)
@Noiseless DevTools has built-in JSON functionErysipelas
L
3

You can also do this without having to write any code. At least with later version of chrome.

When you right click the object you get this context: enter image description here

But if you left click the line to highlight it, the right click the console line you get this context menu: enter image description here

The "Save as..." option will create text file (*.log) of everything "as is" currently on the console log. So if you want to see more of the object simply expand it as far as you need.

collapsed example:

let tmpArr = []; tmpArr.push([]); tmpArr[0].push({ some: 'test'}); tmpArr[0].push({ some: 'next'}); console.log(tmpArr);
VM242:1 [Array(2)]0: (2) [{…}, {…}]length: 1[[Prototype]]: Array(0)
undefined
null
null

expanded example:

let tmpArr = []; tmpArr.push([]); tmpArr[0].push({ some: 'test'}); tmpArr[0].push({ some: 'next'}); console.log(tmpArr);
VM242:1 [Array(2)]0: Array(2)0: some: "test"[[Prototype]]: Object1: some: "next"[[Prototype]]: Objectlength: 2[[Prototype]]: Array(0)length: 1[[Prototype]]: Array(0)
undefined
null
null

Listerism answered 29/12, 2021 at 21:24 Comment(1)
This should be the accepted answer in 2021+ @Merbin Jo!Fade
D
2

there can be many ways to do this. One way could be to do JSON.stringify(yourObject) and then copy the output.

Duluth answered 8/12, 2016 at 5:49 Comment(2)
It shows Uncaught TypeError: Converting circular structure to JSONSingles
that explains why copy wasn't working in the first place. There is an explanation hereDuluth

© 2022 - 2024 — McMap. All rights reserved.