aboutsummaryrefslogtreecommitdiff
path: root/src/file.ts
blob: 1fa7d8f560d3c860219576dde6d7c7092c525bca (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* eslint-disable no-extend-native */
/* eslint-disable @typescript-eslint/no-unsafe-return */

import md5 from "md5";
import { getFetch } from "./utils";
import { auths } from "./file-magics";

export type FileType = "img" | "mp3" | "midi";

const getSuffix = async (scoreUrl: string): Promise<string> => {
    let suffixUrl;
    if (scoreUrl !== "") {
        suffixUrl = (await (await fetch(scoreUrl)).text()).match(
            '<link href="(https://musescore.com/static/public/build/musescore.*?(?:_es6)?/20.+?.js)"'
        )?.[1]!;
    } else {
        const suffixElement =
            (document.head.querySelector(
                "link[href^='https://musescore.com/static/public/build/musescore_es6/20']"
            ) as HTMLLinkElement) ??
            (document.head.querySelector(
                "link[href^='https://musescore.com/static/public/build/musescore/20']"
            ) as HTMLLinkElement) ??
            (document.head.querySelector(
                "link[href^='https://musescore.com/static/public/build/musescore_fonts_es6/20']"
            ) as HTMLLinkElement) ??
            (document.head.querySelector(
                "link[href^='https://musescore.com/static/public/build/musescore_fonts/20']"
            ) as HTMLLinkElement) ??
            (document.head.querySelector(
                "link[href^='https://musescore.com/static/public/build/musescore_offwhite_colours_es6/20']"
            ) as HTMLLinkElement) ??
            (document.head.querySelector(
                "link[href^='https://musescore.com/static/public/build/musescore_offwhite_colours/20']"
            ) as HTMLLinkElement);
        suffixUrl = suffixElement?.href;
    }
    const suffixJs = await fetch(suffixUrl);
    return (await suffixJs.text()).match(
        '(?:.*)"(.+)"\\)\\.substr\\(0,4\\)'
    )?.[1]!;
};

const getApiUrl = (id: number, type: FileType, index: number): string => {
    return `/api/jmuse?id=${id}&type=${type}&index=${index}`;
};

const getApiAuth = async (
    id: number,
    type: FileType,
    index: number,
    scoreUrl: string
): Promise<string> => {
    const code = `${id}${type}${index}${await getSuffix(scoreUrl)}`;
    return md5(code).slice(0, 4);
};

let imgInProgress = false;

const getApiAuthNetwork = async (
    type: FileType,
    index: number
): Promise<string> => {
    let numPages = 0;
    let pageCooldown = 25;
    if (!auths[type + index]) {
        try {
            switch (type) {
                case "midi": {
                    const fsBtn = document.querySelector(
                        'button[title="Toggle Fullscreen"]'
                    ) as HTMLButtonElement;
                    if (!fsBtn) {
                        throw Error;
                    }
                    const el =
                        fsBtn.parentElement?.parentElement?.querySelector(
                            "button"
                        ) as HTMLButtonElement;
                    el.click();
                    break;
                }
                case "mp3": {
                    const el = document.querySelector(
                        'button[title="Toggle Play"]'
                    ) as HTMLButtonElement;
                    el.click();
                    break;
                }
                case "img": {
                    if (!imgInProgress) {
                        imgInProgress = true;
                        let parentDiv = document.querySelector(
                            "#jmuse-scroller-component"
                        )!;

                        numPages = parentDiv.children.length - 3;
                        let i = 0;

                        function scrollToNextChild() {
                            let childDiv = parentDiv.children[i];
                            if (childDiv) {
                                childDiv.scrollIntoView();
                            }

                            i++;

                            if (i < numPages) {
                                setTimeout(scrollToNextChild, pageCooldown);
                            }
                        }

                        scrollToNextChild();
                    }
                    imgInProgress = false;
                    break;
                }
            }
        } catch (err) {
            console.error(err);
            throw Error;
        }
    }

    try {
        return new Promise((resolve, reject) => {
            let timer = setTimeout(
                () => {
                    reject(new Error("token timeout"));
                },
                type === "img"
                    ? numPages * pageCooldown * 2 + 2100
                    : 5 * 1000 /* 5s */
            );

            // Check the auths object periodically
            let interval = setInterval(() => {
                if (auths.hasOwnProperty(type + index)) {
                    clearTimeout(timer);
                    clearInterval(interval);
                    setInterval(
                        () => {
                            resolve(auths[type + index]);
                        },
                        // long delay for images to give time for them to load fully
                        type === "img" ? 2000 : 100
                    );
                }
            }, 100);
        });
    } catch {
        console.error(type, "token timeout");
        throw Error;
    }
};

export const getFileUrl = async (
    id: number,
    type: FileType,
    scoreUrl = "",
    index = 0,
    _fetch = getFetch()
): Promise<string> => {
    const url = getApiUrl(id, type, index);
    let auth = await getApiAuth(id, type, index, scoreUrl);

    let r = await _fetch(url, {
        headers: {
            Authorization: auth,
        },
    });

    if (!r.ok) {
        auth = md5(`${id}${type}${index}01000`).slice(0, 4);
        r = await _fetch(url, {
            headers: {
                Authorization: auth,
            },
        });

        if (!r.ok) {
            auth = await getApiAuthNetwork(type, index);
            if (type === "img" && index === 0) {
                // auth is the URL for the first page
                r = await _fetch(auth);
            } else {
                r = await _fetch(url, {
                    headers: {
                        Authorization: auth,
                    },
                });
            }
        }
    }

    const { info } = await r.json();
    return info.url as string;
};