BetterDiscordAddons/Plugins/ReadAllNotificationsButton/ReadAllNotificationsButton....

423 lines
16 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"ReadAllNotificationsButton","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ReadAllNotificationsButton","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ReadAllNotificationsButton/ReadAllNotificationsButton.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class ReadAllNotificationsButton {
2019-01-17 23:48:29 +01:00
getName () {return "ReadAllNotificationsButton";}
2019-12-30 11:52:37 +01:00
getVersion () {return "1.5.4";}
2019-01-17 23:48:29 +01:00
getAuthor () {return "DevilBro";}
2019-01-26 22:45:19 +01:00
2019-01-17 23:48:29 +01:00
getDescription () {return "Adds a button to clear all notifications.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
this.changelog = {
2019-12-30 11:48:34 +01:00
"fixed":[["Clear Mentions","Clear Mentions buton is now properly added again"]],
2019-11-13 11:14:54 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
};
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this.patchedModules = {
after: {
Guilds: "render",
2019-12-30 11:48:34 +01:00
MessagesPopout: "render"
2019-11-14 17:56:26 +01:00
}
};
2019-09-04 12:34:02 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
initConstructor () {
2019-04-23 14:37:56 +02:00
this.css = `
${BDFDB.dotCN._readallnotificationsbuttonframe} {
2019-04-23 14:37:56 +02:00
margin-bottom: 10px;
}
${BDFDB.dotCN._readallnotificationsbuttonbutton} {
2019-04-23 14:37:56 +02:00
cursor: pointer;
border-radius: 4px;
font-size: 12px;
line-height: 1.3;
}
`;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.defaults = {
settings: {
2019-02-08 21:33:47 +01:00
addClearButton: {value:true, inner:false, description:"Adds a 'Clear Mentions' button to the recent mentions popout"},
includeGuilds: {value:true, inner:true, description:"unread Servers"},
includeMuted: {value:false, inner:true, description:"muted unread Servers"},
includeDMs: {value:false, inner:true, description:"unread DMs"}
2018-10-11 10:21:26 +02:00
}
};
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getSettingsPanel () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
2019-10-22 23:04:35 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-12-18 16:45:08 +01:00
let settingspanel, settingsitems = [], inneritems = [];
2019-10-21 12:11:46 +02:00
2019-11-01 22:47:23 +01:00
for (let key in settings) (!this.defaults.settings[key].inner ? settingsitems : inneritems).push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2019-10-21 12:11:46 +02:00
className: BDFDB.disCN.marginbottom8,
2019-11-01 22:47:23 +01:00
type: "Switch",
2019-10-21 12:11:46 +02:00
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key],
disabled: key == "includeMuted" && !settings.includeGuilds,
onChange: (value, instance) => {
if (key != "includeGuilds") return;
let mutedSwitchIns = BDFDB.ReactUtils.findOwner(instance, {props:[["keys",["settings", "includeMuted"]]]});
if (mutedSwitchIns) {
mutedSwitchIns.props.disabled = !value;
BDFDB.ReactUtils.forceUpdate(mutedSwitchIns);
}
}
}));
2019-10-22 18:55:25 +02:00
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelInner, {
2019-10-21 12:11:46 +02:00
title: "When left clicking the 'read all' button mark following Elements as read:",
2019-11-05 08:56:21 +01:00
first: settingsitems.length == 0,
2019-11-05 08:59:26 +01:00
last: true,
2019-10-21 12:11:46 +02:00
children: inneritems
}));
2019-12-18 16:45:08 +01:00
return settingspanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
2018-10-11 10:21:26 +02:00
}
//legacy
load () {}
start () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
2020-01-21 12:56:26 +01:00
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
2019-05-26 13:55:26 +02:00
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2018-10-11 10:21:26 +02:00
libraryScript.setAttribute("type", "text/javascript");
2019-10-18 10:56:41 +02:00
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
2019-01-17 23:48:29 +01:00
libraryScript.setAttribute("date", performance.now());
2020-01-14 00:06:07 +01:00
libraryScript.addEventListener("load", _ => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
}
2020-01-17 19:50:31 +01:00
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2020-01-14 00:06:07 +01:00
this.startTimeout = setTimeout(_ => {
2019-11-01 10:27:07 +01:00
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
2018-10-11 10:21:26 +02:00
}
initialize () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-22 11:05:54 +01:00
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01:00
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
2018-10-11 10:21:26 +02:00
}
stop () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-11-13 11:14:54 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
// begin of own functions
2019-01-26 22:45:19 +01:00
2019-11-28 09:48:57 +01:00
onUserContextMenu (e) {
if (e.instance.props.channelId && e.instance.props.type == BDFDB.DiscordConstants.ContextMenuTypes.USER_PRIVATE_CHANNELS) {
2020-01-05 17:46:10 +01:00
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name: BDFDB.LibraryComponents.ContextMenuItems.Group});
if (index > -1) this.injectItem(children, e.instance.props.channelId);
}
}
onGroupDMContextMenu (e) {
if (e.instance.props.channelId) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name: BDFDB.LibraryComponents.ContextMenuItems.Group});
if (index > -1) this.injectItem(children, e.instance.props.channelId);
}
}
injectItem (children, channelId) {
children.unshift(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Group, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-11-28 09:48:57 +01:00
label: BDFDB.LanguageUtils.LanguageStrings.MARK_AS_READ,
2020-01-05 17:46:10 +01:00
disabled: !BDFDB.LibraryModules.DirectMessageUnreadStore.getUnreadPrivateChannelIds().includes(channelId),
2019-11-28 09:48:57 +01:00
action: event => {
BDFDB.ContextMenuUtils.close(event.target);
2020-01-05 17:46:10 +01:00
BDFDB.DMUtils.markAsRead(channelId);
2019-11-28 09:48:57 +01:00
}
2020-01-05 17:46:10 +01:00
})
}));
2019-11-28 09:48:57 +01:00
}
2019-11-13 11:14:54 +01:00
processGuilds (e) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name: "ConnectedUnreadDMs"});
if (index > -1) children.splice(index + 1, 0, BDFDB.ReactUtils.createElement("div", {
2019-12-30 11:52:37 +01:00
className: BDFDB.disCNS.guildouter + BDFDB.disCN._readallnotificationsbuttonframe,
2019-11-13 11:14:54 +01:00
style: {height: 20},
children: BDFDB.ReactUtils.createElement("div", {
2019-12-30 11:52:37 +01:00
className: BDFDB.disCNS.guildiconwrapper + BDFDB.disCN._readallnotificationsbuttoninner,
2019-11-13 11:14:54 +01:00
style: {height: 20},
children: BDFDB.ReactUtils.createElement("div", {
2019-12-30 11:52:37 +01:00
className: BDFDB.disCNS.guildiconchildwrapper + BDFDB.disCN._readallnotificationsbuttonbutton,
2019-11-13 11:14:54 +01:00
style: {height: 20},
children: "read all",
onClick: _ => {
let settings = BDFDB.DataUtils.get(this, "settings");
if (settings.includeGuilds) BDFDB.GuildUtils.markAsRead(settings.includeMuted ? BDFDB.GuildUtils.getAll() : BDFDB.GuildUtils.getUnread());
if (settings.includeDMs) BDFDB.DMUtils.markAsRead(BDFDB.DMUtils.getAll());
},
onContextMenu: event => {
2019-12-05 09:10:14 +01:00
BDFDB.ContextMenuUtils.open(this, event, BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Group, {
2019-11-13 11:14:54 +01:00
children: [
2019-12-05 09:10:14 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-11-13 11:14:54 +01:00
label: this.labels.context_unreadguilds_text,
action: event2 => {
BDFDB.ContextMenuUtils.close(event2._targetInst);
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getUnread());
}
}),
2019-12-05 09:10:14 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-11-13 11:14:54 +01:00
label: this.labels.context_pingedguilds_text,
action: event2 => {
BDFDB.ContextMenuUtils.close(event2._targetInst);
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getPinged());
}
}),
2019-12-05 09:10:14 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-11-13 11:14:54 +01:00
label: this.labels.context_mutedguilds_text,
action: event2 => {
BDFDB.ContextMenuUtils.close(event2._targetInst);
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getMuted());
}
}),
2019-12-05 09:10:14 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-11-13 11:14:54 +01:00
label: this.labels.context_guilds_text,
action: event2 => {
BDFDB.ContextMenuUtils.close(event2._targetInst);
this.addPinnedRecent(instance.props.channel.id);
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getAll());
}
}),
2019-12-05 09:10:14 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-11-13 11:14:54 +01:00
label: this.labels.context_dms_text,
action: event2 => {
BDFDB.ContextMenuUtils.close(event2._targetInst);
BDFDB.DMUtils.markAsRead(BDFDB.DMUtils.getAll());
}
})
]
}));
}
})
})
}));
}
2019-12-30 11:48:34 +01:00
processMessagesPopout (e) {
if (e.instance.props.className == BDFDB.disCN.recentmentionspopout && BDFDB.DataUtils.get(this, "settings", "addClearButton")) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name: "RecentMentionsHeader"});
if (index > -1) children.splice(index, 0, BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Button, {
look: BDFDB.LibraryComponents.Button.Looks.OUTLINED,
color: BDFDB.LibraryComponents.Button.Colors.GREY,
hover: BDFDB.LibraryComponents.Button.Hovers.RED,
size: BDFDB.LibraryComponents.Button.Sizes.SMALL,
children: BDFDB.LanguageUtils.LanguageStrings.REMOVE,
onClick: (event, buttoninstance) => {
this.clearMentions(e.instance, BDFDB.DOMUtils.getParent(BDFDB.dotCN.messagespopoutwrap, BDFDB.ReactUtils.findDOMNode(buttoninstance)));
},
style: {
position: "absolute",
top: 8,
right: 20
}
}));
}
}
2019-01-26 22:45:19 +01:00
clearMentions (instance, wrapper) {
2019-12-30 11:48:34 +01:00
if (!instance || !Node.prototype.isPrototypeOf(wrapper) || !document.contains(wrapper)) return;
let closebuttons = wrapper.querySelectorAll(BDFDB.dotCN.messagespopoutclosebutton);
for (let btn of wrapper.querySelectorAll(BDFDB.dotCN.messagespopoutclosebutton)) btn.click();
if (closebuttons.length) {
2019-12-30 11:48:34 +01:00
instance.props.loadMore();
BDFDB.TimeUtils.timeout(_ => {this.clearMentions(instance, wrapper);},3000);
}
2019-03-07 23:56:18 +01:00
}
2019-09-04 12:34:02 +02:00
2019-03-07 23:56:18 +01:00
setLabelsByLanguage () {
2019-10-24 11:47:57 +02:00
switch (BDFDB.LanguageUtils.getLanguage().id) {
case "hr": //croatian
return {
2019-10-21 12:11:46 +02:00
context_unreadguilds_text: "Nepročitani poslužitelje",
context_pingedguilds_text: "Zvižduci poslužitelje",
context_mutedguilds_text: "Prigušeni poslužitelje",
context_guilds_text: "Sve poslužitelje",
context_dms_text: "Prikvacene izravne"
};
case "da": //danish
return {
context_unreadguilds_text: "Ulæste servere",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Pinget servere",
context_mutedguilds_text: "Dæmpede servere",
context_guilds_text: "Alle servere",
context_dms_text: "Private beskeder"
};
case "de": //german
return {
context_unreadguilds_text: "Ungelesene Server",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Gepingte Server",
2019-03-29 19:50:03 +01:00
context_mutedguilds_text: "Stummgeschaltene Server",
context_guilds_text: "Alle Server",
context_dms_text: "Direktnachrichten"
};
case "es": //spanish
return {
context_unreadguilds_text: "Servidores no leídos",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Servidores mencionados",
context_mutedguilds_text: "Servidores silenciados",
context_guilds_text: "Todos los servidores",
context_dms_text: "Mensajes directos"
};
case "fr": //french
return {
context_unreadguilds_text: "Serveurs non lus",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Serveurs mentionnés",
context_mutedguilds_text: "Serveurs en sourdine",
context_guilds_text: "Tous les serveurs",
context_dms_text: "Messages privés"
};
case "it": //italian
return {
context_unreadguilds_text: "Server non letti",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Server pingato",
context_mutedguilds_text: "Server mutate",
context_guilds_text: "Tutti i server",
context_dms_text: "Messaggi diretti"
};
case "nl": //dutch
return {
context_unreadguilds_text: "Ongelezen servers",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Gepingde servers",
context_mutedguilds_text: "Gedempte servers",
context_guilds_text: "Alle servers",
context_dms_text: "Prive berichten"
};
case "no": //norwegian
return {
context_unreadguilds_text: "Uleste servere",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Pinget servere",
context_mutedguilds_text: "Dempet servere",
context_guilds_text: "Alle servere",
context_dms_text: "Direktemeldinger"
};
case "pl": //polish
return {
context_unreadguilds_text: "Nieprzeczytane serwery",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Pingowany serwery",
context_mutedguilds_text: "Wyciszone serwery",
context_guilds_text: "Wszystkie serwery",
context_dms_text: "Prywatne wiadomości"
};
case "pt-BR": //portuguese (brazil)
return {
context_unreadguilds_text: "Servidores não lidos",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Servidores com ping",
context_mutedguilds_text: "Servidores silenciosos",
context_guilds_text: "Todos os servidores",
context_dms_text: "Mensagens diretas"
};
case "fi": //finnish
return {
context_unreadguilds_text: "Lukemattomia palvelimet",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Tapitut palvelimet",
context_mutedguilds_text: "Mykistetyt palvelimet",
context_guilds_text: "Kaikki palvelimet",
context_dms_text: "Yksityisviestit"
};
case "sv": //swedish
return {
context_unreadguilds_text: "Olästa servrar",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Pingade servrar",
context_mutedguilds_text: "Dämpade servrar",
context_guilds_text: "Alla servrar",
context_dms_text: "Direktmeddelanden"
};
case "tr": //turkish
return {
context_unreadguilds_text: "Okunmamış sunucular",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Ping sunucular",
context_mutedguilds_text: "Sessiz sunucular",
context_guilds_text: "Tüm sunucular",
context_dms_text: "Özel mesajlar"
};
case "cs": //czech
return {
context_unreadguilds_text: "Nepřečtené servery",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Pinged servery",
context_mutedguilds_text: "Tlumené servery",
context_guilds_text: "Všechny servery",
context_dms_text: "Přímé zpráva"
};
case "bg": //bulgarian
return {
context_unreadguilds_text: "Непрочетени сървъри",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Споменатите сървъри",
context_mutedguilds_text: "Приглушени сървъри",
context_guilds_text: "Всички сървъри",
context_dms_text: "Директно съобщение"
};
case "ru": //russian
return {
context_unreadguilds_text: "Непрочитанные серверы",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Проверенные серверы",
context_mutedguilds_text: "Отключенные серверы",
context_guilds_text: "Все серверы",
context_dms_text: "Прямые сообщения"
};
case "uk": //ukrainian
return {
2019-10-21 12:11:46 +02:00
context_unreadguilds_text: "Непрочитаних сервери",
context_pingedguilds_text: "Згадані сервери",
context_mutedguilds_text: "Приглушені сервери",
context_guilds_text: "Всі сервери",
context_dms_text: "Прямі Повідомлення"
};
case "ja": //japanese
return {
context_unreadguilds_text: "未読サーバー",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "",
context_mutedguilds_text: "ミュートサーバー",
context_guilds_text: "すべてのサーバー",
context_dms_text: "ダイレクトメッセージ"
};
case "zh-TW": //chinese (traditional)
return {
context_unreadguilds_text: "未讀服務器",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "言及されたサーバー",
context_mutedguilds_text: "靜音服務器",
context_guilds_text: "所有服務器",
context_dms_text: "直接消息",
};
case "ko": //korean
return {
context_unreadguilds_text: "읽지 않은 서버",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "언급 된 서버",
context_mutedguilds_text: "음소거 된 서버",
context_guilds_text: "모든 서버",
context_dms_text: "직접 메시지"
};
default: //default: english
return {
context_unreadguilds_text: "Unread Servers",
2019-10-21 12:11:46 +02:00
context_pingedguilds_text: "Pinged Servers",
context_mutedguilds_text: "Muted Servers",
context_guilds_text: "All Servers",
context_dms_text: "Direct Messages"
};
}
}
2018-10-11 10:21:26 +02:00
}