aboutsummaryrefslogtreecommitdiff
path: root/src/main.ts
blob: 8bf220d86ba5fa1ea9d416b29c8f6ac84f71da7f (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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import "./meta";

import FileSaver from "file-saver";
import { waitForSheetLoaded } from "./utils";
import { downloadPDF } from "./pdf";
import { getFileUrl } from "./file";
import { BtnList, BtnAction, BtnListMode } from "./btn";
import { ScoreInfoInPage, SheetInfoInPage } from "./scoreinfo";
import i18nextInit, { i18next } from "./i18n/index";
import { isGmAvailable, _GM } from "./gm";

(async () => {
    await i18nextInit;
})();

if (isGmAvailable("registerMenuCommand")) {
    // add buttons to the userscript manager menu
    _GM.registerMenuCommand(
        "** " +
            i18next.t("version", { version: _GM.info.script.version }) +
            " **",
        () =>
            _GM.openInTab(
                "https://github.com/LibreScore/dl-librescore/releases",
                {
                    active: true,
                }
            )
    );

    _GM.registerMenuCommand("** " + i18next.t("source_code") + " **", () =>
        _GM.openInTab(_GM.info.script.homepage, { active: true })
    );

    _GM.registerMenuCommand("** Discord **", () =>
        _GM.openInTab("https://discord.gg/DKu7cUZ4XQ", { active: true })
    );
}

const { saveAs } = FileSaver;

const main = (): void => {
    let isOfficial = false;
    new Promise(() => {
        const observer = new MutationObserver(() => {
            let noSub = Array.from(
                document.querySelectorAll("#jmuse-scroller-component div")
            ).some((img: HTMLDivElement) =>
                img.innerText.startsWith("End of preview")
            );
            if (!noSub) {
                noSub = document.querySelector("#jmuse-scroller-component")
                    ? false
                    : true;
            }
            noSub = false;
            const scoreinfo = new ScoreInfoInPage(document);
            const btnList = new BtnList();
            let indvPartBtn: HTMLButtonElement | null = null;
            const fallback = () => {
                // btns fallback to load from MSCZ file (`Individual Parts`)
                return indvPartBtn?.click();
            };

            if (
                noSub &&
                (document.querySelector(
                    "meta[property='musescore:author'][content='Official Scores']"
                ) ||
                    document.querySelector(
                        "meta[property='musescore:author'][content='Official Author']"
                    ))
            ) {
                const btnOffList = new BtnList();
                if (!isOfficial) {
                    btnOffList.add({
                        name:
                            i18next.t("download", { fileType: "PDF" }) +
                            "\n(" +
                            i18next.t("official_button") +
                            ")",
                        action: BtnAction.openUrl(
                            "https://musescore.com/upgrade"
                        ),
                        tooltip: i18next.t("official_tooltip"),
                    });
                    // eslint-disable-next-line @typescript-eslint/no-floating-promises
                    btnOffList.commit(BtnListMode.InPage, 2);
                }
            } else {
                observer.disconnect();
                if (isOfficial) {
                    document
                        .querySelector(".js-page")!
                        .shadowRoot!.querySelectorAll("#\\31, #\\32")
                        .forEach((e) => e.remove());
                    isOfficial = false;
                }
                btnList.add({
                    name: i18next.t("download", {
                        fileType: "PDF",
                    }),
                    action: BtnAction.process(
                        () =>
                            downloadPDF(
                                scoreinfo,
                                new SheetInfoInPage(document),
                                saveAs
                            ),
                        fallback,
                        3 * 60 * 1000 /* 3min */
                    ),
                });
            }

            if (!isOfficial) {
                isOfficial = true;
                btnList.add({
                    name: i18next.t("download", { fileType: "MIDI" }),
                    action: BtnAction.download(
                        () => getFileUrl(scoreinfo.id, "midi"),
                        fallback,
                        30 * 1000 /* 30s */
                    ),
                });

                btnList.add({
                    name: i18next.t("download", { fileType: "MP3" }),
                    action: BtnAction.download(
                        () => getFileUrl(scoreinfo.id, "mp3"),
                        fallback,
                        30 * 1000 /* 30s */
                    ),
                });
                // eslint-disable-next-line @typescript-eslint/no-floating-promises
                btnList.commit(BtnListMode.InPage, 1);
            }
        });
        observer.observe(
            document.querySelector("#jmuse-scroller-component") ?? document,
            {
                childList: true,
                subtree: true,
            }
        );
    });
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
waitForSheetLoaded().then(main);