BetterDiscordAddons/Plugins/RevealAllSpoilersOption/RevealAllSpoilersOption.plu...

89 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"RevealAllSpoilersOption","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/RevealAllSpoilersOption","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/RevealAllSpoilersOption/RevealAllSpoilersOption.plugin.js"}*//
2019-02-01 12:37:27 +01:00
class RevealAllSpoilersOption {
getName () {return "RevealAllSpoilersOption";}
2019-11-21 11:38:04 +01:00
getVersion () {return "1.0.3";}
2019-02-01 12:37:27 +01:00
getAuthor () {return "DevilBro";}
getDescription () {return "Adds an entry to the message contextmenu to reveal all spoilers within a messageblock.";}
2019-09-11 12:14:43 +02:00
constructor () {
this.changelog = {
2019-11-07 10:15:10 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-09-11 12:14:43 +02:00
};
2019-02-01 12:37:27 +01: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) {
2019-02-01 12:37:27 +01:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2019-02-01 12:37:27 +01: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-02-01 12:37:27 +01:00
libraryScript.setAttribute("date", performance.now());
2020-01-14 00:06:07 +01:00
libraryScript.addEventListener("load", _ => {this.initialize();});
2019-02-01 12:37:27 +01: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);
2019-02-01 12:37:27 +01:00
}
initialize () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-02-01 12:37:27 +01:00
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-02-01 12:37:27 +01: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!");
2019-02-01 12:37:27 +01: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-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2019-02-01 12:37:27 +01:00
}
}
2019-09-04 12:34:02 +02:00
2019-02-01 12:37:27 +01:00
// begin of own functions
2019-11-07 10:15:10 +01:00
onMessageContextMenu (e) {
2019-11-11 10:48:30 +01:00
if (e.instance.props.message && e.instance.props.target) {
2019-11-08 14:36:57 +01:00
let messagediv = BDFDB.DOMUtils.getParent(BDFDB.dotCN.messagegroup + "> [aria-disabled]," + BDFDB.dotCN.messagegroup + "> * > [aria-disabled]", e.instance.props.target);
2019-02-01 12:37:27 +01:00
if (!messagediv || !messagediv.querySelector(BDFDB.dotCN.spoilerhidden)) return;
2019-11-07 10:15:10 +01:00
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {name:["FluxContainer(MessageDeveloperModeGroup)", "DeveloperModeGroup"]});
2019-12-05 09:10:14 +01:00
const itemgroup = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Group, {
2019-09-11 12:14:43 +02:00
children: [
2019-12-05 09:10:14 +01:00
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Item, {
2019-09-11 12:14:43 +02:00
label: "Reveal all Spoilers",
2019-10-23 11:10:01 +02:00
hint: BDFDB.BDUtils.isPluginEnabled("MessageUtilities") ? BDFDB.BDUtils.getPlugin("MessageUtilities").getActiveShortcutString("__Reveal_Spoilers") : null,
2019-11-21 11:38:04 +01:00
action: _ => {
2019-11-07 10:15:10 +01:00
BDFDB.ContextMenuUtils.close(e.instance);
2019-09-11 12:14:43 +02:00
this.revealAllSpoilers(messagediv);
}
})
]
2019-02-01 12:37:27 +01:00
});
2019-09-11 12:14:43 +02:00
if (index > -1) children.splice(index, 0, itemgroup);
else children.push(itemgroup);
2019-02-01 12:37:27 +01:00
}
}
2019-09-04 12:34:02 +02:00
2019-02-01 12:37:27 +01:00
revealAllSpoilers (target) {
2019-11-08 14:36:57 +01:00
let messagediv = BDFDB.DOMUtils.getParent(BDFDB.dotCN.messagegroup + "> [aria-disabled]," + BDFDB.dotCN.messagegroup + "> * > [aria-disabled]", target);
2019-02-01 12:37:27 +01:00
if (!messagediv) return;
for (let spoiler of messagediv.querySelectorAll(BDFDB.dotCN.spoilerhidden)) spoiler.click();
}
}