Converting the JavaScript Window Object to JSON -


obviously, seem @ first cannot done, state have converted javascript window object json. need how in more intelligent way doing now.

the process works this:

1) new object created , each key window key copied new object. list of commonly known keys not added new object (ex. websocket). 2) function called getarray recursively cleans each sub-object , saves json. result looks {'key':'json_{....}'} or {'key':'json_{{'key':'json_{....}'}}'}. idea catch error @ smallest level final json object composed of key:json_string values. other critical point catch circular objects. if object has of long list of keys, excluded (ex. tagname).

the problem code bit unstable. ceased working on our site , don't have mind improve it.

i accept solution converts window object json (or text format can decoded properly). suggest partial solution, function detect circular , non-json-encodable objects 100%, best, if possible. take genius produce, believe possible.

the purpose of user-sent bug reports show state of our javascript. there many objects enumerate 1, 1, , our user-created objects contain un-encodable objects. useful , relevant web.

here current code, design pattern described. getwindow()

getwindow: function()     {         var obj2 = {};         (var in window)         {             obj2[i] = 1;         }         var keys = ["requestlogs", "websocket", "clicklogs", "$", "jquery", "window", "top", "location", "external", "chrome", "document", "i", "arr", "webkitnotifications", "localstorage", "sessionstorage", "applicationcache", "webkitstorageinfo", "indexeddb", "webkitindexeddb", "crypto", "css", "performance", "console", "devicepixelratio", "stylemedia", "parent", "opener", "frames", "self", "defaultstatus", "defaultstatus", "status", "name", "length", "closed", "pageyoffset", "pagexoffset", "scrolly", "scrollx", "screentop", "screenleft", "screeny", "screenx", "innerwidth", "innerheight", "outerwidth", "outerheight", "offscreenbuffering", "frameelement", "clientinformation", "navigator", "toolbar", "statusbar", "scrollbars", "personalbar", "menubar", "locationbar", "history", "screen", "postmessage", "close", "blur", "focus", "ondeviceorientation", "ondevicemotion", "onunload", "onstorage", "onresize", "onpopstate", "onpageshow", "onpagehide", "ononline", "onoffline", "onmessage", "onhashchange", "onbeforeunload", "onwaiting", "onvolumechange", "ontimeupdate", "onsuspend", "onsubmit", "onstalled", "onshow", "onselect", "onseeking", "onseeked", "onscroll", "onreset", "onratechange", "onprogress", "onplaying", "onplay", "onpause", "onmousewheel", "onmouseup", "onmouseover", "onmouseout", "onmousemove", "onmouseleave", "onmouseenter", "onmousedown", "onloadstart", "onloadedmetadata", "onloadeddata", "onload", "onkeyup", "onkeypress", "onkeydown", "oninvalid", "oninput", "onfocus", "onerror", "onended", "onemptied", "ondurationchange", "ondrop", "ondragstart", "ondragover", "ondragleave", "ondragenter", "ondragend", "ondrag", "ondblclick", "oncuechange", "oncontextmenu", "onclose", "onclick", "onchange", "oncanplaythrough", "oncanplay", "oncancel", "onblur", "onabort", "onwheel", "onwebkittransitionend", "onwebkitanimationstart", "onwebkitanimationiteration", "onwebkitanimationend", "ontransitionend", "onsearch", "getselection", "print", "stop", "open", "showmodaldialog", "alert", "confirm", "prompt", "find", "scrollby", "scrollto", "scroll", "moveby", "moveto", "resizeby", "resizeto", "matchmedia", "requestanimationframe", "cancelanimationframe", "webkitrequestanimationframe", "webkitcancelanimationframe", "webkitcancelrequestanimationframe", "captureevents", "releaseevents", "atob", "btoa", "settimeout", "cleartimeout", "setinterval", "clearinterval", "temporary", "persistent", "getcomputedstyle", "getmatchedcssrules", "webkitconvertpointfrompagetonode", "webkitconvertpointfromnodetopage", "webkitrequestfilesystem", "webkitresolvelocalfilesystemurl", "opendatabase", "addeventlistener", "removeeventlistener", "dispatchevent"];         (var iii in keys)         {             var key = keys[iii];             if (obj2[key])                 delete(obj2[key]);         }          (var in obj2)         {             obj2[i] = window[i];         }          return this.getarray(obj2, 0);     }, 

