Update 0BDFDB.plugin.js

This commit is contained in:
Mirco Wittrien 2020-09-11 13:10:24 +02:00
parent 20ab2e8051
commit 97ca97ac50
1 changed files with 63 additions and 59 deletions

View File

@ -17,7 +17,9 @@ module.exports = (_ => {
var settings = {}; var settings = {};
if (window.BDFDB && window.BDFDB.PluginUtils && typeof window.BDFDB.PluginUtils.cleanUp == "function") window.BDFDB.PluginUtils.cleanUp(window.BDFDB); if (window.BDFDB_Global && window.BDFDB_Global.PluginUtils && typeof window.BDFDB_Global.PluginUtils.cleanUp == "function") {
window.BDFDB_Global.PluginUtils.cleanUp(window.BDFDB_Global);
}
const BDFDB = Object.assign({ const BDFDB = Object.assign({
started: true started: true
@ -47,7 +49,7 @@ module.exports = (_ => {
getVersion() {return config.version;} getVersion() {return config.version;}
getDescription() {return config.description;} getDescription() {return config.description;}
load() { load() {
if (window.BDFDB.loading) PluginStores.delayedLoad.push(this); if (window.BDFDB_Global.loading) PluginStores.delayedLoad.push(this);
else { else {
Object.assign(this, BDFDB.ObjectUtils.extract(config, "name", "author", "version", "description", "changeLog", "patchedModules", "rawUrl")); Object.assign(this, BDFDB.ObjectUtils.extract(config, "name", "author", "version", "description", "changeLog", "patchedModules", "rawUrl"));
BDFDB.PluginUtils.load(this); BDFDB.PluginUtils.load(this);
@ -55,7 +57,7 @@ module.exports = (_ => {
} }
} }
start() { start() {
if (window.BDFDB.loading) PluginStores.delayedStart.push(this); if (window.BDFDB_Global.loading) PluginStores.delayedStart.push(this);
else { else {
if (this.started) return; if (this.started) return;
BDFDB.PluginUtils.init(this); BDFDB.PluginUtils.init(this);
@ -89,6 +91,40 @@ module.exports = (_ => {
console.error(`%c[${typeof name == "string" && name || "BDFDB"}]`, "color: #3a71c1; font-weight: 700;", "Fatal Error: " + (typeof string == "string" && string || "").trim()); console.error(`%c[${typeof name == "string" && name || "BDFDB"}]`, "color: #3a71c1; font-weight: 700;", "Fatal Error: " + (typeof string == "string" && string || "").trim());
}; };
BDFDB.TimeUtils = {};
BDFDB.TimeUtils.interval = function (callback, delay, ...args) {
if (typeof callback != "function" || typeof delay != "number" || delay < 1) return;
else {
let count = 0, interval = setInterval(_ => {BDFDB.TimeUtils.suppress(callback, "Interval")(...[interval, count++, args].flat());}, delay);
return interval;
}
};
BDFDB.TimeUtils.timeout = function (callback, delay, ...args) {
delay = parseFloat(delay);
if (typeof callback != "function") return;
else if (isNaN(delay) || typeof delay != "number" || delay < 1) {
let immediate = setImmediate(_ => {BDFDB.TimeUtils.suppress(callback, "Immediate")(...[immediate, args].flat());});
return immediate;
}
else {
let timeout = setTimeout(_ => {BDFDB.TimeUtils.suppress(callback, "Timeout")(...[timeout, args].flat());}, delay);
return timeout;
}
};
BDFDB.TimeUtils.clear = function (...timeObjects) {
for (let t of timeObjects.flat(10).filter(n => n)) {
if (typeof t == "number") {
clearInterval(t);
clearTimeout(t);
}
else if (typeof t == "object") clearImmediate(t);
}
};
BDFDB.TimeUtils.suppress = function (callback, string, name) {return function (...args) {
try {return callback(...args);}
catch (err) {BDFDB.LogUtils.error((typeof string == "string" && string || "") + " " + err, name);}
}};
BDFDB.LogUtils.log("Loading library."); BDFDB.LogUtils.log("Loading library.");
BDFDB.sameProto = function (a, b) { BDFDB.sameProto = function (a, b) {
@ -209,21 +245,23 @@ module.exports = (_ => {
} }
}; };
BDFDB.PluginUtils.cleanUp = function (plugin) { BDFDB.PluginUtils.cleanUp = function (plugin) {
if (!BDFDB.ObjectUtils.is(plugin)) return; BDFDB.TimeUtils.suppress(_ => {
if (plugin == BDFDB) { if (!BDFDB.ObjectUtils.is(plugin)) return;
delete window.BDFDB.loaded; if (plugin == BDFDB) {
BDFDB.TimeUtils.interval((interval, count) => { delete window.BDFDB_Global.loaded;
if (count > 60 || window.BDFDB.loaded) BDFDB.TimeUtils.clear(interval); BDFDB.TimeUtils.interval((interval, count) => {
if (window.BDFDB.loaded) for (let pluginName of PluginStores.started) BDFDB.BDUtils.reloadPlugin(pluginName); if (count > 60 || window.BDFDB_Global.loaded) BDFDB.TimeUtils.clear(interval);
}, 1000); if (window.BDFDB_Global.loaded) for (let pluginName of PluginStores.started) BDFDB.BDUtils.reloadPlugin(pluginName);
} }, 1000);
BDFDB.DOMUtils.removeLocalStyle(plugin.name); }
BDFDB.ListenerUtils.remove(plugin); BDFDB.DOMUtils.removeLocalStyle(plugin.name);
BDFDB.StoreChangeUtils.remove(plugin); BDFDB.ListenerUtils.remove(plugin);
BDFDB.ObserverUtils.disconnect(plugin); BDFDB.StoreChangeUtils.remove(plugin);
BDFDB.PatchUtils.unpatch(plugin); BDFDB.ObserverUtils.disconnect(plugin);
BDFDB.WindowUtils.closeAll(plugin); BDFDB.PatchUtils.unpatch(plugin);
BDFDB.WindowUtils.removeListener(plugin); BDFDB.WindowUtils.closeAll(plugin);
BDFDB.WindowUtils.removeListener(plugin);
}, "Failed to clean up plugin!", plugin.name)();
}; };
BDFDB.PluginUtils.checkUpdate = function (pluginName, url) { BDFDB.PluginUtils.checkUpdate = function (pluginName, url) {
if (pluginName && url && window.PluginUpdates.plugins[url]) return new Promise(callback => { if (pluginName && url && window.PluginUpdates.plugins[url]) return new Promise(callback => {
@ -461,14 +499,14 @@ module.exports = (_ => {
} }
}; };
window.BDFDB = Object.assign({ window.BDFDB_Global = Object.assign({
started: true, started: true,
loading: true, loading: true,
PluginUtils: { PluginUtils: {
buildPlugin: BDFDB.PluginUtils.buildPlugin, buildPlugin: BDFDB.PluginUtils.buildPlugin,
cleanUp: BDFDB.PluginUtils.cleanUp cleanUp: BDFDB.PluginUtils.cleanUp
} }
}, config, window.BDFDB); }, config, window.BDFDB_Global);
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/_res/BDFDB.data.json", (error, response, body) => { require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/_res/BDFDB.data.json", (error, response, body) => {
const InternalData = JSON.parse(body); const InternalData = JSON.parse(body);
@ -3419,40 +3457,6 @@ module.exports = (_ => {
} }
}; };
BDFDB.TimeUtils = {};
BDFDB.TimeUtils.interval = function (callback, delay, ...args) {
if (typeof callback != "function" || typeof delay != "number" || delay < 1) return;
else {
let count = 0, interval = setInterval(_ => {BDFDB.TimeUtils.suppress(callback, "Interval")(...[interval, count++, args].flat());}, delay);
return interval;
}
};
BDFDB.TimeUtils.timeout = function (callback, delay, ...args) {
delay = parseFloat(delay);
if (typeof callback != "function") return;
else if (isNaN(delay) || typeof delay != "number" || delay < 1) {
let immediate = setImmediate(_ => {BDFDB.TimeUtils.suppress(callback, "Immediate")(...[immediate, args].flat());});
return immediate;
}
else {
let timeout = setTimeout(_ => {BDFDB.TimeUtils.suppress(callback, "Timeout")(...[timeout, args].flat());}, delay);
return timeout;
}
};
BDFDB.TimeUtils.clear = function (...timeObjects) {
for (let t of timeObjects.flat(10).filter(n => n)) {
if (typeof t == "number") {
clearInterval(t);
clearTimeout(t);
}
else if (typeof t == "object") clearImmediate(t);
}
};
BDFDB.TimeUtils.suppress = function (callback, string, name) {return function (...args) {
try {return callback(...args);}
catch (err) {BDFDB.LogUtils.error((typeof string == "string" && string || "") + " " + err, name);}
}};
BDFDB.StringUtils = {}; BDFDB.StringUtils = {};
BDFDB.StringUtils.htmlEscape = function (string) { BDFDB.StringUtils.htmlEscape = function (string) {
let ele = document.createElement("div"); let ele = document.createElement("div");
@ -7280,7 +7284,7 @@ module.exports = (_ => {
InternalBDFDB.forceUpdateAll(); InternalBDFDB.forceUpdateAll();
const pluginQueue = window.BDFDB && BDFDB.ArrayUtils.is(window.BDFDB.pluginQueue) ? window.BDFDB.pluginQueue : []; const pluginQueue = window.BDFDB_Global && BDFDB.ArrayUtils.is(window.BDFDB_Global.pluginQueue) ? window.BDFDB_Global.pluginQueue : [];
if (BDFDB.UserUtils.me.id == myId) { if (BDFDB.UserUtils.me.id == myId) {
for (let module in DiscordClassModules) if (!DiscordClassModules[module]) BDFDB.LogUtils.warn(module + " not initialized in DiscordClassModules"); for (let module in DiscordClassModules) if (!DiscordClassModules[module]) BDFDB.LogUtils.warn(module + " not initialized in DiscordClassModules");
@ -7398,10 +7402,10 @@ module.exports = (_ => {
}; };
BDFDB.DevUtils.req = InternalBDFDB.getWebModuleReq(); BDFDB.DevUtils.req = InternalBDFDB.getWebModuleReq();
window.BDFDB = BDFDB; window.BDFDB_Global = BDFDB;
} }
else { else {
window.BDFDB = Object.assign({ window.BDFDB_Global = Object.assign({
PluginUtils: { PluginUtils: {
buildPlugin: BDFDB.PluginUtils.buildPlugin, buildPlugin: BDFDB.PluginUtils.buildPlugin,
cleanUp: BDFDB.PluginUtils.cleanUp cleanUp: BDFDB.PluginUtils.cleanUp
@ -7423,8 +7427,8 @@ module.exports = (_ => {
BDFDB.DOMUtils.appendLocalStyle("BDFDB", body.replace(/[\n\t\r]/g, "").replace(/\[REPLACE_CLASS_([A-z0-9_]+?)\]/g, function(a, b) {return BDFDB.dotCN[b];})); BDFDB.DOMUtils.appendLocalStyle("BDFDB", body.replace(/[\n\t\r]/g, "").replace(/\[REPLACE_CLASS_([A-z0-9_]+?)\]/g, function(a, b) {return BDFDB.dotCN[b];}));
}); });
window.BDFDB.loaded = true; window.BDFDB_Global.loaded = true;
delete window.BDFDB.loading; delete window.BDFDB_Global.loading;
BDFDB.LogUtils.log("Finished loading library."); BDFDB.LogUtils.log("Finished loading library.");