2019-09-20 22:32:52 +02:00
|
|
|
//META{"name":"ImageGallery","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ImageGallery","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ImageGallery/ImageGallery.plugin.js"}*//
|
2018-10-11 10:21:26 +02:00
|
|
|
|
|
|
|
class ImageGallery {
|
2019-01-11 21:57:59 +01:00
|
|
|
getName () {return "ImageGallery";}
|
|
|
|
|
2019-09-11 12:14:43 +02:00
|
|
|
getVersion () {return "1.5.9";}
|
2019-01-11 21:57:59 +01:00
|
|
|
|
|
|
|
getAuthor () {return "DevilBro";}
|
|
|
|
|
|
|
|
getDescription () {return "Allows the user to browse through images sent inside the same message.";}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-09-04 12:34:02 +02:00
|
|
|
constructor () {
|
2019-02-18 11:34:42 +01:00
|
|
|
this.changelog = {
|
2019-09-11 12:14:43 +02:00
|
|
|
"fixed":[["Light Theme Update","Fixed bugs for the Light Theme Update, which broke 99% of my plugins"]]
|
2019-02-18 11:34:42 +01:00
|
|
|
};
|
2019-09-04 12:34:02 +02:00
|
|
|
|
2019-01-11 21:57:59 +01:00
|
|
|
this.patchModules = {
|
|
|
|
"ImageModal":["componentDidMount","componentWillUnmount"]
|
2019-09-04 12:34:02 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
initConstructor () {
|
2018-10-11 10:21:26 +02:00
|
|
|
this.eventFired = false;
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
this.imageMarkup = `<div class="${BDFDB.disCN.imagewrapper}" style="width: 100px; height: 100px;"><img src="" style="width: 100px; height: 100px; display: inline;"></div>`;
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
this.css = `
|
|
|
|
.image-gallery ${BDFDB.dotCN.imagewrapper}.prev,
|
|
|
|
.image-gallery ${BDFDB.dotCN.imagewrapper}.next {
|
|
|
|
position: absolute;
|
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
.image-gallery ${BDFDB.dotCN.imagewrapper}.prev {
|
|
|
|
right: 90%;
|
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
.image-gallery ${BDFDB.dotCN.imagewrapper}.next {
|
|
|
|
left: 90%;
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
//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();
|
2018-10-11 10:21:26 +02:00
|
|
|
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-11 21:57:59 +01:00
|
|
|
this.closemodal = true;
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-10-22 18:55:25 +02:00
|
|
|
BDFDB.ModuleUtils.forceAllUpdates(this, "ImageModal");
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-11 21:57:59 +01:00
|
|
|
delete this.closemodal;
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
document.removeEventListener("keydown", document.keydownImageGalleryListener);
|
|
|
|
document.removeEventListener("keyup", document.keyupImageGalleryListener);
|
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-09-11 12:14:43 +02:00
|
|
|
processImageModal (instance, wrapper, returnvalue, methodnames) {
|
2019-01-11 21:57:59 +01:00
|
|
|
if (this.closemodal && instance.props && instance.props.onClose) instance.props.onClose();
|
|
|
|
else if (methodnames.includes("componentDidMount")) {
|
2019-10-23 11:10:01 +02:00
|
|
|
let modal = BDFDB.DOMUtils.getParent(BDFDB.dotCN.modal, wrapper);
|
2019-01-11 21:57:59 +01:00
|
|
|
if (!modal) return;
|
|
|
|
let start = performance.now();
|
|
|
|
let waitForImg = setInterval(() => {
|
|
|
|
let img = modal.querySelector(BDFDB.dotCNS.imagewrapper + "img");
|
|
|
|
if (img && img.src) {
|
|
|
|
clearInterval(waitForImg);
|
|
|
|
let message = this.getMessageGroupOfImage(img);
|
|
|
|
if (message) {
|
2019-10-23 11:10:01 +02:00
|
|
|
BDFDB.DOMUtils.addClass(modal, "image-gallery");
|
2019-01-11 21:57:59 +01:00
|
|
|
this.addImages(modal, message.querySelectorAll(BDFDB.dotCNS.imagewrapper + "img"), img);
|
|
|
|
}
|
2018-10-11 10:21:26 +02:00
|
|
|
}
|
2019-01-11 21:57:59 +01:00
|
|
|
else if (performance.now() - start > 10000) {
|
|
|
|
clearInterval(waitForImg);
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}
|
2019-01-17 23:48:29 +01:00
|
|
|
else if (methodnames.includes("componentWillUnmount")) {
|
|
|
|
document.removeEventListener("keydown", document.keydownImageGalleryListener);
|
|
|
|
document.removeEventListener("keyup", document.keyupImageGalleryListener);
|
|
|
|
}
|
2018-10-11 10:21:26 +02:00
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
getMessageGroupOfImage (thisimg) {
|
|
|
|
if (thisimg && thisimg.src) {
|
|
|
|
for (let group of document.querySelectorAll(BDFDB.dotCN.messagegroup)) {
|
|
|
|
for (let img of group.querySelectorAll(BDFDB.dotCNS.imagewrapper + "img")) {
|
|
|
|
if (img.src && this.getSrcOfImage(img) == this.getSrcOfImage(thisimg)) {
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
getSrcOfImage (img) {
|
2019-01-11 21:57:59 +01:00
|
|
|
return (img.src || (img.querySelector("canvas") ? img.querySelector("canvas").src : "")).split("?width=")[0];
|
2018-10-11 10:21:26 +02:00
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
addImages (modal, imgs, img) {
|
2019-10-23 11:10:01 +02:00
|
|
|
BDFDB.DOMUtils.remove(modal.querySelectorAll(`${BDFDB.dotCN.imagewrapper}.prev, ${BDFDB.dotCN.imagewrapper}.next`));
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
let inner = modal.querySelector(BDFDB.dotCN.modalinner);
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
if (!inner) return;
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
var prevImg, nextImg, index;
|
|
|
|
for (index = 0; index < imgs.length; index++) {
|
|
|
|
if (this.getSrcOfImage(img) == this.getSrcOfImage(imgs[index])) {
|
|
|
|
prevImg = imgs[index-1];
|
|
|
|
img = imgs[index];
|
|
|
|
nextImg = imgs[index+1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
var imagesrc = this.getSrcOfImage(img);
|
|
|
|
modal.querySelector(BDFDB.dotCN.downloadlink).setAttribute("href", imagesrc);
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
var imagewrapper = modal.querySelector(BDFDB.dotCN.imagewrapper);
|
2019-10-23 11:10:01 +02:00
|
|
|
BDFDB.DOMUtils.addClass(imagewrapper, "current");
|
2019-01-17 23:48:29 +01:00
|
|
|
var imagewrapperimage = imagewrapper.querySelector("img");
|
|
|
|
imagewrapperimage.setAttribute("src", imagesrc);
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
this.resizeImage(modal, img, imagewrapperimage);
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
if (prevImg) inner.appendChild(this.createImage(modal, imgs, prevImg, "prev"));
|
|
|
|
if (nextImg) inner.appendChild(this.createImage(modal, imgs, nextImg, "next"));
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
document.removeEventListener("keydown", document.keydownImageGalleryListener);
|
|
|
|
document.removeEventListener("keyup", document.keyupImageGalleryListener);
|
|
|
|
document.keydownImageGalleryListener = e => {this.keyPressed({modal, imgs, prevImg, nextImg}, e);};
|
|
|
|
document.keyupImageGalleryListener = e => {this.eventFired = false;};
|
|
|
|
document.addEventListener("keydown", document.keydownImageGalleryListener);
|
|
|
|
document.addEventListener("keyup", document.keyupImageGalleryListener);
|
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
createImage (modal, imgs, img, type) {
|
2019-10-23 11:10:01 +02:00
|
|
|
var imagewrapper = BDFDB.DOMUtils.create(this.imageMarkup);
|
|
|
|
BDFDB.DOMUtils.addClass(imagewrapper, type);
|
2019-01-17 23:48:29 +01:00
|
|
|
imagewrapper.addEventListener("click", () => {this.addImages(modal, imgs, img);});
|
|
|
|
var imagewrapperimage = imagewrapper.querySelector("img");
|
|
|
|
imagewrapperimage.setAttribute("src", this.getSrcOfImage(img));
|
|
|
|
this.resizeImage(modal, img, imagewrapperimage);
|
|
|
|
return imagewrapper;
|
2018-10-11 10:21:26 +02:00
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
resizeImage (container, src, img) {
|
2019-10-23 11:10:01 +02:00
|
|
|
BDFDB.DOMUtils.hide(img);
|
2018-10-11 10:21:26 +02:00
|
|
|
var temp = new Image();
|
|
|
|
temp.src = src.src.split("?width=")[0];
|
|
|
|
temp.onload = function () {
|
|
|
|
var resizeX = (container.clientWidth/src.clientWidth) * 0.71;
|
|
|
|
var resizeY = (container.clientHeight/src.clientHeight) * 0.57;
|
|
|
|
var resize = resizeX < resizeY ? resizeX : resizeY;
|
|
|
|
var newWidth = src.clientWidth * resize;
|
|
|
|
var newHeight = src.clientHeight * resize;
|
|
|
|
newWidth = temp.width > newWidth ? newWidth : temp.width;
|
|
|
|
newHeight = temp.height > newHeight ? newHeight : temp.height;
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
var wrapper = img.parentElement;
|
2019-10-23 11:10:01 +02:00
|
|
|
if (!BDFDB.DOMUtils.containsClass(wrapper, "current")) wrapper.style.setProperty("top", (container.clientHeight - newHeight) / 2 + "px");
|
2019-01-17 23:48:29 +01:00
|
|
|
wrapper.style.setProperty("width", newWidth + "px");
|
|
|
|
wrapper.style.setProperty("height", newHeight + "px");
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
img.style.setProperty("width", newWidth + "px");
|
|
|
|
img.style.setProperty("height", newHeight + "px");
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-10-23 11:10:01 +02:00
|
|
|
BDFDB.DOMUtils.show(img);
|
2018-10-11 10:21:26 +02:00
|
|
|
};
|
|
|
|
}
|
2019-01-26 22:45:19 +01:00
|
|
|
|
2019-01-17 23:48:29 +01:00
|
|
|
keyPressed ({modal, imgs, prevImg, nextImg}, e) {
|
2018-10-11 10:21:26 +02:00
|
|
|
if (!this.eventFired) {
|
|
|
|
this.eventFired = true;
|
2019-01-17 23:48:29 +01:00
|
|
|
if (e.keyCode == 37 && prevImg) this.addImages(modal, imgs, prevImg);
|
|
|
|
else if (e.keyCode == 39 && nextImg) this.addImages(modal, imgs, nextImg);
|
2018-10-11 10:21:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|