1. #1
    Avatar von DotNet
    Registriert seit
    10.06.2015
    Beiträge
    661
    Thanked 316 Times in 185 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:

    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, "#", "<br>");
    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.