2007/11/22 09:21

흉칙한 코드를 아름답게 - Beautify Javascript

Beautify Javascript는 조금이라도 더 빠른 웹 애플리케이션을 만들기 위해 Minify와 같은 라이브러리자바스크립트 소스코드를 압축(최소화)하거나, 추가로 남들이 알아보지 못하도록(또는 보안상의 목적 으로) 암호화까지 된 흉칙한 자바스크립트 코드를 다시금 보기(읽기) 좋게 만들어 주는 PHP스크립트입니다.

minify로 압축한 아래와 같은 코드를
var Prototype={Version:'1.6.0',Browser:{IE:!!(window.attachEvent&amp;&amp;!window.opera),Opera:!!window.opera,WebKit:navigator.userAgent.indexOf('AppleWebKit/')>-1,Gecko:navigator.userAgent.indexOf('Gecko')>-1&amp;&amp;navigator.userAgent.indexOf('KHTML')==-1,MobileSafari:!!navigator.userAgent.match(/Apple.*Mobile.*Safari/)},BrowserFeatures:{XPath:!!document.evaluate,ElementExtensions:!!window.HTMLElement,SpecificElementExtensions:document.createElement('div').__proto__&amp;&amp;document.createElement('div').__proto__!==document.createElement('form').__proto__},ScriptFragment:'&lt;script[^>]*>([\\S\\s]*?)<\/script>',JSONFilter:/^\/\*-secure-([\s\S]*)\*\/\s*$/,emptyFunction:function(){},K:function(x){return x}};

요렇게 만들어 줍니다.
var Prototype = {
    Version: '1.6.0',
    Browser: {
        IE: !!(window.attachEvent &amp;&amp; !window.opera),
        Opera: !!window.opera,
        WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
        Gecko: navigator.userAgent.indexOf('Gecko') > -1 &amp;&amp; navigator.userAgent.indexOf('KHTML') == -1,
        MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
    },
    BrowserFeatures: {
        XPath: !!document.evaluate,
        ElementExtensions: !!window.HTMLElement,
        SpecificElementExtensions: document.createElement('div').__proto__ &amp;&amp; document.createElement('div').__proto__ !== document.createElement('form').__proto__
    },
    ScriptFragment: '&lt;script[^>]*>([\\S\\s]*?)<\/script>',
    JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/,
    emptyFunction: function() {},
    K: function(x) {
        return x
    }
};


Example:


* 자바스크립트 버전이 추가되었습니다. 2008-5-21

파이어준 | 카테고리 : 자료 - 웹개발 | 조회(6423) | 엮인글 | 댓글(2) |


▲ 이전 글 : Canvas태그로 만들어지는 UI - Mocha UI
▼ 다음 글 : 플래시 사이트에 뒤로가기 버튼을... - SWFAddress