aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/viewIcons.tsx
blob: a0d08a966a99f466a3cc082b912df686656f4301 (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
105
106
107
108
109
110
111
/*
 * Vencord, a modification for Discord's desktop app
 * Copyright (c) 2022 Vendicated and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import type { Guild } from "discord-types/general";

import { Devs } from "../utils/constants";
import { LazyComponent, lazyWebpack } from "../utils/misc";
import { ModalRoot, ModalSize, openModal } from "../utils/modal";
import { PluginDef } from "../utils/types";
import { filters, find } from "../webpack";
import { Menu } from "../webpack/common";

const ImageModal = LazyComponent(() => find(m => m.prototype?.render?.toString().includes("OPEN_ORIGINAL_IMAGE")));
const MaskedLink = LazyComponent(() => find(m => m.type?.toString().includes("MASKED_LINK)")));

const GuildBannerStore = lazyWebpack(filters.byProps("getGuildBannerURL"));

const OPEN_URL = "Vencord.Plugins.plugins.ViewIcons.openImage(";
export default new class ViewIcons implements PluginDef {
    name = "ViewIcons";
    authors = [Devs.Ven];
    description = "Makes Avatars/Banners in user profiles clickable, and adds Guild Context Menu Entries to View Banner/Icon.";

    dependencies = ["MenuItemDeobfuscatorAPI"];

    openImage(url: string) {
        const u = new URL(url);
        u.searchParams.set("size", "512");
        url = u.toString();

        openModal(modalProps => (
            <ModalRoot size={ModalSize.DYNAMIC} {...modalProps}>
                <ImageModal
                    shouldAnimate={true}
                    original={url}
                    src={url}
                    renderLinkComponent={() => <MaskedLink />}
                />
            </ModalRoot>
        ));
    }

    patches = [
        {
            find: "onAddFriend:",
            replacement: {
                // global because Discord has two components that are 99% identical with one small change ._.
                match: /\{src:(.{1,2}),avatarDecoration/g,
                replace: (_, src) => `{src:${src},onClick:()=>${OPEN_URL}${src}),avatarDecoration`
            }
        }, {
            find: "().popoutNoBannerPremium",
            replacement: {
                match: /style:.{0,10}\{\},(.{1,2})\)/,
                replace: (m, style) =>
                    `onClick:${style}.backgroundImage&&(${style}.cursor="pointer",` +
                    `()=>${OPEN_URL}${style}.backgroundImage.replace("url(", ""))),${m}`
            }
        }, {
            find: '"GuildContextMenu:',
            replacement: [
                {
                    match: /\w=(\w)\.id/,
                    replace: "_guild=$1,$&"
                },
                {
                    match: /(id:"leave-guild".{0,200}),(\(0,.{1,3}\.jsxs?\).{0,200}function)/,
                    replace: "$1,Vencord.Plugins.plugins.ViewIcons.buildGuildContextMenuEntries(_guild),$2"
                }
            ]
        }
    ];

    buildGuildContextMenuEntries(guild: Guild) {
        return (
            <Menu.MenuGroup>
                {guild.banner && (
                    <Menu.MenuItem
                        id="view-banner"
                        key="view-banner"
                        label="View Banner"
                        action={() => this.openImage(GuildBannerStore.getGuildBannerURL(guild))}
                    />
                )}
                {guild.icon && (
                    <Menu.MenuItem
                        id="view-icon"
                        key="view-icon"
                        label="View Icon"
                        action={() => this.openImage(guild.getIconURL(0, true))}
                    />
                )}
            </Menu.MenuGroup>
        );
    }
};