BetterDiscordAddons/Plugins/ShowImageDetails/ShowImageDetails.plugin.js

185 lines
6.4 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"ShowImageDetails","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
class ShowImageDetails {
2019-01-17 23:48:29 +01:00
getName () {return "ShowImageDetails";}
2019-11-11 21:04:43 +01:00
getVersion () {return "1.1.7";}
2019-01-17 23:48:29 +01:00
getAuthor () {return "DevilBro";}
2019-11-11 13:54:30 +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
2019-09-04 12:34:02 +02:00
constructor () {
2019-02-04 09:40:44 +01:00
this.changelog = {
2019-11-11 13:54:30 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-02-04 09:40:44 +01:00
};
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this.patchedModules = {
after: {
LazyImage: "render"
}
};
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.css = `
.image-details .image-details-size {
margin: 0 10px;
}
.image-details-tooltip {
max-width: 500px;
}
.image-details-tooltip .image-details-tooltip-size {
margin: 10px 0;
}
`;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.defaults = {
settings: {
showOnHover: {value:false, description:"Show the details as Tooltip instead:"}
},
amounts: {
2019-02-26 12:16:08 +01:00
hoverDelay: {value:0, min:0, description:"Tooltip delay in millisec:"}
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 19:49:57 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
let amounts = BDFDB.DataUtils.get(this, "amounts");
2019-12-18 16:45:08 +01:00
let settingspanel, settingsitems = [];
2019-11-11 13:54:30 +01:00
for (let key in settings) settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom8
}));
for (let key in amounts) settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
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]
}));
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 () {
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-05-26 13:55:26 +02: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-11-11 13:54:30 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-01-26 22:45:19 +01:00
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-11 13:54:30 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.ModuleUtils.forceAllUpdates(this);
}
}
2019-01-26 22:45:19 +01:00
2019-11-11 13:54:30 +01:00
processLazyImage (e) {
if (typeof e.returnvalue.props.children == "function" && e.instance.props.original && e.instance.props.src.indexOf("https://media.discordapp.net/attachments") == 0) {
2019-11-11 21:04:43 +01:00
let attachment = BDFDB.ReactUtils.findValue(e.instance, "attachment", {up:true});
if (!attachment) return;
let settings = BDFDB.DataUtils.get(this, "settings");
let amounts = BDFDB.DataUtils.get(this, "amounts");
let renderChildren = e.returnvalue.props.children;
2019-11-17 12:34:38 +01:00
e.returnvalue.props.children = (...args) => {
let renderedChildren = renderChildren(...args);
2019-11-11 21:04:43 +01:00
if (settings.showOnHover) 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: renderedChildren
});
else return [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
className: "image-details",
children: [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Anchor, {
title: e.instance.props.original,
href: e.instance.props.original,
children: attachment.filename
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextElement, {
color: BDFDB.LibraryComponents.TextElement.Colors.PRIMARY,
children: BDFDB.NumberUtils.formatBytes(attachment.size)
})
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex.Child, {
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextElement, {
color: BDFDB.LibraryComponents.TextElement.Colors.PRIMARY,
children: `${attachment.width}x${attachment.height}px`
})
2019-11-11 13:54:30 +01:00
})
2019-11-11 21:04:43 +01:00
]
}),
renderedChildren
];
};
}
2018-10-11 10:21:26 +02:00
}
}