7

I need to examine the entire window object, in order to check where data is actually being saved.

If i execute window.toString() i get "[object Window]" I have also tried with the JSON.stringify(window) function and got the following error:

VM18766:1 Uncaught TypeError: Converting circular structure to JSON

Is there any way i can get the entire javascript object content including prototype functions?

I need this in order so that i can search inside the object text for specific content being saved in an object and where this object is.

Aryeh Armon
  • 1,971
  • 1
  • 18
  • 35

4 Answers4

1

if you try to do JSON.stringify(window), you will get the following error:

Uncaught TypeError: Converting circular structure to JSON

From this this StackOverflow post, you can do the following:

const p = document.getElementById('p');

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

p.innerHTML = JSON.stringify(window, getCircularReplacer());
<html>
<head>
</head>
<body>
<p id="p"></p>
<script src="app.js"></script>
</body>
</html>
Anthony
  • 782
  • 1
  • 9
  • 20
0

The DOM is impossible to serialize in JSON because, as the error says, it contains circular references. A simple example of this is where every element has a .children array and each child has a .parent value. This seems to be an XY problem to me. Take a look at this question which might help with what you are actually trying to do.

Bardi Harborow
  • 1,683
  • 1
  • 27
  • 40
-1

You can use a 3rd party library like this and see if it outputs what you want. Usage:

<script src="circular-json.js"></script>

And (e.g. in your onload function)

var CircularJSON = window.CircularJSON;
var obj = window;
var str;

obj.self = obj;
console.log(CircularJSON.stringify(obj));

You could also just use

console.log(window);

to inspect the object and search through the object in the browser console.

oberbics
  • 378
  • 3
  • 11
  • i am getting an error: VM10998:2 Uncaught DOMException: Blocked a frame with origin "" from accessing a cross-origin frame.(…) – Aryeh Armon Jun 08 '16 at 09:04
  • To track that I would need to see your entire page. But it seems that you have somewhere iFrames and access to the iFrame is blocked (see [this answer](http://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame)) – oberbics Jun 08 '16 at 09:09
  • is there a way to search the entire window object for a specific property – Aryeh Armon Jun 08 '16 at 09:17
  • [Maybe that helps](http://stackoverflow.com/questions/5443436/how-do-i-recursively-search-an-object-tree-and-return-the-matching-object-based) – oberbics Jun 08 '16 at 09:20
-1

This is not possible since the window object is a circular object

For example the object passed in the request has a circular reference, something like:

var a = {};
a.b = a;
Aryeh Armon
  • 1,971
  • 1
  • 18
  • 35