jQuery / Javascript leet翻译插件

时间:2012-02-21 19:35:10

标签: javascript jquery html jquery-plugins

所以我一直在寻找不同的地方试图获得一个1337翻译器,我可以理解到足以使用jQuery插件,我觉得我很接近..但我不是100%什么我做错了..我很确定我有什么..不是一个jQuery插件..所以我希望有人可以帮我弄清楚我做错了什么。

(function($){
    $.fn.leet = function() {

        // Create the Phrase translations arrays
            var PhrasesEnglish = 
                new Array('crap', 'dude', 'hacker',
                          'hacks', 'you', 'cool', 'oh my god',
                          'fear', 'power', 'own',
                          'what the hell', 'elite', 'for the win', 
                          'oh really', 'good game');
            var PhrasesLeet = 
                new Array('carp', 'dood', 'haxor', 'hax', 'joo',
                          'kewl', 'omg', 'ph43', 'powwah', 'pwn', 
                          'wth', 'leet', 'ftw', 'o rly', 'gg');

            // Create the Letter translations arrays
            var LettersEnglish = 
                new Array('n', 'b', 'k', 'd', 'e', 'f', 'g', 'h',
                          'p', 'm', 'r', 'l', 'o', 'q', 's', 't',
                          'u', 'x', 'w', 'y', 'z', 'c', 'a', 'j', 
                          'i', 'v', ' ');
            var LettersLeet = 
                new Array('/\\/', '|}', '|X', '[)', '3', '|=', 'gee', '|-|',
                          '|*', '(\\/)', '|2', '1', '()', '0', '$', '+',
                          '|_|', '><', '\\X/', '\'/', '2', '<', '/\\', '_|', 
                          '|', '\\/', '  ');

            // Translates text in input area to/from leet speak
            function translateText() {
                var inputString = document.getElementById('input').value;

                if (document.getElementById('conversionType').value == "e") {
                    for (i = 0; i < PhrasesEnglish.length; ++i)
                        inputString = inputString.replace(
                                new RegExp(PhrasesEnglish[i], "gi"),
                                PhrasesLeet[i]
                                );

                    for (i = 0; i < LettersEnglish.length; ++i)
                        inputString = inputString.replace(
                                new RegExp(LettersEnglish[i], "gi"),
                                LettersLeet[i]
                                );
                }
                else {
                    for (i = 0; i < LettersLeet.length; ++i)
                        inputString = inputString.replace(
                                new RegExp(RegExp.escape(LettersLeet[i]), "g"),
                                LettersEnglish[i]
                                );

                    for (i = 0; i < PhrasesLeet.length; ++i)
                        inputString = inputString.replace(
                                new RegExp(RegExp.escape(PhrasesLeet[i]), "g"),
                                PhrasesEnglish[i]
                                );
                }

                document.getElementById('input').value = inputString;
            }

        return this;
    };
})(jQuery);

这就是我所拥有的插件,请注意我在网上找到了这个并且我正在尝试重做它...所以html是有点可怕的atm ..

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script src="leet.js"></script>
    <script>
        $(function() {
            $("div").leet();
        });
    </script>

    <body>

        <div style="border: solid 1px Black; 
                    padding: 5px; width: 350px; 
                    background-color: White;">
            <label for="input">
                Enter message here:</label><br />
            <textarea id="input" name="input" rows="10" cols="40" 
            style="font-weight: bold;
                   background-image: url('leetBG.png'); 
                   background-attachment: fixed; 
                   background-position: 160px 165px;
                   background-repeat: no-repeat;"></textarea>
            <br />
            <input type="submit" value="Translate" 
             onclick="translateText();" />
            <select id="conversionType">
                <option value="e">English -> 1337</option>
                <option value="3">1337 -> English</option>
            </select>
        </div>

    </body>

1 个答案:

答案 0 :(得分:0)

尝试将翻译文本功能从其自己的功能中移除到扩展的一部分。然后调用扩展名onclick,这样

(function($){
$.fn.leet = function() {

    // Create the Phrase translations arrays

        var PhrasesEnglish = ['crap', 'dude', 'hacker',
                      'hacks', 'you', 'cool', 'oh my god',
                      'fear', 'power', 'own',
                      'what the hell', 'elite', 'for the win', 
                      'oh really', 'good game'];
        var PhrasesLeet = ['carp', 'dood', 'haxor', 'hax', 'joo',
                      'kewl', 'omg', 'ph43', 'powwah', 'pwn', 
                      'wth', 'leet', 'ftw', 'o rly', 'gg'];

    // Create the Letter translations arrays

        var LettersEnglish = ['n', 'b', 'k', 'd', 'e', 'f', 'g', 'h',
                      'p', 'm', 'r', 'l', 'o', 'q', 's', 't',
                      'u', 'x', 'w', 'y', 'z', 'c', 'a', 'j', 
                      'i', 'v', ' '];
        var LettersLeet = ['/\\/', '|}', '|X', '[)', '3', '|=', 'gee', '|-|',
                      '|*', '(\\/)', '|2', '1', '()', '0', '$', '+',
                      '|_|', '><', '\\X/', '\'/', '2', '<', '/\\', '_|', 
                      '|', '\\/', '  '];
    // Translates text in input area to/from leet speak

        var id = $(this).attr('id');            
        var inputString = document.getElementById(id).value;

        if (document.getElementById('conversionType').value == "e") {
            for (i = 0; i < PhrasesEnglish.length; ++i)
                inputString = inputString.replace(
                        new RegExp(PhrasesEnglish[i], "gi"),
                        PhrasesLeet[i]
                        );

                for (i = 0; i < LettersEnglish.length; ++i)
                    inputString = inputString.replace(
                            new RegExp(LettersEnglish[i], "gi"),
                            LettersLeet[i]
                            );
        }
        else {
            for (i = 0; i < LettersLeet.length; ++i)
                inputString = inputString.replace(
                        new RegExp(RegExp.escape(LettersLeet[i]), "g"),
                        LettersEnglish[i]
                        );

            for (i = 0; i < PhrasesLeet.length; ++i)
                inputString = inputString.replace(
                        new RegExp(RegExp.escape(PhrasesLeet[i]), "g"),
                        PhrasesEnglish[i]
                        );
        }

        document.getElementById(id).value = inputString;

    };
})(jQuery);​

你的onclick处理程序看起来像这样

<input type="submit" value="Translate" onclick="$('#input').leet();" />

这是小提琴(我不知道人们如何阅读,我认为在我破译它之前它无法正常工作。)http://jsfiddle.net/ThD29/1/

此外,1337的翻译 - &gt;英语不起作用。显然RegExp没有名为'escape'的方法。我对正则表达式了解不多,所以希望其他人可以指出答案。