forked from Metalloriff/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvatarIconViewer.plugin.js
169 lines (150 loc) · 5.21 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
/**
* @name AvatarIconViewer
* @invite yNqzuJa
* @authorLink https://github.com/Metalloriff
* @donate https://www.paypal.me/israelboone
* @website https://metalloriff.github.io/toms-discord-stuff/
* @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.0",
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, ModalStack } = DiscordModules
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();
}
onStart()
{
const modules = WebpackModules.findAll(m => m.default && m.default.displayName && (m.default.displayName.endsWith("UserContextMenu") || m.default.displayName == "GroupDMContextMenu"));
const GuildContextMenu = WebpackModules.find(m => m.default && m.default.displayName == "GuildContextMenu");
const MessageContextMenu = WebpackModules.find(m => m.default && m.default.displayName == "MessageContextMenu");
for (const m of modules)
{
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));
});
}
Patcher.after(GuildContextMenu, "default", (_, [props], re) =>
{
const url = formatURL(props.guild.getIconURL());
if (url)
re.props.children.push(this.createContext(url, "Icon"));
});
Patcher.after(MessageContextMenu, "default", (_, [props], re) =>
{
if (props.target && props.target.src)
{
re.props.children.push(this.createContext(props.target.src, "Emoji"));
}
});
}
createContext(url, type)
{
return React.createElement(ContextMenu.MenuGroup,
{
children:
[
React.createElement(
ContextMenu.MenuItem,
{
label: "View " + type,
id: "aiv-view",
action: () =>
ModalStack.push(
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();
}
}
};
return plugin(Plugin, Api);
})(global.ZeresPluginLibrary.buildPlugin(config));
})();