-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (167 loc) · 9.03 KB
/
index.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
import * as webpackModules from '@goosemod/webpack';
import * as patcher from '@goosemod/patcher';
// Webpacks
const channelsWebpack = webpackModules.findByProps("getMutableGuildChannels");
const getAllChannels = channelsWebpack.getMutableGuildChannels;
const getChannel = channelsWebpack.getChannel;
const currentUser = webpackModules.findByProps("getCurrentUser").getCurrentUser();
const checkPermission = webpackModules.findByProps("computePermissions").can;
const ChannelItem = webpackModules.find(m => m?.default?.displayName === 'ChannelItem');
const { getGuilds } = webpackModules.findByProps("getGuilds");
const FluxDispatcher = webpackModules.common.FluxDispatcher;
const { Permissions, ChannelTypes } = webpackModules.findByProps("Permissions","ChannelTypes");
// Webpacks to be patched
const getDefaultChannel = webpackModules.findByProps("getDefaultChannel");
const getCategories = webpackModules.findByProps("getCategories", "initialize");
const unreadManager = webpackModules.findByProps("hasUnread").__proto__;
const fetchMessages = webpackModules.findByProps("fetchMessages");
const originalFetch = Object.assign({}, fetchMessages).fetchMessages;
// Helper functions
const appendHiddenChannelNotice = () => {
const messagesWrapper = document.querySelector(`.${webpackModules.findByProps("messagesWrapper").messagesWrapper}`);
if (!messagesWrapper) return;
messagesWrapper.firstChild.style.display = "none";
if(messagesWrapper.firstChild.nextSibling) messagesWrapper.firstChild.nextSibling.style.display = "none";
messagesWrapper.parentElement.children[1].style.display = "none";
messagesWrapper.parentElement.parentElement.children[1].style.display = "none";
const toolbar = document.querySelector(`.${webpackModules.findByProps("toolbar", "selected").toolbar}`);
toolbar.style.display = "none";
const newMessage = document.createElement("div");
const txt = webpackModules.findByProps("h5");
const flex = webpackModules.findByProps("flex");
newMessage.className = flex.flexCenter;
newMessage.style.width = "100%";
newMessage.innerHTML = `
<div class="${flex.flex} ${flex.directionColumn} ${flex.alignCenter}">
<h2 class="${txt.h2} ${txt.defaultColor}">This is a hidden channel.</h2>
<h5 class="${txt.h5} ${txt.defaultColor}">You cannot see the contents of this channel. However, you may see its name and topic.</h5>
</div>`;
messagesWrapper.appendChild(newMessage);
}
const handleChannelChange = data => {
if (data.type !== "CHANNEL_SELECT" || !data.channelId) return;
if (!isChannelVisible(data.channelId)) setTimeout(appendHiddenChannelNotice);
}
const isChannelVisible = channelId => {
const channel = getChannel(channelId);
if(!channel || !channelId || [ChannelTypes.DM, ChannelTypes.GROUP_DM].includes(channel?.type)) return true;
return [ChannelTypes.GUILD_TEXT, ChannelTypes.GUILD_VOICE, ChannelTypes.GUILD_STAGE_VOICE, ChannelTypes.GUILD_ANNOUNCEMENT, ChannelTypes.ANNOUNCEMENT_THREAD, ChannelTypes.PRIVATE_THREAD, ChannelTypes.PUBLIC_THREAD].includes(channel?.type) && checkPermission(Permissions.VIEW_CHANNEL, currentUser, channel);
}
const hiddenChannelCache = Object.values(getGuilds()).reduce((cache, currentGuild) => {
cache[currentGuild.id] = {
channels: getDefaultChannel.getChannels(currentGuild.id).count,
hiddenChannels: [],
done: true
};
return cache;
}, {});
const cacheHiddenChannels = () => {
const fetchedChannels = Object.values(getAllChannels());
for(let channel of fetchedChannels) {
if (channel.type !== ChannelTypes.GUILD_CATEGORY && !isChannelVisible(channel.id))
hiddenChannelCache[channel.guild_id].hiddenChannels.push(channel);
};
}
const cacheServerHiddenChannels = (guildId, newHiddenChannels) => {
if(newHiddenChannels?.length > 0 && hiddenChannelCache[guildId]?.channels !== undefined) {
hiddenChannelCache[guildId].hiddenChannels.concat(newHiddenChannels);
return;
}
const channels = getDefaultChannel.getChannels(guildId);
if(hiddenChannelCache[guildId]?.channels > 0 && hiddenChannelCache[guildId]?.channels == channels.count) return;
hiddenChannelCache[guildId] = {
channels: channels.count,
hiddenChannels: [],
done: false
};
for(let channel of channels.SELECTABLE.concat(channels.VOCAL)) {
if (!isChannelVisible(channel?.id))
hiddenChannelCache[guildId].hiddenChannels.push(channel);
}
hiddenChannelCache[guildId].done = true;
}
const handleGuildJoin = (event) => {
cacheServerHiddenChannels(event.guild.id);
};
const handleGuildLeave = (event) => {
delete hiddenChannelCache[event.guild.id];
}
const handleChannelUpdate = (event) => {
cacheServerHiddenChannels(event?.updates?.[0]?.channel?.guild_id || event?.channel?.guild_id, event?.updates?.filter(x => !isChannelVisible(x.id)));
};
const handleChannelDelete = (event) => {
const guildId = event.channel.guild_id;
if(!hiddenChannelCache[guildId]) return cacheServerHiddenChannels(guildId);
hiddenChannelCache[guildId].hiddenChannels.filter(channel => channel?.id != event.channel.id)
hiddenChannelCache[guildId].channels -= 1;
}
// Unpatchers
const Unpatch = {}
// CSS stuff
const cssHeader = document.createElement('style');
cssHeader.id = 'hidden-channels-css';
cssHeader.textContent = `[aria-label$="hidden"] > div path[d^="M2"][d*="19"], [aria-label$="hidden"] > div path[d^="M17"][d*="19"]{ fill: #ed4245 !important; }`;
export default {
goosemodHandlers: {
onImport: async () => {
cacheHiddenChannels();
document.head.appendChild(cssHeader);
Unpatch.CSS = () => {
cssHeader.remove();
};
Unpatch.guildCreate = () => {FluxDispatcher.unsubscribe("GUILD_CREATE", handleGuildJoin)};
FluxDispatcher.subscribe("GUILD_CREATE", handleGuildJoin);
Unpatch.guildDelete = () => {FluxDispatcher.unsubscribe("GUILD_DELETE", handleGuildLeave)};
FluxDispatcher.subscribe("GUILD_DELETE", handleGuildLeave);
Unpatch.channelUpdate = () => {FluxDispatcher.unsubscribe("CHANNEL_UPDATES", handleChannelUpdate)};
FluxDispatcher.subscribe("CHANNEL_UPDATES", handleChannelUpdate);
Unpatch.channelCreate = () => {FluxDispatcher.unsubscribe("CHANNEL_CREATE", handleChannelUpdate)};
FluxDispatcher.subscribe("CHANNEL_CREATE", handleChannelUpdate);
Unpatch.channelDelete = () => {FluxDispatcher.unsubscribe("CHANNEL_DELETE", handleChannelDelete)};
FluxDispatcher.subscribe("CHANNEL_CREATE", handleChannelDelete);
Unpatch.channelSelect = () => {FluxDispatcher.unsubscribe("CHANNEL_SELECT", handleChannelChange)};
FluxDispatcher.subscribe("CHANNEL_SELECT", handleChannelChange);
Unpatch.getCategories = patcher.patch(getCategories, "getCategories", (originalArgs, previousReturn) => {
// originalArgs[0] is the server id
if(!hiddenChannelCache[originalArgs[0]]?.done) {
setTimeout(() => {
getCategories.getCategories(originalArgs[0])
}, 5000);
return previousReturn;
}
for(let channel of hiddenChannelCache[originalArgs[0]].hiddenChannels) {
if(!channel) return previousReturn;
const channelsInCategory = previousReturn[channel.parent_id || "null"];
if (channelsInCategory.filter((item) => item?.channel?.id === channel.id).length) return previousReturn;
channelsInCategory.push({ channel: channel, index: 0 });
};
return previousReturn;
});
Unpatch.ChannelItem = patcher.patch(ChannelItem, "default", (originalArgs) => {
// originalArgs[0] are the props
if(!isChannelVisible(originalArgs[0].channel.id)) originalArgs[0]["aria-label"] += " hidden";
return originalArgs;
}, true);
Unpatch.hasUnread = patcher.patch(unreadManager, "hasUnread", (originalArgs) => {
// originalArgs[0] is the channel id
if(!isChannelVisible(originalArgs[0])) originalArgs[0] = "";
return originalArgs;
}, true);
Unpatch.hasUnreadPins = patcher.patch(unreadManager, "hasUnreadPins", (originalArgs) => {
// originalArgs[0] is the channel id
if(!isChannelVisible(originalArgs[0])) return ["unread"];
return originalArgs;
}, true);
fetchMessages.fetchMessages = (originalArgs) => {
if(!isChannelVisible(originalArgs.channelId)) return;
return originalFetch(originalArgs);
}
Unpatch.fetchMessages = () => {
fetchMessages.fetchMessages = originalFetch;
}
},
onRemove: () => {
Object.values(Unpatch).forEach(unpatch => unpatch());
},
}
};