-
Notifications
You must be signed in to change notification settings - Fork 356
/
AvatarIconViewer.plugin.js
202 lines (177 loc) · 6.7 KB
/
AvatarIconViewer.plugin.js
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
199
200
201
202
/**
* @name AvatarIconViewer
* @invite yNqzuJa
* @authorLink https://discord.com/users/264163473179672576
* @donate https://www.paypal.me/israelboone
* @website https://kinzoku.one/
* @source https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/AvatarIconViewer.plugin.js
*/
module.exports = (() => {
const config =
{
info:
{
name: "AvatarIconViewer",
authors:
[
{
name: "Metalloriff",
discord_id: "264163473179672576",
github_username: "metalloriff",
twitter_username: "Metalloriff"
}
],
version: "2.0.2",
description: "Allows you to view server icons, user avatars, and emotes in fullscreen via the context menu, or copy the link to them.",
github: "https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/AvatarIconViewer.plugin.js",
github_raw: "https://raw.githubusercontent.com/Metalloriff/BetterDiscordPlugins/master/AvatarIconViewer.plugin.js"
}
};
return !global.ZeresPluginLibrary ? class {
constructor() { this._config = config; }
getName = () => config.info.name;
getAuthor = () => config.info.description;
getVersion = () => config.info.version;
load() {
BdApi.showConfirmationModal("Library Missing", `The library plugin needed for ${config.info.name} is missing. Please click Download Now to install it.`, {
confirmText: "Download Now",
cancelText: "Cancel",
onConfirm: () => {
require("request").get("https://rauenzi.github.io/BDPluginLibrary/release/0PluginLibrary.plugin.js", async (err, res, body) => {
if (err) return require("electron").shell.openExternal("https://betterdiscord.net/ghdl?url=https://raw.githubusercontent.com/rauenzi/BDPluginLibrary/master/release/0PluginLibrary.plugin.js");
await new Promise(r => require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0PluginLibrary.plugin.js"), body, r));
});
}
});
}
start() { }
stop() { }
} : (([Plugin, Api]) => {
const plugin = (Plugin, Api) => {
const { DiscordModules, WebpackModules, Patcher } = Api;
const { React } = DiscordModules
const ModalStack = WebpackModules.getByProps("openModal", "hasModalOpen");
const { ModalRoot } = WebpackModules.getByProps("ModalRoot");
const { ModalSize } = WebpackModules.getByProps("ModalSize");
const ImageModal = WebpackModules.getByDisplayName("ImageModal");
const ContextMenu = WebpackModules.getByProps("MenuItem");
const MaskedLink = WebpackModules.getByDisplayName("MaskedLink");
const getChannelIconURL = WebpackModules.getByProps("getChannelIconURL").getChannelIconURL;
const copyToClipboard = require("electron").clipboard.writeText;
const formatURL = url =>
url == null || url.length == 0
? null
: (url.includes("/a_")
? url.replace(".webp", ".gif").replace(".png", ".gif")
: url).split("?")[0] + "?size=2048";
return class AvatarIconViewer extends Plugin {
constructor() {
super();
}
async onStart() {
// I have no idea why, nor do I have the energy to figure out why,
// but shitcord does not load the context menu modules until the context menu is opened.
// Therefore, this shitty workaround was made, which requires the user to
// open the context menu once before the plugin will work.
// UPDATE: It's less spaghetti, but still spaghetti. I'm sorry.
this.patched = [];
document.getElementById("app-mount").addEventListener("contextmenu", this.contextMenuListener = e => {
const modules = WebpackModules.findAll(
m => m.default && m.default.displayName && (
m.default.displayName.endsWith("UserContextMenu") || ~[
"GroupDMContextMenu",
"GuildContextMenu",
"MessageContextMenu"
].indexOf(m.default.displayName)
)
);
for (const m of modules) {
if (~this.patched.indexOf(m.default.displayName)) {
continue;
}
switch (m.default.displayName) {
default: {
Patcher.after(m, "default", (_, [props], re) => {
const type = props.user ? "Avatar" : props.channel && props.channel.type == 3 ? "Icon" : null;
const url = formatURL(type == "Avatar" ? props.user.getAvatarURL() : type == "Icon" ? getChannelIconURL(props.channel) : null);
if (type && url)
re.props.children.props.children.push(this.createContext(url, type));
});
} break;
case "GuildContextMenu": {
Patcher.after(m, "default", (_, [props], re) => {
const url = formatURL(props.guild.getIconURL());
if (url)
re.props.children.push(this.createContext(url, "Icon"));
});
} break;
case "MessageContextMenu": {
Patcher.after(m, "default", (_, [props], re) => {
if (props.target && props.target.src) {
re.props.children.push(this.createContext(props.target.src, "Emoji"));
}
});
} break;
}
this.patched.push(m.default.displayName);
}
});
}
createContext(url, type) {
const ClassModule = WebpackModules.getByProps("modal", "image");
return React.createElement(ContextMenu.MenuGroup,
{
children:
[
React.createElement(
ContextMenu.MenuItem,
{
label: "View " + type,
id: "aiv-view",
action: () =>
ModalStack.openModal(
props => (
React.createElement(
ModalRoot,
{
className: ClassModule.modal,
...props,
size: ModalSize.DYNAMIC
},
React.createElement(
ImageModal,
{
src: url,
placeholder: url,
original: url,
width: 2048,
height: 2048,
onClickUntrusted: e => e.openHref(),
renderLinkComponent: props => React.createElement(MaskedLink, props)
}
)
)
)
)
}
),
React.createElement(
ContextMenu.MenuItem,
{
label: "Copy " + type + " Link",
id: "aiv-copy",
action: () => copyToClipboard(url)
}
)
]
});
}
onStop() {
Patcher.unpatchAll();
document.getElementById("app-mount").removeEventListener("contextmenu", this.contextMenuListener);
}
}
};
return plugin(Plugin, Api);
})(global.ZeresPluginLibrary.buildPlugin(config));
})();