aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/js-components/chat.ts
blob: 4bbb4a2beddb0bac6d0e5706a571cdb455837b4b (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
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);

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