1. #1
    Avatar von DotNet
    Registriert seit
    10.06.2015
    Beiträge
    693
    Thanked 327 Times in 196 Posts

    Standard Knuddels KCodes in HTML umwandeln (Javascript)

    Wandelt Knuddels KCodes wie _ in HTML Code um, den man gut im Browser darstellen kann. Woher das kommt weiß ich gar nicht mehr so genau, hatte mal mit einem Browser-Client gespielt, als Knuddels früher noch Relevanz hatte. Wahrscheinlich kann es keiner mehr gebrauchen, aber bevor ich es lösche wollte ich es wenigstens hier posten.
    Spoiler:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    function TagReplacement(str, fc, fs)
    {
        str = replaceTags(str, "_", "<b>", "</b>");
        str = replaceTags(str, "\"", "<i>", "</i>");
        str = replaceTags(str, '§', '<span style="color: ' + fc + '; font-size: ' + fs + 'px;">', '</span>');
        str = replaceSingleTags(str, "#", "");
        str = replaceSingleTags(str, '\\', '');
        return str;
    }
     
    function replaceTags(row, code, firstTag, secondTag)
    {
        var chars = row.split("");
        first = new Boolean(true);
        for (var i in chars)
        {
            if (chars[i] == code)
            {
                if (i > 0 && chars[i - 1] == '\\')
                    continue;
                else
                {
                    if (first)
                        chars[i] = firstTag;
                    else
                        chars[i] = secondTag;
                    first = !first;
                }
            }
        }
        return (chars.join(""));
    }
     
    function replaceSingleTags(row, code, tag)
    {
        var chars = row.split("");
        for (var i in chars)
        {
            if (chars[i] == code)
            {
                if (i > 0 && chars[i - 1] == '\\')
                    continue;
                else
                    chars[i] = tag;
            }
        }
        return (chars.join(""));
    }
     
    function KCode2HTML(string, bb, rr, fc, fs)
    {
        var p = new RegExp('(°.*?°)');
        var m = p.exec(string);
        var occurences = new Array();
        var i = 0;
        while (m != null)
        {
            occurences.push(m[1]);
            string = string.replace(m[1], "{" + i + "}");
            m = p.exec(string);
            i++;
        }
        string = TagReplacement(string, fc, fs);
        for (var x in occurences)
        {
            string = string.replace("{" + x + "}", detect(occurences[x], bb, rr, fc, fs));
        }
        return string;
    }
     
    function detect(code, bb, rr, fc, fs)
    {
        code = code.toString();
        if (code.substr(code.length - 2, 1) == "\\")
        {
            return code.replace('\\', '');
        }
     
        code = code.replace("°", "");
        code = code.replace("°", "");
     
        var ChatColorShortCuts = new Array("W", "E", "R", "O", "P", "A", "D", "G", "K", "Y", "C", "B", "N", "M", "BB", "RR");
        var HTMLColors = new Array("#FFFFFF", "#00AC00", "#FF0000", "#FFC800", "#FFAFAF", "#808080", "#404040", "#00FF00", "#000000", "#FFFF00", "#00FFFF", "#0000FF", "#964A00", "#FF00FF", bb, rr);
     
        if (!code.startsWith(">") && !code.startsWith("BB>"))
        {
            if (code.startsWith("["))
            {
                return code;
            }
            //Format reset
            if (code == "r")
            {
                code = '<span style="color: ' + fc + '; font-size: ' + fs + 'px;">';
                return code;
            }
            //Points -> Linebreaks
            if (code == "!")
            {
                code = '.........'
                return code;
            }
            //Color+Sizes
            if (code.length > 2)
            {
                var colorcode = "";
                var color = "";
                var size = 0;
                for (var y = 0; y <= 16; y++)
                {
                    if (code.substr(code.length - 2, 2) == ChatColorShortCuts[y])
                    {
                        color = HTMLColors[y];
                        colorcode = code.substr(code.length - 2, 2);
                        break;
                    }
                    else if (code.substr(0, 2) == ChatColorShortCuts[y])
                    {
                        color = HTMLColors[y];
                        colorcode = code.substr(0, 2);
                        break;
                    }
                    else if (code.substr(0, 1) == ChatColorShortCuts[y])
                    {
                        color = HTMLColors[y];
                        colorcode = code.substr(0, 1);
                        break;
                    }
                    else if (code.substr(code.length - 1, 1) == ChatColorShortCuts[y])
                    {
                        color = HTMLColors[y];
                        colorcode = code.substr(code.length - 1, 1);
                        break;
                    }
                }
                code = code.replace(colorcode, "");
                if (code.length > 0)
                {
                    size = code;
                    return '<span style="color: ' + color + '; font-size: ' + size + 'px;">';
                }
            }
            //Colors
            for (var i = 0; i <= 16; i++)
                if (code == ChatColorShortCuts[i])
                    return '<span style="color: ' + HTMLColors[i] + ';">';
            //Sizes
            if (typeof parseInt(code) == "number")
                return '<span style="font-size: ' + code + 'px;">';
        }
        else
        {
            if (code.startsWith(">_") || code.startsWith("BB>") || code.contains('|'))
            {          
                var fontColor = "";
                var fontWeight = 'normal';
                var textDecoration = '';
                 
                if(code.startsWith('BB')){
                    fontColor =  bb;
                    textDecoration = 'underline'
                }
                if(code.contains('_'))
                    fontWeight = 'bold';
                 
                var specialFormat = ' style="color: rgb(' + fontColor + '); font-weight:' + fontWeight + '; text-decoration: '+ textDecoration +'"';
                     
                code = code.replace(">_h", "");
                code = code.replace(">_", "");
                code = code.replace("BB>", "");
                code = code.replace("BB", "");
                code = code.replace(">", "");
             
                var params = null;
                var leadingImage = "";
                if(code.contains('<>')){
                    params = code.split('<>')[1].split('|');
                    leadingImage = code.split('<>')[0];
                    leadingImage = buildImageUrl(leadingImage);            
                }
                else{
                    params = code.split('|');
                }
             
                code = code.replace(">", "");
                 
                code = code.replace("<", "");           
                 
                var elemt = document.createElement("span");
                 
                var linkText = params[0];
                var leftClick = params[1];
                var rightClick = params[1];
                if(params.length >= 3){
                    rightClick = params[2];        
                    rightClick = rightClick.replace('"', linkText);
                    elemt.oncontextmenu = linkRightClick;
                }
                else
                    elemt.oncontextmenu = linkLeftClick;
                     
                leftClick = leftClick.replace('"', linkText);
                elemt.onclick = linkLeftClick;
                 
                if(leadingImage == "")
                     return '<span '+ specialFormat +' class="linkedContent" name="' + leftClick + '|' + rightClick + '">' + linkText + '</span>';
                else
                     return '<img src="' + leadingImage + '"><span '+ specialFormat +' class="linkedContent" name="' + leftClick + '|' + rightClick + '">' + linkText + '</span>';
            }
            else if (code.startsWith(">sm_"))
            {          
                var bgImg = "";
                if (code.contains('<>'))
                {
                    bgImg = code.split('<>')[0];
                    bgImg = bgImg.replace('>');
                    code = code.split('<>')[1];
                }
     
                code = code.replace(">", "");
                code = code.replace("<", "");
                 
                code = buildImageUrl(code);
     
                return '<img style="background-image: url(' + bgImg + ');" src="' + code + '">';
            }
        }
        return code;
    }
     
    function buildImageUrl(code){
                var end = "";
                if (code.endsWith(".png"))
                    end = ".png";
                else if (code.endsWith(".jpg"))
                    end = ".jpg";
                else if (code.endsWith(".gif"))
                    end = ".gif";
     
                if (code.contains('/') && !code.contains("features") && !code.contains("icons"))
                    code = code.split('/')[code.split('/').Length - 1];
     
                if (code.contains("..."))
                    code = code.substring(0, code.indexOf("..."));
     
                code = code.replace("?", "´");
                code = code.replace("%", "~");
     
                if (!code.endsWith(".png") && !code.endsWith(".jpg") && !code.endsWith(".gif"))
                    code += end;
     
                if (!code.contains("pics"))
                    code = "knuddels.de" + "/pics/" + code;
                else
                    code = "knuddels.de" + "/" + code;
                code = "http://chat." + code;
                 
                return code;
    }
    Geändert von Darkfield (24.03.2016 um 06:35 Uhr)

    Im Krieg gibt es keine Gewinner, nur Verlierer!

  2. The Following 2 Users Say Thank You to DotNet For This Useful Post:

    BitNet (03.04.2016), Negok (23.03.2016)

Ähnliche Themen

  1. C# Webbrowser inject Javascript in html loaded js
    Von Bl1zz4rD im Forum .Net
    Antworten: 11
    Letzter Beitrag: 16.06.2013, 22:20
  2. Knuddels startet HTML-Chat?
    Von Fif im Forum Knuddels-News
    Antworten: 21
    Letzter Beitrag: 14.06.2013, 18:02
  3. HTML Version des Knuddels-Designs gesucht
    Von Devon im Forum Skriptsprachen
    Antworten: 1
    Letzter Beitrag: 23.01.2012, 01:08
  4. Antworten: 1
    Letzter Beitrag: 17.01.2012, 11:51
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.