BetterDiscordAddons/Plugins/MoveablePopups/MoveablePopups.plugin.js

137 lines
5.4 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"MoveablePopups","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/MoveablePopups","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/MoveablePopups/MoveablePopups.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class MoveablePopups {
getName () {return "MoveablePopups";}
2019-10-16 21:31:47 +02:00
getVersion () {return "1.1.4";}
2018-10-11 10:21:26 +02:00
getAuthor () {return "DevilBro";}
2019-01-16 14:57:02 +01:00
getDescription () {return "Adds the feature to move all popups and modals around like on a normal desktop. Ctrl + drag with your left mousebutton to drag element.";}
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
this.libLoadTimeout = setTimeout(() => {
libraryScript.remove();
2019-10-18 10:56:41 +02:00
require("request")("https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js", (error, response, body) => {
2019-05-26 13:55:26 +02:00
if (body) {
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("date", performance.now());
libraryScript.innerText = body;
document.head.appendChild(libraryScript);
}
this.initialize();
});
}, 15000);
}
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
2018-10-11 10:21:26 +02:00
var observer = null;
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
2019-10-23 11:10:01 +02:00
if (node && BDFDB.DOMUtils.containsClass(node, BDFDB.disCN.popout)) {
2018-10-11 10:21:26 +02:00
this.makeMoveable(node);
}
});
}
}
);
});
2019-10-22 18:55:25 +02:00
BDFDB.ObserverUtils.connect(this, BDFDB.dotCN.popouts, {name:"popoutObserver",instance:observer}, {childList: true});
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
2019-10-23 11:10:01 +02:00
if (node && BDFDB.DOMUtils.containsClass(node, BDFDB.disCN.modal) && !node.querySelector(BDFDB.dotCN.downloadlink)) {
2018-10-11 10:21:26 +02:00
this.makeMoveable(node.querySelector(BDFDB.dotCN.modalinner));
}
2019-01-16 14:57:02 +01:00
else if (node.tagName && node.querySelector(BDFDB.dotCN.modal) && !node.querySelector(BDFDB.dotCN.downloadlink)) {
2018-10-11 10:21:26 +02:00
this.makeMoveable(node.querySelector(BDFDB.dotCN.modalinner));
}
});
}
}
);
});
2019-10-22 19:38:25 +02:00
BDFDB.ObserverUtils.connect(this, BDFDB.ReactUtils.findDOMNode(BDFDB.ReactUtils.findOwner(document.querySelector(BDFDB.dotCN.app), {name:"Modals"})), {name:"modalObserver", instance:observer}, {childList: true});
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-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
2018-10-11 10:21:26 +02:00
makeMoveable (div) {
2019-01-16 14:57:02 +01:00
div.removeEventListener("click", div.clickMovablePopups);
div.removeEventListener("mousedown", div.mousedownMovablePopups);
2019-10-22 18:55:25 +02:00
div.clickMovablePopups = e => {if (this.dragging) BDFDB.ListenerUtils.stopEvent(e);};
2019-01-16 14:57:02 +01:00
div.mousedownMovablePopups = e => {
if (!e.ctrlKey) return;
div.style.setProperty("position", "fixed", "important");
this.dragging = true;
2019-10-23 11:10:01 +02:00
var rects = BDFDB.DOMUtils.getRects(div);
2019-01-16 14:57:02 +01:00
var transform = getComputedStyle(div,null).getPropertyValue("transform").replace(/[^0-9,-]/g,"").split(",");
var left = rects.left - (transform.length > 4 ? parseFloat(transform[4]) : 0);
var top = rects.top - (transform.length > 4 ? parseFloat(transform[5]) : 0);
var oldX = e.pageX;
var oldY = e.pageY;
var mouseup = e2 => {
2019-10-23 11:10:01 +02:00
BDFDB.DOMUtils.removeLocalStyle("disableTextSelection");
2019-01-16 14:57:02 +01:00
document.removeEventListener("mouseup", mouseup);
document.removeEventListener("mousemove", mousemove);
setTimeout(() => {this.dragging = false},1);
};
var mousemove = e2 => {
left = left - (oldX - e2.pageX);
top = top - (oldY - e2.pageY);
oldX = e2.pageX;
oldY = e2.pageY;
div.style.setProperty("left", left + "px", "important");
div.style.setProperty("top", top + "px", "important");
2019-01-26 22:45:19 +01:00
2019-01-16 14:57:02 +01:00
};
document.addEventListener("mouseup", mouseup);
document.addEventListener("mousemove", mousemove);
};
div.addEventListener("click", div.clickMovablePopups);
div.addEventListener("mousedown", div.mousedownMovablePopups);
2018-10-11 10:21:26 +02:00
}
}