diff --git a/index.html b/index.html index 9a99158b..cf3dec3f 100644 --- a/index.html +++ b/index.html @@ -35,100 +35,7 @@ - + @@ -144,166 +51,7 @@ - - + @@ -499,64 +247,8 @@
- - + + diff --git a/web/common-function.js b/web/common-function.js new file mode 100644 index 00000000..66cf487b --- /dev/null +++ b/web/common-function.js @@ -0,0 +1,157 @@ +var the = { + use_codemirror: (!window.location.href.match(/without-codemirror/)), + beautify_in_progress: false, + editor: null // codemirror editor +}; + +function run_tests() { + var st = new SanityTest(); + run_javascript_tests(st, Urlencoded, js_beautify, html_beautify, css_beautify); + run_css_tests(st, Urlencoded, js_beautify, html_beautify, css_beautify); + run_html_tests(st, Urlencoded, js_beautify, html_beautify, css_beautify); + JavascriptObfuscator.run_tests(st); + P_A_C_K_E_R.run_tests(st); + Urlencoded.run_tests(st); + MyObfuscate.run_tests(st); + var results = st.results_raw() + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/ /g, ' ') + .replace(/\r/g, '·') + .replace(/\n/g, '
'); + $('#testresults').html(results).show(); +} + + +function any(a, b) { + return a || b; +} + +function read_settings_from_cookie() { + $('#tabsize').val(any($.cookie('tabsize'), '4')); + $('#brace-style').val(any($.cookie('brace-style'), 'collapse')); + $('#detect-packers').prop('checked', $.cookie('detect-packers') !== 'off'); + $('#max-preserve-newlines').val(any($.cookie('max-preserve-newlines'), '5')); + $('#keep-array-indentation').prop('checked', $.cookie('keep-array-indentation') === 'on'); + $('#break-chained-methods').prop('checked', $.cookie('break-chained-methods') === 'on'); + $('#indent-scripts').val(any($.cookie('indent-scripts'), 'normal')); + $('#space-before-conditional').prop('checked', $.cookie('space-before-conditional') !== 'off'); + $('#wrap-line-length').val(any($.cookie('wrap-line-length'), '0')); + $('#unescape-strings').prop('checked', $.cookie('unescape-strings') === 'on'); + $('#jslint-happy').prop('checked', $.cookie('jslint-happy') === 'on'); + $('#end-with-newline').prop('checked', $.cookie('end-with-newline') === 'on'); + $('#indent-inner-html').prop('checked', $.cookie('indent-inner-html') === 'on'); + $('#comma-first').prop('checked', $.cookie('comma-first') === 'on'); + $('#e4x').prop('checked', $.cookie('e4x') === 'on'); +} + +function store_settings_to_cookie() { + var opts = { + expires: 360 + }; + $.cookie('tabsize', $('#tabsize').val(), opts); + $.cookie('brace-style', $('#brace-style').val(), opts); + $.cookie('detect-packers', $('#detect-packers').prop('checked') ? 'on' : 'off', opts); + $.cookie('max-preserve-newlines', $('#max-preserve-newlines').val(), opts); + $.cookie('keep-array-indentation', $('#keep-array-indentation').prop('checked') ? 'on' : 'off', opts); + $.cookie('break-chained-methods', $('#break-chained-methods').prop('checked') ? 'on' : 'off', opts); + $.cookie('space-before-conditional', $('#space-before-conditional').prop('checked') ? 'on' : 'off', + opts); + $.cookie('unescape-strings', $('#unescape-strings').prop('checked') ? 'on' : 'off', opts); + $.cookie('jslint-happy', $('#jslint-happy').prop('checked') ? 'on' : 'off', opts); + $.cookie('end-with-newline', $('#end-with-newline').prop('checked') ? 'on' : 'off', opts); + $.cookie('wrap-line-length', $('#wrap-line-length').val(), opts); + $.cookie('indent-scripts', $('#indent-scripts').val(), opts); + $.cookie('indent-inner-html', $('#indent-inner-html').prop('checked') ? 'on' : 'off', opts); + $.cookie('comma-first', $('#comma-first').prop('checked') ? 'on' : 'off', opts); + $.cookie('e4x', $('#e4x').prop('checked') ? 'on' : 'off', opts); + +} + +function unpacker_filter(source) { + var trailing_comments = '', + comment = '', + unpacked = '', + found = false; + + // cut trailing comments + do { + found = false; + if (/^\s*\/\*/.test(source)) { + found = true; + comment = source.substr(0, source.indexOf('*/') + 2); + source = source.substr(comment.length).replace(/^\s+/, ''); + trailing_comments += comment + "\n"; + } else if (/^\s*\/\//.test(source)) { + found = true; + comment = source.match(/^\s*\/\/.*/)[0]; + source = source.substr(comment.length).replace(/^\s+/, ''); + trailing_comments += comment + "\n"; + } + } while (found); + + var unpackers = [P_A_C_K_E_R, Urlencoded, JavascriptObfuscator/*, MyObfuscate*/]; + for (var i = 0; i < unpackers.length; i++) { + if (unpackers[i].detect(source)) { + unpacked = unpackers[i].unpack(source); + if (unpacked != source) { + source = unpacker_filter(unpacked); + } + } + } + + return trailing_comments + source; +} + + +function beautify() { + if (the.beautify_in_progress) return; + + store_settings_to_cookie(); + + the.beautify_in_progress = true; + + var source = the.editor ? the.editor.getValue() : $('#source').val(), + output, + opts = {}; + + opts.indent_size = $('#tabsize').val(); + opts.indent_char = opts.indent_size == 1 ? '\t' : ' '; + opts.max_preserve_newlines = $('#max-preserve-newlines').val(); + opts.preserve_newlines = opts.max_preserve_newlines !== "-1"; + opts.keep_array_indentation = $('#keep-array-indentation').prop('checked'); + opts.break_chained_methods = $('#break-chained-methods').prop('checked'); + opts.indent_scripts = $('#indent-scripts').val(); + opts.brace_style = $('#brace-style').val() + ($('#brace-preserve-inline').prop('checked') ? ",preserve-inline" : ""); + opts.space_before_conditional = $('#space-before-conditional').prop('checked'); + opts.unescape_strings = $('#unescape-strings').prop('checked'); + opts.jslint_happy = $('#jslint-happy').prop('checked'); + opts.end_with_newline = $('#end-with-newline').prop('checked'); + opts.wrap_line_length = $('#wrap-line-length').val(); + opts.indent_inner_html = $('#indent-inner-html').prop('checked'); + opts.comma_first = $('#comma-first').prop('checked'); + opts.e4x = $('#e4x').prop('checked'); + + if (looks_like_html(source)) { + output = html_beautify(source, opts); + } else { + if ($('#detect-packers').prop('checked')) { + source = unpacker_filter(source); + } + output = js_beautify(source, opts); + } + if (the.editor) { + the.editor.setValue(output); + } else { + $('#source').val(output); + } + + the.beautify_in_progress = false; +} + +function looks_like_html(source) { + // - looks like html + var trimmed = source.replace(/^[ \t\n\r]+/, ''); + return trimmed && (trimmed.substring(0, 1) === '<'); +} diff --git a/web/common-style.css b/web/common-style.css new file mode 100644 index 00000000..b21a292c --- /dev/null +++ b/web/common-style.css @@ -0,0 +1,92 @@ +body { + background: #eee; + color: #333; +} +img { + border: 0; +} +a.self { + font-weight: bold; + text-decoration: none; + color: #444; + border-bottom: 1px solid #aaa +} +p, select, label, .blurb, a.turn-off-codemirror { + font:13px/1.231 arial, sans-serif; + *font-size:small; +} +a.turn-off-codemirror { + margin-left: 25px; +} +button.submit { + width: 100%; + padding: 10px 0; + cursor: pointer; + margin: 0; +} +button.submit em { + font-size: 11px; + font-style: normal; + color: #999; +} +label { + cursor: pointer; +} +select { + width: 220px; +} +table#options { + float: right; +} +table#options td { + vertical-align: top; + padding-left: 10px; +} +.col-6 { + width: 50%; + float: left; +} +#about { + float: left; +} +#about p { + margin: 0 6px 6px 0; +} +.uses li { + padding-top: 3px; + line-height: 150%; +} +.uses li.sep { + margin-top: 8px; +} +#testresults { + display: none; + font-family: monaco, "lucida console", "courier new", monospace; +} +.CodeMirror { + border: 1px solid #ccc; + height: 450px; + font-size: 90%; + margin-bottom: 6px; + background: white; +} +p { + margin-left: 40px; + margin-right: 40px; +} +a { + white-space: nowrap; + color: #36d; +} +.contributor-sep { + clear: left; + border-top: 1px solid #ccc; + padding-top: 8px; +} +h2 { + margin-top: 32px; + margin-left: 40px; + margin-bottom: 0; + font-size: 20px; + font-weight: normal; +} diff --git a/web/google-analytics.js b/web/google-analytics.js new file mode 100644 index 00000000..f140055d --- /dev/null +++ b/web/google-analytics.js @@ -0,0 +1,10 @@ +var _gaq = [ + ['_setAccount', 'UA-7409939-1'], + ['_trackPageview'] +]; +(function (d, t) { + var g = d.createElement(t), + s = d.getElementsByTagName(t)[0]; + g.src = '//www.google-analytics.com/ga.js'; + s.parentNode.insertBefore(g, s); +}(document, 'script')); diff --git a/web/onload.js b/web/onload.js new file mode 100644 index 00000000..940ad934 --- /dev/null +++ b/web/onload.js @@ -0,0 +1,44 @@ +$(function () { + + read_settings_from_cookie(); + + var default_text = + "// This is just a sample script. Paste your real code (javascript or HTML) here.\n\nif ('this_is'==/an_example/){of_beautifier();}else{var a=b?(c%d):e[f];}"; + var textArea = $('#source')[0]; + + if (the.use_codemirror && typeof CodeMirror !== 'undefined') { + the.editor = CodeMirror.fromTextArea(textArea, { + theme: 'default', + lineNumbers: true + }); + the.editor.focus(); + + the.editor.setValue(default_text); + $('.CodeMirror').click(function () { + if (the.editor.getValue() == default_text) { + the.editor.setValue(''); + } + }); + } else { + $('#source').val(default_text).bind('click focus', function () { + if ($(this).val() == default_text) { + $(this).val(''); + } + }).bind('blur', function () { + if (!$(this).val()) { + $(this).val(default_text); + } + }); + } + + + $(window).bind('keydown', function (e) { + if (e.ctrlKey && e.keyCode == 13) { + beautify(); + } + }) + $('.submit').click(beautify); + $('select').change(beautify); + + +});