summaryrefslogtreecommitdiffstats
path: root/script.js
blob: e2c8f36fc41b781243c380f1c0d40b976c384420 (plain) (blame)
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
(() => {
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);

    const title = urlParams.get("title");
    const streamUrl = urlParams.get("stream");
    const ircUrl = urlParams.get("irc");

    if (streamUrl === null || ircUrl === null) {
        return;
    }

    /** @type HTMLInputElement */
    const titleEl = document.querySelector(".c-form-field__input[name=title]");
    /** @type HTMLInputElement */
    const streamEl = document.querySelector(".c-form-field__input[name=stream]");
    /** @type HTMLInputElement */
    const ircEl = document.querySelector(".c-form-field__input[name=irc]");

    titleEl.value = title;
    streamEl.value = streamUrl;
    ircEl.value = ircUrl;

    if (streamUrl.trim().length === 0 || ircUrl.trim().length === 0) {
        return;
    }

    const normalizedircUrl = ircUrl.startsWith("irc://")
        ? ircUrl.substring(6)
        : ircUrl.startsWith("ircs://")
        ? ircUrl.substring(7)
        : ircUrl;
    const ircUrlComponents = normalizedircUrl.split("/");

    if (ircUrlComponents.length !== 2) {
        return;
    }

    if (title !== null && title.trim().length !== 0) {
        document.querySelector("title").textContent = title;
    }

    const ircChan = ircUrlComponents[0];
    const ircHost = ircUrlComponents[1];

    document.querySelector(".c-section--create").classList.add("u-hidden");
    document.querySelector(".c-section--main").classList.remove("u-hidden");

    /** @type HTMLIFrameElement */
    const streamFrameEl = document.querySelector(".c-frame--pt iframe");
    /** @type HTMLIFrameElement */
    const ircFrameEl = document.querySelector(".c-frame--irc iframe");

    streamFrameEl.src = streamUrl;
    ircFrameEl.src = "https://irc.vulpes.one/?uri=" + ircUrl;

    /** @type HTMLAnchorElement */
    const ircLink = document.querySelector(".c-irc-link");
    ircLink.href = ircUrl;
    ircLink.textContent = `Join #${ircChan} on ${ircHost}`;

    document.querySelector(".c-msg__close").addEventListener("click", (e) => {
        e.preventDefault();

        /** @type HTMLElement */
        const el = e.currentTarget;
        el.parentElement.remove();
    });
})();