aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/js-components/chat.ts
blob: bb7028d2ca37cf7571dc9c1c6f1d5ffdd6e38028 (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
36
37
38
39
40
41
42
43
44
45
import Config from "../config";
import Utils from "../utils";
const utils = new Utils();

export interface ChatConfig {
    displayName: string,
    composerInitialValue?: string,
    customDescription?: string
}

export function openChat(config: ChatConfig): void {
    const chat = document.createElement("div");
    chat.classList.add("sbChatNotice");
    chat.style.zIndex = "2000";

    const iframe= document.createElement("iframe");
    iframe.src = "https://chat.sponsor.ajay.app/#" + utils.objectToURI("", config, false);
    chat.appendChild(iframe);

    const closeButton  = document.createElement("img");
    closeButton.classList.add("sbChatClose");
    closeButton.src = chrome.extension.getURL("icons/close.png");
    closeButton.addEventListener("click", () => {
        chat.remove();
        closeButton.remove();
    });
    chat.appendChild(closeButton);

    const referenceNode = utils.findReferenceNode();
    referenceNode.prepend(chat);
}

export async function openWarningChat(warningMessage: string): Promise<void> {
    const userNameData = await utils.asyncRequestToServer("GET", "/api/getUsername?userID=" + Config.config.userID);
    const userName = userNameData.ok ? JSON.parse(userNameData.responseText).userName : "";
    const publicUserID = await utils.getHash(Config.config.userID);
    const warningReasonMatch = warningMessage.match(/Warning reason: '(.+)'/);

    openChat({
        displayName: `${userName ? userName : ``}${userName !== publicUserID ? ` | ${publicUserID}` : ``}`,
        composerInitialValue: `I got a warning and want to know what I need to do to improve.` +
                                warningReasonMatch ? ` Warning reason: ${warningReasonMatch[1]}` : ``,
        customDescription: chrome.i18n.getMessage("warningChatInfo")
    });
}