Javascript/Chrome - 如何从 webkit 检查器复制对象作为代码 [英] Javascript / Chrome - How to copy an object from the webkit inspector as code
问题描述
我正在我的 javascript 中执行 console.log 语句以记录一个 javascript 对象.我想知道是否有办法,一旦完成 - 将该对象复制为 javascript 代码.我想要做的是将使用 ajax 创建的对象转换为静态 javascript 对象,以便文件可以在没有服务器的情况下在本地运行.我在 chrome 检查器窗口中包含了对象的屏幕截图,因此您可以看到我正在尝试做什么.
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 javascript code. 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. I've included a screenshot of the object in the chrome inspector window so you can see what I'm trying to do.
推荐答案
右键单击 Chrome 控制台中的对象,然后从上下文菜单中选择
Store as Global Variable
.它将返回类似于temp1
的内容作为变量名.
Right-click an object in Chrome's console and select
Store as Global Variable
from the context menu. It will return something liketemp1
as the variable name.
Chrome 也有一个 copy()
方法,因此控制台中的 copy(temp1)
应该将该对象复制到剪贴板.
Chrome also has a copy()
method, so copy(temp1)
in the console should copy that object to your clipboard.
关于递归对象的注意事项:如果您尝试复制递归对象,您将获得 [object Object]
.出路是尝试 copy(JSON.stringify(temp1))
,该对象将作为有效的 JSON 完全复制到剪贴板,因此您可以根据需要对其进行格式化,使用众多资源之一.
Note on Recursive Objects: If you're trying to copy a recursive object, you will get [object Object]
. The way out is to try 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.
如果您收到 Uncaught TypeError: Converting circle structure to JSON
消息,您可以使用 JSON.stringify
的第二个参数(这是一个过滤器函数)来过滤找出有问题的循环属性.有关详细信息,请参阅此堆栈溢出答案.
If you get the Uncaught TypeError: Converting circular structure to JSON
message, you can use JSON.stringify
's second argument (which is a filter function) to filter out the offending circular properties. See this Stack Overflow answer for more details.
这篇关于Javascript/Chrome - 如何从 webkit 检查器复制对象作为代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!