BetterDiscordAddons/Plugins/ReadAllNotificationsButton/ReadAllNotificationsButton....

410 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-10-21 12:11:46 +02:00
getVersion () {return "1.5.1";}
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-10-21 12:11:46 +02:00
"added":[["Pinged servers","Added the contextmenu item to only clear unread notifications on pinged servers"]]
};
2019-09-04 12:34:02 +02:00
this.patchModules = {
2019-05-03 17:49:54 +02:00
"Guilds":["componentDidMount","componentDidUpdate"],
2019-03-07 23:56:18 +01:00
"RecentMentions":"componentDidMount",
"DirectMessage":"componentDidMount"
};
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 () {
2018-10-11 10:21:26 +02:00
this.RANbuttonMarkup =
`<div class="${BDFDB.disCN.guildouter} RANbutton-frame" style="height: 20px;">
2019-09-11 12:14:43 +02:00
<div class="${BDFDB.disCN.guildiconwrapper} RANbutton-inner" style="height: 20px;">
<div class="${BDFDB.disCNS.guildiconchildwrapper + BDFDB.disCN.guildiconacronym} RANbutton" style="height: 20px;">read all</div>
</div>
2018-10-11 10:21:26 +02:00
</div>`;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.RAMbuttonMarkup =
`<button type="button" class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.button + BDFDB.disCNS.buttonlookfilled + BDFDB.disCNS.buttoncolorbrand + BDFDB.disCNS.buttonsizemin + BDFDB.disCN.buttongrow} RAMbutton" style="flex: 0 0 auto; margin-left: 25px; height: 25px;">
<div class="${BDFDB.disCN.buttoncontents}">Clear Mentions</div>
2018-10-11 10:21:26 +02:00
</button>`;
2019-09-04 12:34:02 +02:00
2019-04-23 14:37:56 +02:00
this.css = `
.RANbutton-frame {
margin-bottom: 10px;
}
.RANbutton {
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 () {
2019-01-22 11:28:32 +01:00
if (!global.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-11-05 08:30:31 +01:00
let 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-10-22 18:55:25 +02:00
return BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
2018-10-11 10:21:26 +02:00
}
//legacy
load () {}
start () {
2019-02-04 09:13:15 +01:00
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
2019-05-26 13:55:26 +02:00
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
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());
2019-05-26 13:55:26 +02:00
libraryScript.addEventListener("load", () => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
}
2019-01-17 23:48:29 +01:00
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2019-11-01 10:27:07 +01:00
this.startTimeout = setTimeout(() => {
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 () {
2019-01-17 23:48:29 +01:00
if (global.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 () {
2019-01-17 23:48:29 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".RANbutton-frame", ".RAMbutton");
BDFDB.DOMUtils.removeClassFromDOM("RAN-added", "RAM-added");
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-09-11 12:14:43 +02:00
processGuilds (instance, wrapper, returnvalue, methodnames) {
if (methodnames.includes("componentDidMount") || (methodnames.includes("componentDidUpdate") && document.querySelector(".bd-guild ~ .RANbutton-frame"))) {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".RANbutton-frame");
let insertnode = this.getInsertNode();
if (insertnode) {
2019-10-23 11:10:01 +02:00
let ranbutton = BDFDB.DOMUtils.create(this.RANbuttonMarkup);
insertnode.parentElement.insertBefore(ranbutton, insertnode);
ranbutton.addEventListener("click", () => {
2019-10-22 19:49:57 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-10-22 18:55:25 +02:00
if (settings.includeGuilds) BDFDB.GuildUtils.markAsRead(settings.includeMuted ? BDFDB.GuildUtils.getAll() : BDFDB.GuildUtils.getUnread());
2019-10-23 11:10:01 +02:00
if (settings.includeDMs) BDFDB.DMUtils.markAsRead(BDFDB.DMUtils.getAll());
});
ranbutton.addEventListener("contextmenu", e => {
2019-10-22 18:55:25 +02:00
const itemGroup = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItemGroup, {
2019-09-11 12:14:43 +02:00
children: [
2019-10-22 18:55:25 +02:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItem, {
2019-09-11 12:14:43 +02:00
label: this.labels.context_unreadguilds_text,
action: e => {
2019-11-01 10:14:50 +01:00
BDFDB.ContextMenuUtils.close(BDFDB.DOMUtils.getParent(BDFDB.dotCN.contextmenu, e.target));
2019-10-22 18:55:25 +02:00
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getUnread());
2019-09-11 12:14:43 +02:00
}
}),
2019-10-22 18:55:25 +02:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItem, {
2019-10-21 12:11:46 +02:00
label: this.labels.context_pingedguilds_text,
action: e => {
2019-11-01 10:14:50 +01:00
BDFDB.ContextMenuUtils.close(BDFDB.DOMUtils.getParent(BDFDB.dotCN.contextmenu, e.target));
2019-10-22 18:55:25 +02:00
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getPinged());
2019-10-21 12:11:46 +02:00
}
}),
2019-10-22 18:55:25 +02:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItem, {
2019-09-11 12:14:43 +02:00
label: this.labels.context_mutedguilds_text,
action: e => {
2019-11-01 10:14:50 +01:00
BDFDB.ContextMenuUtils.close(BDFDB.DOMUtils.getParent(BDFDB.dotCN.contextmenu, e.target));
2019-10-22 18:55:25 +02:00
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getMuted());
2019-09-11 12:14:43 +02:00
}
}),
2019-10-22 18:55:25 +02:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItem, {
2019-09-11 12:14:43 +02:00
label: this.labels.context_guilds_text,
action: e => {
2019-11-01 10:14:50 +01:00
BDFDB.ContextMenuUtils.close(BDFDB.DOMUtils.getParent(BDFDB.dotCN.contextmenu, e.target));
2019-09-11 12:14:43 +02:00
this.addPinnedRecent(instance.props.channel.id);
2019-10-22 18:55:25 +02:00
BDFDB.GuildUtils.markAsRead(BDFDB.GuildUtils.getAll());
2019-09-11 12:14:43 +02:00
}
}),
2019-10-22 18:55:25 +02:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItem, {
2019-09-11 12:14:43 +02:00
label: this.labels.context_dms_text,
action: e => {
2019-11-01 10:14:50 +01:00
BDFDB.ContextMenuUtils.close(BDFDB.DOMUtils.getParent(BDFDB.dotCN.contextmenu, e.target));
2019-10-23 11:10:01 +02:00
BDFDB.DMUtils.markAsRead(BDFDB.DMUtils.getAll());
2019-09-11 12:14:43 +02:00
}
})
]
});
2019-11-01 10:14:50 +01:00
BDFDB.ContextMenuUtils.open(this, e, itemGroup);
});
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.addClass(wrapper, "RAN-added");
}
2019-01-17 23:48:29 +01:00
}
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processDirectMessage (instance, wrapper, returnvalue, methodnames) {
2019-03-07 23:56:18 +01:00
let ranbutton = document.querySelector(".RANbutton-frame");
2019-04-27 08:48:35 +02:00
let insertnode = this.getInsertNode();
if (ranbutton && insertnode) insertnode.parentElement.insertBefore(ranbutton, insertnode);
2019-03-07 23:56:18 +01:00
}
2019-09-11 12:14:43 +02:00
processRecentMentions (instance, wrapper, returnvalue) {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.remove(".RAMbutton");
2019-11-11 11:11:33 +01:00
if (instance.props.popoutName == "RECENT_MENTIONS_POPOUT" && BDFDB.DataUtils.get(this, "settings", "addClearButton")) {
2019-02-08 21:33:47 +01:00
let recentmentionstitle = wrapper.querySelector(BDFDB.dotCN.messagespopouttitle);
2019-01-17 23:48:29 +01:00
if (recentmentionstitle) {
2019-10-23 11:10:01 +02:00
let ranbutton = BDFDB.DOMUtils.create(this.RAMbuttonMarkup);
2019-01-17 23:48:29 +01:00
recentmentionstitle.appendChild(ranbutton);
ranbutton.addEventListener("click", () => {this.clearMentions(instance, wrapper);});
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.addClass(wrapper, "RAM-added");
2019-01-17 23:48:29 +01:00
}
}
}
2019-01-26 22:45:19 +01:00
clearMentions (instance, wrapper) {
let closebuttons = wrapper.querySelectorAll(BDFDB.dotCN.messagespopoutclosebutton);
for (let btn of wrapper.querySelectorAll(BDFDB.dotCN.messagespopoutclosebutton)) btn.click();
if (closebuttons.length) {
instance.loadMore();
2019-11-01 11:09:32 +01:00
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-04-27 08:48:35 +02:00
getInsertNode () {
2019-10-23 11:10:01 +02:00
let homebutton = BDFDB.DOMUtils.getParent(BDFDB.dotCN.guildouter, document.querySelector(BDFDB.dotCN.homebuttonicon));
2019-05-03 17:49:54 +02:00
if (!homebutton) return null;
2019-04-27 08:48:35 +02:00
let nextsibling = homebutton.nextElementSibling, insertnode = null;
while (nextsibling && insertnode == null) {
if (nextsibling.querySelector(`${BDFDB.dotCN.guildseparator}:not(.folderseparator)`)) insertnode = nextsibling;
nextsibling = nextsibling.nextElementSibling
}
return insertnode;
}
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
}