-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordOwnerTag.user.js
246 lines (206 loc) · 8.47 KB
/
DiscordOwnerTag.user.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
// ==UserScript==
// @name DiscordOwnerTag
// @namespace https://files.noodlebox.moe/
// @downloadURL https://files.noodlebox.moe/userscripts/DiscordOwnerTag.user.js
// @version 1.2.4
// @description Show a tag next to a Discord server owner's name
// @author noodlebox
// @require https://code.jquery.com/jquery-3.1.1.min.js
// @require https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js
// @match *://*.discordapp.com/channels/*
// @match *://*.discordapp.com/invite/*
// @match *://*.discordapp.com/login
// @run-at document-idle
// @grant none
// ==/UserScript==
(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;
}
// Get the relevant guild ID for an element, or undefined
function getGuildId(e) {
var props = getInternalProps(e);
if (props === undefined) {
return undefined;
}
try {
return props.guild.id;
} catch (err) {
// Catch TypeError if no guild in props
}
return undefined;
}
// Get the relevant guild owner ID for an element, or undefined
function getOwnerId(e) {
var props = getInternalProps(e);
if (props === undefined) {
return undefined;
}
try {
return props.guild.ownerId;
} catch (err) {
// Catch TypeError if no guild in props
}
return undefined;
}
var prevGuildId;
function processServer(mutation) {
var guild, guildId, ownerId, usernames, tags;
guild = $(".guild.selected")[0];
// Get the ID of the server
guildId = getGuildId(guild);
// Get the ID of the server's owner
ownerId = getOwnerId(guild);
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
usernames = $(".member-username-inner");
// Remove tags that were added
usernames.siblings(".kawaii-tag").remove();
usernames.filter(".kawaii-tagged").removeClass("kawaii-tagged");
// Add the set of message authors affected by this mutation
usernames = usernames.add(mutationFind(mutation, ".user-name"));
} else {
// Get the set of message authors and server members affected by this mutation
usernames = mutationFind(mutation, ".member-username-inner, .user-name");
}
// Process usernames
usernames.filter((_, e) => getUserId(e) === ownerId).not(".kawaii-tagged").after(function () {
var color = getUserColor(this);
var tag = $("<span>", {
class: "bot-tag kawaii-tag",
}).text("OWNER");
if (color !== null) {
tag.css("background-color", color);
}
return tag;
}).addClass("kawaii-tagged");
tags = mutationFind(mutation, ".discord-tag");
tags.filter((_, e) => getUserId(e) === ownerId).not(".kawaii-tagged")
.append($("<span>", {class: "bot-tag bot-tag-invert 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: "bot-tag 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);
}
// Watch for new usernames
new MutationObserver(function (mutations, observer) {
mutations.forEach(function (mutation) {
processServer(mutation);
processProfile(mutation);
});
}).observe(document, { childList:true, subtree:true });
})(jQuery.noConflict(true), _.noConflict());