forked from noodlebox/betterdiscord-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwnerTag.plugin.js
387 lines (321 loc) · 12.5 KB
/
OwnerTag.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//META{"name":"ownerTag"}*//
/*@cc_on
@if (@_jscript)
// Offer to self-install for clueless users that try to run this directly.
var shell = WScript.CreateObject("WScript.Shell");
var fs = new ActiveXObject("Scripting.FileSystemObject");
var pathPlugins = shell.ExpandEnvironmentStrings("%APPDATA%\\BetterDiscord\\plugins");
var pathSelf = WScript.ScriptFullName;
// Put the user at ease by addressing them in the first person
shell.Popup("It looks like you mistakenly tried to run me directly. (don't do that!)", 0, "I'm a plugin for BetterDiscord", 0x30);
if (fs.GetParentFolderName(pathSelf) === fs.GetAbsolutePathName(pathPlugins)) {
shell.Popup("I'm in the correct folder already.\nJust reload Discord with Ctrl+R.", 0, "I'm already installed", 0x40);
} else if (!fs.FolderExists(pathPlugins)) {
shell.Popup("I can't find the BetterDiscord plugins folder.\nAre you sure it's even installed?", 0, "Can't install myself", 0x10);
} else if (shell.Popup("Should I copy myself to BetterDiscord's plugins folder for you?", 0, "Do you need some help?", 0x34) === 6) {
fs.CopyFile(pathSelf, fs.BuildPath(pathPlugins, fs.GetFileName(pathSelf)), true);
// Show the user where to put plugins in the future
shell.Exec("explorer " + pathPlugins);
shell.Popup("I'm installed!\nJust reload Discord with Ctrl+R.", 0, "Successfully installed", 0x40);
}
WScript.Quit();
@else @*/
var ownerTag = function () {};
(function () {
"use strict";
// This is super hackish, and will likely break as Discord's internal API changes
// Anything using this or what it returns should be prepared to catch some exceptions
const getInternalInstance = e => e[Object.keys(e).find(k => k.startsWith("__reactInternalInstance"))];
function getOwnerInstance(e, {include, exclude=["Popout", "Tooltip", "Scroller", "BackgroundFlash"]} = {}) {
if (e === undefined) {
return undefined;
}
// Set up filter; if no include filter is given, match all except those in exclude
const excluding = include === undefined;
const filter = excluding ? exclude : include;
// Get displayName of the React class associated with this element
// Based on getName(), but only check for an explicit displayName
function getDisplayName(owner) {
const type = owner._currentElement.type;
const constructor = owner._instance && owner._instance.constructor;
return type.displayName || constructor && constructor.displayName || null;
}
// Check class name against filters
function classFilter(owner) {
const name = getDisplayName(owner);
return (name !== null && !!(filter.includes(name) ^ excluding));
}
// Walk up the hierarchy until a proper React object is found
for (let prev, curr=getInternalInstance(e); !_.isNil(curr); prev=curr, curr=curr._hostParent) {
// Before checking its parent, try to find a React object for prev among renderedChildren
// This finds React objects which don't have a direct counterpart in the DOM hierarchy
// e.g. Message, ChannelMember, ...
if (prev !== undefined && !_.isNil(curr._renderedChildren)) {
/* jshint loopfunc: true */
let owner = Object.values(curr._renderedChildren)
.find(v => !_.isNil(v._instance) && v.getHostNode() === prev.getHostNode());
if (!_.isNil(owner) && classFilter(owner)) {
return owner._instance;
}
}
if (_.isNil(curr._currentElement)) {
continue;
}
// Get a React object if one corresponds to this DOM element
// e.g. .user-popout -> UserPopout, ...
let owner = curr._currentElement._owner;
if (!_.isNil(owner) && classFilter(owner)) {
return owner._instance;
}
}
return null;
}
function getInternalProps(e) {
if (e === undefined) {
return undefined;
}
try {
return getOwnerInstance(e).props;
} catch (err) {
return undefined;
}
}
// Get the relevant user ID for an element, or undefined
function getUserId(e) {
var props = getInternalProps(e);
if (props === undefined) {
return undefined;
}
try {
return props.user.id;
} catch (err) {
// Catch TypeError if no user in props
}
try {
return props.message.author.id;
} catch (err) {
// Catch TypeError if no message in props
}
return undefined;
}
// Get the relevant user color for an element, or undefined
function getUserColor(e) {
var props = getInternalProps(e);
if (props === undefined) {
return undefined;
}
try {
return props.message.colorString;
} catch (err) {
// Catch TypeError if no message in props
}
// Return colorString or undefined if not present
return props.colorString;
}
// Convert colorString to array of RGB values
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
] : null;
}
// Check whether colorString is brighter than middle gray
// See: https://stackoverflow.com/a/3943023/6807290
function isBright(color){
var c = hexToRgb(color);
return !!c && (c[0]*0.299 + c[1]*0.587 + c[2]*0.114) > 186;
}
// Get the relevant guild ID for an element, or undefined
function getGuildId(e) {
var owner = getOwnerInstance(e);
if (owner === undefined) {
return undefined;
}
try {
return owner.props.guild.id;
} catch (err) {
// Catch TypeError if no guild in props
}
try {
return owner.state.guild.id;
} catch (err) {
// Catch TypeError if no guild in state
}
return undefined;
}
// Get the relevant guild owner ID for an element, or undefined
function getOwnerId(e) {
var owner = getOwnerInstance(e);
if (owner === undefined) {
return undefined;
}
try {
return owner.props.guild.ownerId;
} catch (err) {
// Catch TypeError if no guild in props
}
try {
return owner.state.guild.ownerId;
} catch (err) {
// Catch TypeError if no guild in state
}
return undefined;
}
function addTag() {
/* jshint validthis: true */
var color = getUserColor(this);
var tag = $("<span>", {
class: "kawaii-tag",
}).text("OWNER");
if (color !== null) {
tag.css("background-color", color);
if (isBright(color)) {
tag.addClass("kawaii-tag-bright");
}
}
return tag;
}
var prevGuildId;
function processServer(mutation) {
var chat, guildId, ownerId, members, authors, tags;
chat = $(".chat")[0];
// Get the ID of the server
guildId = getGuildId(chat);
// Get the ID of the server's owner
ownerId = getOwnerId(chat);
if (ownerId === undefined) {
// (Probably) not looking at a server
return;
}
// Check if changed servers and need to redo member list tagging
// React likes to make minimal changes to the DOM, so owner tags
// will stick around (or not get added) when a user is in both this
// and the previous server.
if (guildId !== prevGuildId) {
// Get all visible members
members = $(".member-username-inner");
// Remove tags that were added
members.siblings(".kawaii-tag").remove();
members.filter(".kawaii-tagged").removeClass("kawaii-tagged");
} else {
members = mutationFind(mutation, ".member-username-inner");
}
// Get the set of message authors affected by this mutation
authors = mutationFind(mutation, ".user-name");
if (!authors.closest(".message-group").hasClass("compact")) {
// If not in compact mode, process the same as guild members
members = members.add(authors);
} else {
// Process authors (tag before of name in compact mode)
authors.filter((_, e) => getUserId(e) === ownerId).not(".kawaii-tagged")
.before(addTag)
.addClass("kawaii-tagged");
}
// Process guild members
members.filter((_, e) => getUserId(e) === ownerId).not(".kawaii-tagged")
.after(addTag)
.addClass("kawaii-tagged");
// User profile popout
tags = mutationFind(mutation, ".discord-tag")
.filter((_, e) => getUserId(e) === ownerId).not(".kawaii-tagged");
// Small popout
if (tags.parent(".username-wrapper").length > 0) {
tags.append($("<span>", {class: "kawaii-tag kawaii-tag-invert"})
.text("OWNER"))
.addClass("kawaii-tagged");
}
// Fullscreen popout (modal)
if (tags.parent(".header-info-inner").length > 0) {
tags.append($("<span>", {class: "kawaii-tag kawaii-tag"})
.text("OWNER"))
.addClass("kawaii-tagged");
}
prevGuildId = guildId;
}
function processProfile(mutation) {
var profile, userId, guilds;
profile = mutationFind(mutation, "#user-profile-modal");
userId = getUserId(profile[0]);
if (userId === undefined) {
// (Probably) not looking at a profile
return;
}
guilds = profile.find(".guild .avatar-large");
guilds.filter((_, e) => getOwnerId(e) === userId).parent().not(".kawaii-tagged")
.append($("<span>", {class: "kawaii-tag"}).text("OWNER"))
.addClass("kawaii-tagged");
}
// Helper function for finding all elements matching selector affected by a mutation
function mutationFind(mutation, selector) {
var target = $(mutation.target), addedNodes = $(mutation.addedNodes);
var mutated = target.add(addedNodes).filter(selector);
var descendants = addedNodes.find(selector);
var ancestors = target.parents(selector);
return mutated.add(descendants).add(ancestors);
}
// Default colors (https://discordapp.com/branding)
// #7289DA - "Blurple"
// #23272A - "Not quite black"
var css = `
.kawaii-tag {
background: #7289da;
font-size: 10px;
font-weight: 600;
color: #fff!important;
margin-left: 6px;
padding: 1px 2px;
border-radius: 3px;
text-transform: uppercase;
vertical-align: bottom;
line-height: 16px;
-ms-flex-negative: 0;
flex-shrink: 0;
}
.compact .kawaii-tag {
margin: 0 3px 0 0;
}
.kawaii-tag-bright {
color: #23272A!important;
}
.kawaii-tag-invert {
background: #fff;
color: #7289da!important;
}`;
ownerTag.prototype.start = function () {
BdApi.injectCSS("kawaii-tag-css", css);
// process entire document
var mutation = {target: document, addedNodes: [document]};
processServer(mutation);
processProfile(mutation);
};
ownerTag.prototype.observer = function (mutation) {
processServer(mutation);
processProfile(mutation);
};
ownerTag.prototype.load = function () {};
ownerTag.prototype.unload = function () {};
ownerTag.prototype.stop = function () {
BdApi.clearCSS("kawaii-tag-css");
// Remove tags that were added
$(".kawaii-tag").remove();
$(".kawaii-tagged").removeClass("kawaii-tagged");
};
ownerTag.prototype.getSettingsPanel = function () {
return "";
};
ownerTag.prototype.getName = function () {
return "Owner Tags";
};
ownerTag.prototype.getDescription = function () {
return "Show a tag next to a server owner's name";
};
ownerTag.prototype.getVersion = function () {
return "1.3.2";
};
ownerTag.prototype.getAuthor = function () {
return "noodlebox";
};
})();
/*@end @*/