BetterDiscordAddons/Plugins/ShowImageDetails/ShowImageDetails.plugin.js

229 lines
8.4 KiB
JavaScript
Raw Normal View History

2020-02-27 08:44:03 +01:00
//META{"name":"ShowImageDetails","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ShowImageDetails","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ShowImageDetails/ShowImageDetails.plugin.js"}*//
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
var ShowImageDetails = (_ => {
2020-02-04 11:34:31 +01:00
const ImageDetails = class ImageDetails extends BdApi.React.Component {
componentDidMount() {
2020-02-04 23:12:35 +01:00
this.props.attachment = BDFDB.ReactUtils.findValue(BDFDB.ReactUtils.getValue(this, "_reactInternalFiber.return"), "attachment", {up: true});
BDFDB.ReactUtils.forceUpdate(this);
2020-02-04 11:34:31 +01:00
}
2020-02-05 12:22:49 +01:00
componentDidUpdate() {
2020-02-05 14:38:08 +01:00
if ((!this.props.attachment || !this.props.attachment.size) && !this.props.loaded) {
2020-02-05 13:40:35 +01:00
this.props.loaded = true;
2020-02-05 12:22:49 +01:00
this.props.attachment = BDFDB.ReactUtils.findValue(BDFDB.ReactUtils.getValue(this, "_reactInternalFiber.return"), "attachment", {up: true});
BDFDB.ReactUtils.forceUpdate(this);
}
}
2020-02-04 11:34:31 +01:00
render() {
2020-02-04 23:12:35 +01:00
return !this.props.attachment ? null : BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
2020-02-04 11:34:31 +01:00
className: BDFDB.disCN._showimagedetailsdetails,
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Anchor, {
title: this.props.original,
href: this.props.original,
children: this.props.attachment.filename,
onClick: event => {
BDFDB.ListenerUtils.stopEvent(event);
BDFDB.DiscordUtils.openLink(this.props.original);
}
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextElement, {
children: BDFDB.NumberUtils.formatBytes(this.props.attachment.size)
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextElement, {
children: `${this.props.attachment.width}x${this.props.attachment.height}px`
})
})
]
});
}
};
2020-02-04 08:20:40 +01:00
return class ShowImageDetails {
getName () {return "ShowImageDetails";}
2019-01-17 23:48:29 +01:00
2020-02-11 10:00:17 +01:00
getVersion () {return "1.2.9";}
2019-01-17 23:48:29 +01:00
2020-02-04 08:20:40 +01:00
getAuthor () {return "DevilBro";}
2019-01-17 23:48:29 +01:00
2020-02-04 08:20:40 +01:00
getDescription () {return "Display the name, size and dimensions of uploaded images in the chat as an header or as a tooltip.";}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
constructor () {
this.changelog = {
2020-02-11 10:00:17 +01:00
"fixed":[["Image Link","Clicking an image link in the details now properly opens the image in a new window"]],
2020-02-04 08:20:40 +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
2020-02-04 08:20:40 +01:00
this.patchedModules = {
after: {
LazyImage: "render"
}
};
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
initConstructor () {
this.css = `
${BDFDB.dotCN._showimagedetailsdetails} {
margin: 5px 0;
}
2020-02-05 12:02:56 +01:00
${BDFDB.dotCNS.spoilerhidden + BDFDB.dotCN._showimagedetailsdetails} {
visibility: hidden;
max-width: 1px;
}
2020-02-04 08:20:40 +01:00
`;
this.defaults = {
settings: {
showOnHover: {value:false, description:"Show the details as Tooltip instead:"}
},
amounts: {
hoverDelay: {value:0, min:0, description:"Tooltip delay in millisec:"}
}
};
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
getSettingsPanel () {
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
let settings = BDFDB.DataUtils.get(this, "settings");
let amounts = BDFDB.DataUtils.get(this, "amounts");
2020-03-28 07:55:39 +01:00
let settingsPanel, settingsItems = [];
2020-02-04 08:20:40 +01:00
2020-03-28 07:55:39 +01:00
for (let key in settings) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-02-04 08:20:40 +01:00
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
2020-03-28 07:55:39 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
2020-02-04 08:20:40 +01:00
className: BDFDB.disCN.marginbottom8
}));
2020-03-28 07:55:39 +01:00
for (let key in amounts) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-02-04 08:20:40 +01:00
className: BDFDB.disCN.marginbottom8,
type: "TextInput",
childProps: {
type: "number"
},
plugin: this,
keys: ["amounts", key],
label: this.defaults.amounts[key].description,
basis: "50%",
min: this.defaults.amounts[key].min,
max: this.defaults.amounts[key].max,
value: amounts[key]
}));
2020-03-28 07:55:39 +01:00
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
2020-02-04 08:20:40 +01:00
}
2018-10-11 10:21:26 +02:00
2020-04-11 19:32:58 +02:00
// Legacy
2020-02-04 08:20:40 +01:00
load () {}
start () {
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
libraryScript.setAttribute("date", performance.now());
libraryScript.addEventListener("load", _ => {this.initialize();});
document.head.appendChild(libraryScript);
}
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
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);
2019-05-26 13:55:26 +02:00
}
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
initialize () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
BDFDB.PluginUtils.init(this);
2020-02-04 11:34:31 +01:00
2020-02-11 10:00:17 +01:00
BDFDB.ModuleUtils.patch(this, (BDFDB.ModuleUtils.findByName("renderImageComponent", false).exports || {}), "renderImageComponent", {after: e => {
2020-02-04 11:34:31 +01:00
if (e.returnValue && e.returnValue.type && (e.returnValue.type.displayName == "LazyImageZoomable" || e.returnValue.type.displayName == "LazyImage") && e.methodArguments[0].original && e.methodArguments[0].src.indexOf("https://media.discordapp.net/attachments") == 0) return this.injectImageDetails(e.methodArguments[0], e.returnValue);
}});
2019-01-26 22:45:19 +01:00
2020-02-04 11:34:31 +01:00
BDFDB.MessageUtils.rerenderAll();
2020-02-04 08:20:40 +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
}
2020-02-04 08:20:40 +01:00
stop () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
this.stopping = true;
2020-02-04 11:34:31 +01:00
2020-02-05 12:24:05 +01:00
BDFDB.MessageUtils.rerenderAll();
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
BDFDB.PluginUtils.clear(this);
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
2020-02-04 11:34:31 +01:00
BDFDB.MessageUtils.rerenderAll();
2020-02-04 08:20:40 +01:00
}
}
2020-02-04 11:34:31 +01:00
injectImageDetails (props, child) {
let settings = BDFDB.DataUtils.get(this, "settings");
if (!settings.showOnHover) {
props.detailsAdded = true;
return BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCN.embedwrapper,
children: [
BDFDB.ReactUtils.createElement(ImageDetails, {
2020-02-11 10:00:17 +01:00
original: props.original,
2020-02-04 11:34:31 +01:00
attachment: {
height: 0,
width: 0,
filename: "unknown.png"
}
}),
child
]
});
}
return child;
}
2020-02-04 08:20:40 +01:00
processLazyImage (e) {
2020-02-04 13:00:41 +01:00
if (e.instance.props.original && e.instance.props.src.indexOf("https://media.discordapp.net/attachments") == 0 && typeof e.returnvalue.props.children == "function") {
let attachment = BDFDB.ReactUtils.findValue(e.instance, "attachment", {up:true});
if (!attachment) return;
let settings = BDFDB.DataUtils.get(this, "settings");
if (settings.showOnHover) {
let amounts = BDFDB.DataUtils.get(this, "amounts");
let renderChildren = e.returnvalue.props.children;
e.returnvalue.props.children = (...args) => {
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: `${attachment.filename}\n${BDFDB.NumberUtils.formatBytes(attachment.size)}\n${attachment.width}x${attachment.height}px`,
tooltipConfig: {
type: "right",
delay: amounts.hoverDelay
},
children: renderChildren(...args)
});
};
2020-02-04 08:20:40 +01:00
}
}
2019-11-11 21:04:43 +01:00
}
2018-10-11 10:21:26 +02:00
}
2020-02-05 14:38:08 +01:00
})();