Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy the objects from chrome console window?

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

like image 591
Merbin Jo Avatar asked Dec 08 '16 05:12

Merbin Jo


People also ask

How do you copy things from the console?

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++.

How do I copy a global variable in Chrome console?

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. Chrome also has a copy() method, so copy(temp1) in the console should copy that object to your clipboard.

How do I capture an image from the console?

Press Cmd + P on Mac or Ctrl + P on Windows. Type > screen . You will get 3 relevant suggestions: Mobile Capture full size screenshot: Captures the whole page, including the non-visible (out of viewport) area.


2 Answers

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)) 
like image 67
Abhinav Singi Avatar answered Oct 07 '22 02:10

Abhinav Singi


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));

like image 25
Rakesh Chaudhari Avatar answered Oct 07 '22 02:10

Rakesh Chaudhari