aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dearrowPromotion.ts
blob: 208c627f733bdb083771a2c6c5ca08bf6856d64d (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
import { waitFor } from "../maze-utils/src";
import { getYouTubeTitleNode } from "../maze-utils/src/elements";
import { getHash } from "../maze-utils/src/hash";
import { getVideoID, isOnInvidious, isOnMobileYouTube } from "../maze-utils/src/video";
import Config from "./config";
import { Tooltip } from "./render/Tooltip";
import { isDeArrowInstalled } from "./utils/crossExtension";
import { isVisible } from "./utils/pageUtils";
import { asyncRequestToServer } from "./utils/requests";

let tooltip: Tooltip = null;
export async function tryShowingDeArrowPromotion() {
    if (Config.config.showDeArrowPromotion
            && !isOnMobileYouTube()
            && !isOnInvidious()
            && document.URL.includes("watch")
            && Config.config.showUpsells 
            && Config.config.showNewFeaturePopups
            && (Config.config.skipCount > 30 || !Config.config.trackViewCount)) {

            if (!await isDeArrowInstalled()) {
                try {
                    const element = await waitFor(() => getYouTubeTitleNode(), 5000, 500, (e) => isVisible(e)) as HTMLElement;
                    if (element && element.innerText && badTitle(element.innerText)) {
                        const hashPrefix = (await getHash(getVideoID(), 1)).slice(0, 4);
                        const deArrowData = await asyncRequestToServer("GET", "/api/branding/" + hashPrefix);
                        if (!deArrowData.ok) return;

                        const deArrowDataJson = JSON.parse(deArrowData.responseText);
                        const title = deArrowDataJson?.[getVideoID()]?.titles?.[0];
                        if (title && title.title && (title.locked || title.votes > 0)) {
                            Config.config.showDeArrowPromotion = false;
        
                            tooltip = new Tooltip({
                                text: chrome.i18n.getMessage("DeArrowTitleReplacementSuggestion") + "\n\n" + title.title,
                                linkOnClick: () => {
                                    window.open("https://dearrow.ajay.app");
                                    Config.config.shownDeArrowPromotion = true;
                                },
                                referenceNode: element,
                                prependElement: element.firstElementChild as HTMLElement,
                                timeout: 15000,
                                positionRealtive: false,
                                containerAbsolute: true,
                                bottomOffset: "inherit",
                                topOffset: "55px",
                                leftOffset: "0",
                                rightOffset: "0",
                                topTriangle: true,
                                center: true,
                                opacity: 1
                            });
                        }
                    }
                } catch { } // eslint-disable-line no-empty
            } else {
                Config.config.showDeArrowPromotion = false;
            }
        }
}

/**
 * Two upper case words (at least 2 letters long)
 */
function badTitle(title: string): boolean {
    return !!title.match(/\p{Lu}{2,} \p{Lu}{2,}[.!? ]/u);
}

export function hideDeArrowPromotion(): void {
    if (tooltip) tooltip.close();
}