Javascript / Chrome - How to copy an object from the webkit inspector as code

后端 未结 11 1744
天涯浪人
天涯浪人 2020-11-29 14:47

I am doing a console.log statement in my javascript in order to log a javascript object. I\'m wondering if there\'s a way, once that\'s done - to copy that object as javascr

相关标签:
11条回答
  • 2020-11-29 14:48

    So,. I had this issue,. except I got [object object]

    I'm sure you could do this with recursion but this worked for me:

    Here is what I did in my console:

    var object_that_is_not_shallow = $("all_obects_with_this_class_name");
    var str = '';
    object_that_is_not_shallow.map(function(_,e){
        str += $(e).html();
    });
    copy(str);
    

    Then paste into your editor.

    0 讨论(0)
  • 2020-11-29 14:49

    Try JSON.stringify(). Copy the resulting string. Does not work with objects containing circular references.

    0 讨论(0)
  • 2020-11-29 14:59
    1. Right-click an object in Chrome's console and select Store as Global Variable from the context menu. It will return something like temp1 as the variable name.

    2. Chrome also has a copy() method, so copy(temp1) in the console should copy that object to your clipboard.

    Note on Recursive Objects: If you're trying to copy a recursive object, you will get [object Object]. The way out is to copy(JSON.stringify(temp1)) , the object will be fully copied to your clipboard as a valid JSON, so you'd be able to format it as you wish, using one of many resources.

    0 讨论(0)
  • 2020-11-29 15:02

    If you've sent the object over a request you can copy it from the Chrome -> Network tab.

    Request Payload - > View Source

    enter image description here

    enter image description here

    0 讨论(0)
  • 2020-11-29 15:03

    You can copy an object to your clip board using copy(JSON.stringify(Object_Name)); in the console.

    Eg:- Copy & Paste the below code in your console and press ENTER. Now, try to paste(CTRL+V for Windows or CMD+V for mac) it some where else and you will get {"name":"Daniel","age":25}

    var profile = {
        name: "Daniel",
        age: 25
    };
    
    copy(JSON.stringify(profile));
    
    0 讨论(0)
  • 2020-11-29 15:06

    Add this to your console and execute

    copy(JSON.stringify(foo));
    

    This copies your JSON to clipboard

    0 讨论(0)
提交回复
热议问题