本文翻译自:Javascript / Chrome - How to copy an object from the webkit inspector as code
I am doing a console.log statement in my javascript in order to log a javascript object. 我在我的javascript中做一个console.log语句,以便记录一个javascript对象。 I'm wondering if there's a way, once that's done - to copy that object as javascript code. 我想知道是否有办法,一旦完成 - 将该对象复制为javascript代码。 What I'm trying to do is convert an object that was created using ajax to parse an xml feed into a static javascript object so that a file can run locally, without a server. 我要做的是转换使用ajax创建的对象将xml提要解析为静态javascript对象,以便文件可以在本地运行,而无需服务器。 I've included a screenshot of the object in the chrome inspector window so you can see what I'm trying to do. 我在chrome检查器窗口中包含了对象的屏幕截图,以便您可以看到我正在尝试做的事情。
#1楼
参考:https://stackoom.com/question/hEtZ/Javascript-Chrome-如何从webkit检查器复制对象作为代码
#2楼
Try JSON.stringify()
. 试试JSON.stringify()
。 Copy the resulting string. 复制结果字符串。 Does not work with objects containing circular references. 不适用于包含循环引用的对象。
#3楼
You can copy an object to your clip board using copy(JSON.stringify(Object_Name)); 您可以使用copy(JSON.stringify(Object_Name))将对象复制到剪贴板; in the console. 在控制台中。
Eg:- Copy & Paste the below code in your console and press ENTER. 例如: - 在控制台中复制并粘贴以下代码,然后按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} 现在,尝试粘贴(Windows的CTRL + V或Mac的CMD + V)以及其他地方,您将获得{“name”:“Daniel”,“age”:25}
var profile = {
name: "Daniel",
age: 25
};
copy(JSON.stringify(profile));
#4楼
You can now accomplish this in Chrome by right clicking on the object and selecting "Store as Global Variable": http://www.youtube.com/watch?v=qALFiTlVWdg 您现在可以通过右键单击对象并选择“存储为全局变量”在Chrome中完成此操作: http : //www.youtube.com/watch?v = qALFiTlVWdg
#5楼
Right-click an object in Chrome's console and select
Store as Global Variable
from the context menu. 右键单击Chrome控制台中的对象,然后从上下文菜单中选择“Store as Global Variable
”。 It will return something liketemp1
as the variable name. 它将返回类似temp1
的变量名称。Chrome also has a
copy()
method, socopy(temp1)
in the console should copy that object to your clipboard. Chrome还有一个copy()
方法,因此控制台中的copy(temp1)
应该将该对象复制到剪贴板。
Note on Recursive Objects: If you're trying to copy a recursive object, you will get [object Object]
. 关于递归对象的注意事项:如果您正在尝试复制递归对象,您将获得[object Object]
。 This is to be expected. 这是可以预料的。
#6楼
If you've sent the object over a request you can copy it from the Chrome -> Network tab. 如果您通过请求发送了对象,则可以从Chrome - >网络标签中复制该对象。
Request Payload - > View Source 请求有效负载 - >查看源