summaryrefslogtreecommitdiff
path: root/chat.js
blob: f3e81ef2b72a897b217851c5054d1db715f12374 (plain)
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
function getChannelName() {
    const path = window.location.pathname.split('/').filter(Boolean);
    if (path.length == 1) return path[path.length - 1];
    return null;
}

function replaceEmotes(node, emotes) {
    const walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null);

    const textNodes = [];
    while (walker.nextNode()) textNodes.push(walker.currentNode);

    textNodes.forEach(x => {
        const parent = x.parentNode;
        const frag = document.createDocumentFragment();
        const words = x.textContent.split(/(\s+)/);

        words.forEach(word => {
            if (emotes[word]) {
                const emote = emotes[word];

                const img = document.createElement("img");
                img.src = emote.miniatureurl;
                img.alt = word;
                img.classList.add("tiny-emote");
                img.addEventListener("mouseenter", () => console.log(word));
                frag.appendChild(img);
            } else {
                frag.appendChild(document.createTextNode(word));
            }
        });

        parent.replaceChild(frag, x);
    });
}