aboutsummaryrefslogtreecommitdiff
path: root/src/webpack/index.ts
blob: 2c5a4557f6c3aaad2fa6f5f3d9f3d85673198c93 (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
96
97
98
99
100
101
102
103
104
import { startAll } from "../plugins";

let webpackCache: typeof window.webpackChunkdiscord_app;

export const subscriptions = new Map<FilterFn, CallbackFn>();
export const listeners = new Set<CallbackFn>();

type FilterFn = (mod: any) => boolean;
type CallbackFn = (mod: any) => void;

export let React: typeof import("react");
export let FluxDispatcher: any;
export let Forms: any;
export let UserStore: any;

export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) {
    if (webpackCache !== void 0) throw "no.";

    webpackCache = instance.push([[Symbol()], {}, (r) => r.c]);
    instance.pop();

    // Abandon Hope All Ye Who Enter Here

    let started = false;
    waitFor("getCurrentUser", x => UserStore = x);
    waitFor(["dispatch", "subscribe"], x => {
        FluxDispatcher = x;
        const cb = () => {
            console.info("Connection open");
            x.unsubscribe("CONNECTION_OPEN", cb);
            startAll();
        };
        x.subscribe("CONNECTION_OPEN", cb);
    });
    waitFor("useState", x => (React = x));
    waitFor("FormSection", x => Forms = x);
}

export function find(filter: FilterFn, getDefault = true) {
    if (typeof filter !== "function")
        throw new Error("Invalid filter. Expected a function got", filter);

    for (const key in webpackCache) {
        const mod = webpackCache[key];
        if (mod?.exports && filter(mod.exports))
            return mod.exports;
        if (mod?.exports?.default && filter(mod.exports.default))
            return getDefault ? mod.exports.default : mod.exports;
    }

    return null;
}

export function findAll(filter: FilterFn, getDefault = true) {
    if (typeof filter !== "function") throw new Error("Invalid filter. Expected a function got", filter);

    const ret = [] as any[];
    for (const key in webpackCache) {
        const mod = webpackCache[key];
        if (mod?.exports && filter(mod.exports)) ret.push(mod.exports);
        if (mod?.exports?.default && filter(mod.exports.default)) ret.push(getDefault ? mod.exports.default : mod.exports);
    }

    return ret;
}

export const filters = {
    byProps: (props: string[]): FilterFn =>
        props.length === 1
            ? m => m[props[0]] !== void 0
            : m => props.every(p => m[p] !== void 0),
    byDisplayName: (deezNuts: string): FilterFn => m => m.default?.displayName === deezNuts
};

export function findByProps(...props: string[]) {
    return find(filters.byProps(props));
}

export function findAllByProps(...props: string[]) {
    return findAll(filters.byProps(props));
}

export function findByDisplayName(deezNuts: string) {
    return find(filters.byDisplayName(deezNuts));
}

export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn) {
    if (typeof filter === "string") filter = filters.byProps([filter]);
    else if (Array.isArray(filter)) filter = filters.byProps(filter);
    else if (typeof filter !== "function") throw new Error("filter must be a string, string[] or function, got", filter);

    const existing = find(filter!);
    if (existing) return void callback(existing);

    subscriptions.set(filter, callback);
}

export function addListener(callback: CallbackFn) {
    listeners.add(callback);
}

export function removeListener(callback: CallbackFn) {
    listeners.delete(callback);
}