aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/render/RectangleTooltip.tsx
blob: 1b357fa8596d5a8fca09a0465ae25cc0f26a8ac2 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as React from "react";
import { createRoot, Root } from 'react-dom/client';

export interface RectangleTooltipProps {
    text: string; 
    link?: string;
    referenceNode: HTMLElement;
    prependElement?: HTMLElement; // Element to append before
    bottomOffset?: string;
    leftOffset?: string;
    timeout?: number;
    htmlId?: string;
    maxHeight?: string;
    maxWidth?: string;
    backgroundColor?: string;
    fontSize?: string;
    buttonFunction?: () => void;
}

export class RectangleTooltip {
    text: string;   
    container: HTMLDivElement;
    root: Root;
    timer: NodeJS.Timeout;
    
    constructor(props: RectangleTooltipProps) {
        props.bottomOffset ??= "0px";
        props.leftOffset ??= "0px";
        props.maxHeight ??= "100px";
        props.maxWidth ??= "300px";
        props.backgroundColor ??= "rgba(28, 28, 28, 0.7)";
        this.text = props.text;
        props.fontSize ??= "10px";

        this.container = document.createElement('div');
        props.htmlId ??= "sponsorRectangleTooltip" + props.text;
        this.container.id = props.htmlId;
        this.container.style.display = "relative";

        if (props.prependElement) {
            props.referenceNode.insertBefore(this.container, props.prependElement);
        } else {
            props.referenceNode.appendChild(this.container);
        }

        if (props.timeout) {
            this.timer = setTimeout(() => this.close(), props.timeout * 1000);
        }

        this.root = createRoot(this.container);
        this.root.render(
            <div style={{
                bottom: props.bottomOffset, 
                left: props.leftOffset,
                maxHeight: props.maxHeight,
                maxWidth: props.maxWidth,
                backgroundColor: props.backgroundColor,
                fontSize: props.fontSize}} 
                    className="sponsorBlockRectangleTooltip" >
                    <div>
                        <img className="sponsorSkipLogo sponsorSkipObject"
                            src={chrome.extension.getURL("icons/IconSponsorBlocker256px.png")}>
                        </img>
                        <span className="sponsorSkipObject">
                            {this.text + (props.link ? ". " : "")}
                            {props.link ? 
                                <a style={{textDecoration: "underline"}} 
                                        target="_blank"
                                        rel="noopener noreferrer"
                                        href={props.link}>
                                    {chrome.i18n.getMessage("LearnMore")}
                                    </a> 
                            : null}
                        </span>
                    </div>
                    <button className="sponsorSkipObject sponsorSkipNoticeButton"
                        style ={{float: "right" }}
                        onClick={() => {
                            if (props.buttonFunction) props.buttonFunction();
                            this.close();
                        }}>

                        {chrome.i18n.getMessage("GotIt")}
                    </button>
            </div>
        )
    }

    close(): void {
        this.root.unmount();
        this.container.remove();

        if (this.timer) clearTimeout(this.timer);
    }
}