Converting circular structure to JSON in window

TSR

I am trying to display all the elements of the window in the console. I tried with console.log(window) but it's not expanding automatically. This thread Is there a way to auto expand objects in Chrome Dev Tools? suggested I use JSON.stringify but I get the following error:

enter image description here

Michał Sałaciński

That's an unlikely thing to do, and I don't think that you'll be able to fully mimic everything you can see expanding things inside console - one of the reasons is a "circular structure" of window. Still, it can be done to some extent by cloning window object with some adjustments - this might be good starting point.

var maxDepth = 10;
function cloneObject(obj,depth) {
  if (!depth) depth = 1;
    var clone = {};
    for (var i in obj) {
        if (typeof(obj[i])=="object" && obj[i] != null) {
          try {
            if (obj[i].wowImCloned) clone[i] = '[I\'ve seen you somewhere..]';
            else if (depth >= maxDepth) clone[i]  = '[I\'m not going deeper]'
            else {
              obj[i].wowImCloned = true;
              clone[i] = cloneObject(obj[i],depth+1);
            }
          } catch(err) clone[i] = err.message;

        }
      else if (typeof(obj[i])=="function") clone[i]  = obj[i].toString()
      else clone[i] = obj[i];   
    }
    return clone;
}
var clone = cloneObject(window)
//console.log(JSON.stringify(clone))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting circular structure to JSON Firebase

Error: Converting circular structure to JSON

Converting circular structure to JSON at JSON.stringify ()

Sequelize: TypeError: Converting circular structure to JSON

GraphCool TypeError: Converting circular structure to JSON

"TypeError: Converting circular structure to JSON" with GET request

"TypeError: Converting circular structure to JSON" with GET request

Elliptic : TypeError : Converting circular structure to JSON

Axios TypeError: Converting circular structure to JSON

express & sequalize Converting circular structure to JSON

Angular - TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON in nodejs

Nodejs express Converting circular structure to JSON

avoiding TypeError: Converting circular structure to JSON

Converting circular structure to JSON, can't see anything circular

TypeError: Converting circular structure to JSON - find error in json

TypeError converting circular structure to json at JSON.stringify (<anonymous>)

Error Converting Circular Structure to JSON...take 2

MongoDB and Express: Type Error: Converting Circular structure to JSON

Reading fixture is throwing error TypeError: Converting circular structure to JSON

how to solve (Uncaught TypeError: Converting circular structure to JSON)

Moving code to an exported function results in TypeError: Converting circular structure to JSON

AWS Cognito - Encountering Error Converting Circular Structure to JSON

[Vue warn]: Error in render: "TypeError: Converting circular structure to JSON

Express Middleware: ERROR: TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON in retrieving html string from mongodb

Firestore references create a "TypeError: Converting circular structure to JSON"

Chrome sendrequest error: TypeError: Converting circular structure to JSON

TypeError: Converting circular structure to JSON property 'name' closes the circle