如何从铬控制台窗口复制对象?

我尝试将对象复制为文本,但它只显示[ object object ]。在此之前,我已经尝试与 收到赞扬它是成功的,但不是现在。是铬的问题?

我试过什么? 只需右键单击对象,并从 chrome 控制台窗口存储为全局变量,然后下一步只需使用 copy (tem6)命令,并尝试粘贴在记事本 + + 。


enter image description here

56347 次浏览

理想情况下,它应该使用您编写的 copy命令复制对象。 我只是试着为自己工作。
您可以尝试做的其他事情是将该对象字符串化,然后复制它。
例子

copy(JSON.stringify(temp6))

有很多方法可以做到这一点。一种方法可以是做 JSON.stringify(yourObject),然后复制输出。

您可以在控制台中使用以下命令: 假设我们的目标是:

  var object = {x:"xyz"}

现在在控制台中使用以下命令-

 copy(JSON.stringify(object))

对象现在可用于剪贴板。现在可以使用 Ctrl + v 来使用该对象。

您应该检查 count对象以避免循环引用,在使用 copy(JSON.stringify(count))之前,请参阅 给你

如果对象已经记录

  • 右键单击控制台中的对象,然后单击 Store as a global
  • 变量的输出类似于 tem1
  • 复制和粘贴下面的代码在铬控制台,并点击进入

    (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)
    
    • 然后你就可以使用这个函数来下载,

Save (temp1) ;

- 如果它显示 未捕获的 TypeError: 将循环结构转换为 JSON

然后你需要十周期 JSON 对象和粘贴下面的代码在铬浏览器控制台,并点击进入

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 $;
};
}
  • 最后执行下载代码。

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

您也可以这样做,而不必编写任何代码。至少在以后的版本的铬。

当你右键点击对象时,你会看到这样的上下文: enter image description here

但是如果你左键单击该行来突出显示它,右键单击控制台行你会看到这个上下文菜单: enter image description here

“另存为...”选项将创建文本文件(* 。(log)当前控制台日志中的所有内容。因此,如果你想看到更多的对象,只需将它展开到你需要的程度。

折叠的例子:

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

详细例子:

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