10

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
  • 2,137
  • 2
  • 22
  • 37

5 Answers5

2

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
  • 931
  • 1
  • 13
  • 30
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,803
  • 1
  • 28
  • 41
0

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
  • 408
  • 4
  • 12
  • 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
0

You can use a try...catch block and JSON.stringify to filter out the properties that can't be properly converted to JSON. This will capture valid objects, arrays, strings, and numbers. It will exclude functions so if you want those you'll need convert them to strings separately. For example:

function stringifyWindow() {
    let result = {}
    Object.getOwnPropertyNames(window).forEach(key => {
        const value = window[key];
        switch (typeof window[key]) {
           case 'undefined':
               result[key] = 'undefined';
               break;
           case 'function': 
               result[key] = value.toString();
               break;
           default:
               try {
                   result[key] = JSON.parse(JSON.stringify(value));
               } catch {}
               break;
        }
    });
    return JSON.stringify(result);
}
-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
  • 2,137
  • 2
  • 22
  • 37