Skip to content

Instantly share code, notes, and snippets.

@johnbabb
Last active December 2, 2017 15:35
Show Gist options
  • Save johnbabb/2d69374455e386eae87a to your computer and use it in GitHub Desktop.
Save johnbabb/2d69374455e386eae87a to your computer and use it in GitHub Desktop.
Chrome Dev Tools - Save Large Variable to File.
var JsonNetDecycle = (function () {
function JsonNetDecycle() { }
JsonNetDecycle.decycle = function decycle(obj) {
var catalog = [];
var newObj = JsonNetDecycle.getDecycledCopy(obj, catalog);
return newObj;
}
JsonNetDecycle.getDecycledCopy = function getDecycledCopy(obj, catalog) {
var i;
var name;
var nu;
switch(typeof obj) {
case 'object': {
if(obj === null || obj instanceof Boolean || obj instanceof Date || obj instanceof Number || obj instanceof RegExp || obj instanceof String) {
return obj;
}
for(i = 0; i < catalog.length; i += 1) {
if(catalog[i] === obj) {
return {
$ref: i.toString()
};
}
}
obj.$id = catalog.length.toString();
catalog.push(obj);
if(Object.prototype.toString.apply(obj) === '[object Array]') {
nu = [];
for(i = 0; i < obj.length; i += 1) {
nu[i] = JsonNetDecycle.getDecycledCopy(obj[i], catalog);
}
} else {
nu = {
};
for(name in obj) {
if(Object.prototype.hasOwnProperty.call(obj, name)) {
nu[name] = JsonNetDecycle.getDecycledCopy(obj[name], catalog);
}
}
}
return nu;
}
case 'number':
case 'string':
case 'boolean': {
return obj;
}
}
}
JsonNetDecycle.retrocycle = function retrocycle(obj) {
var catalog = [];
JsonNetDecycle.findReferences(obj, catalog);
JsonNetDecycle.resolveReferences(obj, catalog);
}
JsonNetDecycle.findReferences = function findReferences(obj, catalog) {
var i;
if(obj && typeof obj === 'object') {
var id = obj.$id;
if(typeof id === 'string') {
catalog[id] = obj;
}
if(Object.prototype.toString.apply(obj) === '[object Array]') {
for(i = 0; i < obj.length; i += 1) {
JsonNetDecycle.findReferences(obj[i], catalog);
}
} else {
for(name in obj) {
if(typeof obj[name] === 'object') {
JsonNetDecycle.findReferences(obj[name], catalog);
}
}
}
}
}
JsonNetDecycle.resolveReferences = function resolveReferences(obj, catalog) {
var i, item, name, id;
if(obj && typeof obj === 'object') {
if(Object.prototype.toString.apply(obj) === '[object Array]') {
for(i = 0; i < obj.length; i += 1) {
item = obj[i];
if(item && typeof item === 'object') {
id = item.$ref;
if(typeof id === 'string') {
obj[i] = catalog[id];
} else {
JsonNetDecycle.resolveReferences(item, catalog);
}
}
}
} else {
for(name in obj) {
if(typeof obj[name] === 'object') {
item = obj[name];
if(item) {
id = item.$ref;
if(typeof id === 'string') {
obj[name] = catalog[id];
} else {
JsonNetDecycle.resolveReferences(item, catalog);
}
}
}
}
}
}
}
return JsonNetDecycle;
})();
//@ sourceMappingURL=JsonNetDecycle.js.map
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(JsonNetDecycle.decycle(data));
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
})(console)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment