js 复制/转换 window对象的全部属性内容 为字符串
控制台copy(window)不行,只得到[object Window]
copy(object)
copies a string representation至于具体是怎么实现的嘛,猜测类似于.toString() of the specified object to the clipboard.(参考https://developer.chrome.com/docs/devtools/console/utilities/#copy-function)
copy(JSON.stringify(window))也不行,报错Uncaught TypeError: Converting circular structure to JSON
需要定义一个处理循环结构的函数,可以参考https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format
我是用的这个
const circularReference = {otherData: 123};
circularReference.myself = circularReference;
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
const stringified = JSON.stringify(circularReference, getCircularReplacer());
console.log(stringified);
不过复制出来有一些长的转义格式化有问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构