2

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

Community
  • 1
  • 1
TSR
  • 17,242
  • 27
  • 93
  • 197

1 Answers1

1

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))
Michał Sałaciński
  • 2,256
  • 1
  • 11
  • 10