aboutsummaryrefslogtreecommitdiff
path: root/src/file-magics.ts
blob: ebe4892512695e40c5df50f8e12368de59b41f6b (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
import isNodeJs from "detect-node";
import { hookNative } from "./anti-detection";
import type { FileType } from "./file";

const TYPE_REG = /type=(img|mp3|midi)/;
// first page has different URL
const INIT_PAGE_REG = /(score_0\.png@0|score_0\.svg)/;
const INDEX_REG = /index=(\d+)/;

export const auths = {};

(() => {
    if (isNodeJs) {
        // noop in CLI
        return () => Promise.resolve("");
    }

    try {
        const p = Object.getPrototypeOf(document.body);
        Object.setPrototypeOf(document.body, null);

        hookNative(document.body, "append", () => {
            return function (...nodes: Node[]) {
                p.append.call(this, ...nodes);

                if (nodes[0].nodeName === "IFRAME") {
                    const iframe = nodes[0] as HTMLIFrameElement;
                    const w = iframe.contentWindow as Window;

                    hookNative(w, "fetch", () => {
                        return function (url, init) {
                            let token = init?.headers?.Authorization;
                            if (
                                typeof url === "string" &&
                                (token || url.match(INIT_PAGE_REG))
                            ) {
                                let m = url.match(TYPE_REG);
                                let i = url.match(INDEX_REG);
                                if (m && i) {
                                    // console.log(url, token, m[1], i[1]);
                                    const type = m[1];
                                    const index = i[1];
                                    auths[type + index] = token;
                                } else if (url.match(INIT_PAGE_REG)) {
                                    auths["img0"] = url;
                                }
                            }
                            return fetch(url, init);
                        };
                    });
                }
            };
        });

        Object.setPrototypeOf(document.body, p);
    } catch (err) {
        console.error(err);
    }
})();