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


不过复制出来有一些长的转义格式化有问题

posted @   hrdom  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示