getarray()

getarray: function(obj, count)     {         try {             var data = this._getarray(obj, count);             var cache = [];             if (typeof data === 'object' && data !== null) {                 if (cache.indexof(data) !== -1) {                     // circular reference found, discard key                     return 'circular';                 }                 // store value in our collection                 cache.push(data);             }             if (typeof data == 'object')                 return 'json_' + json.stringify(data);             else                 return data;             //return data;         } catch (e)         {             console.log(e);             return '';         }     } 

,

_getarray

_getarray: function(obj, count)     {         count = isset(count) ? count : 0;         if (obj == window && count > 0)             return 'window';         if (obj && typeof obj == 'object')         {             if (object.keys(obj).length >= 100 && count > 0)             {                 return 'object,>100keys';             }             if (obj['tagname'])             {                 return obj['tagname'];             }              if (count > 0)             {                 var keys = ['$', 'tagname', 'scrolltop', 'contenteditable', 'documenturi', 'documentelement', 'context', 'selector', 'jquery', 'attribute_node', 'nodename'];                 (var ii in keys)                 {                     if (obj[0] && obj[0].length == 3)                     {                         ///log2(obj);                     }                     var key = keys[ii];                     if (obj[key])                     {                         return ii;                     }                 }             }             if (!$.isplainobject(obj))             {                 var obj2 = {};                 (var ii in obj)                 {                     obj2[ii] = obj[ii];                 }                 obj = obj2;             }         }         var arr = {};         (var in obj)         {             var type = typeof obj[i];             if (type == 'string' || type == 'number' || type == 'boolean')             {                 arr[i] = obj[i];             }              if (type == 'function')             {                 var obj2 = {};                 var funchasdata = false;                 (var ii in obj[i])                 {                     funchasdata = true;                     obj2[ii] = obj[i][ii];                 }                 if (funchasdata)                 {                     arr[i] = obj2;                 }             }               if (type == 'object')             {                 if (count > 5)                 {                     return '_';                 }                 arr[i] = this.getarray(obj[i], count + 1);             }         }         return arr;     } 

usage: getlocalproperties(object); or getlocalproperties.getwindow();

getlocalproperties = function(obj,parent_objects){     var keys = object.getownpropertynames(obj);     if (typeof parent_objects == 'object')     {         if (parent_objects.indexof(obj) >= 0)         {             return 'recursive obj';         }         parent_objects = getlocalproperties.copyarray(parent_objects);     } else {         parent_objects = [window];     }     //console.log(parent_objects.length);     if (parent_objects.length > 10)     {         return 'depth greater 10';     }     parent_objects.push(obj);     var new_object = {};     (var key_i_key in keys)     {         var key_i = keys[key_i_key];         var value_i = obj[key_i];         var value_cloned = getlocalproperties.getkeyvalue(value_i,parent_objects);         new_object[key_i] = value_cloned;     }     return new_object; }; getlocalproperties.getkeyvalue = function (value_i,parent_objects) {     getlocalproperties.last_value_i = value_i;     getlocalproperties.last_parent_objects = parent_objects;     var type = typeof value_i;     switch (type)     {         case 'object':             if (!value_i)             {                 return 'value_is_null';             }             if (array.isarray(value_i))             {                 //return value_i;             }             return getlocalproperties(value_i,parent_objects);         case 'boolean':         case 'number':         case 'string':             return value_i;     }     return 'not_copied';  }; getlocalproperties.copyarray = function(arr) {     var arr_new = [];     (var in arr)     {         arr_new.push(arr[i]);     }     return arr_new; }; getlocalproperties.getwindow = function() {     return getlocalproperties.getobjectclean(window); }; getlocalproperties.getobjectclean = function(obj) {     var = getlocalproperties(obj);     (var in a)     {         var value_i = a[i];         if (typeof value_i == 'string' && value_i == 'not_copied')         {             delete(a[i]);         }     }     return a; }; 

Comments