Created
December 23, 2010 07:46
-
-
Save jpmckinney/752712 to your computer and use it in GitHub Desktop.
Unpack and beautify JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// blog post: http://blog.slashpoundbang.com/post/2428557007/unpack-and-beautify-javascript | |
// Assumes js_beautify is available: see https://github.com/einars/js-beautify/raw/master/beautify.js | |
function unpack_and_beautify(value) { | |
var position = 0, | |
open_string = "eval(function(", | |
open_index = -1, | |
close_string = "'.split('|'),0,{}))", | |
close_index = -1, | |
close_length = close_string.length, | |
string = '', | |
pack; | |
while (true) { | |
open_index = value.indexOf(open_string, open_index + 1); | |
close_index = value.indexOf(close_string, close_index + 1); | |
if (open_index == -1 || close_index == -1) { | |
string += value.substring(position, value.length); | |
break; | |
} | |
else { | |
string += value.substring(position, open_index); | |
pack = value.substring(open_index, close_index + close_length); | |
try { | |
eval("var tmp = String" + pack.substring(4)); | |
string += tmp; | |
} | |
catch (e) { | |
window.console && console.log && console.log(e, pack); | |
string += pack; | |
} | |
position = close_index + close_length; | |
} | |
}; | |
return js_beautify(string, { | |
indent_size: 2, | |
space_after_anon_function: true, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment