BetterDiscordAddons/Plugins/BDFDB.min.js

46 lines
2.6 KiB
JavaScript

if (window.BDFDB && window.BDFDB.myPlugins && Object.keys(window.BDFDB.myPlugins).length) BdApi.showConfirmationModal("BDFDB - Switching from remote to local library", `Due to recent security concerns, plugins are no longer allowed to load remote libraries. That's why I moved my plugin library to a plugin file and converted all my plugins so they use the local library. To download the library plugin and convert all plugins to the newest version press "Convert now". This might take a moment depending on how many of my plugins you are using. This will not delete any of your plugin configurations!\nFound plugins: ${Object.keys(window.BDFDB.myPlugins).join(", ")}`, {
confirmText: "Convert Now",
cancelText: "Cancel",
onConfirm: _ => {
let request = require("request"), fs = require("fs"), path = require("path");
let finish = _ => {
BdApi.alert("Finished converting", "The new library has been downloaded and all old plugins that were enabled have been converted. Old disabled plugins need to be converted once you enable them.");
};
let downloadPlugins = _ => {
let plugins = Object.assign({}, window.BDFDB.myPlugins), i = 0;
for (let name in plugins) if (plugins[name]) {
i++;
let last = i >= Object.keys(plugins).length;
clearTimeout(plugins[name].startTimeout);
setTimeout(_ => {
let url = typeof plugins[name].getRawUrl == "function" && typeof plugins[name].getRawUrl() == "string" ? plugins[name].getRawUrl() : `https://mwittrien.github.io/BetterDiscordAddons/Plugins/${name}/${name}.plugin.js`;
request(url, (error, response, body) => {
let filePath = path.join(BdApi.Plugins.folder, name + ".plugin.js");
if (!error && body && body.indexOf("/**") == 0 && body.indexOf(` * @name `) > -1) {
fs.writeFile(filePath, body, _ => {if (last) finish();});
}
else {
fs.exists(filePath, exists => {if (exists) fs.unlink(filePath, _ => {});});
let configPath = path.join(BdApi.Plugins.folder, name + ".config.json");
fs.exists(configPath, exists => {if (exists) fs.unlink(configPath, _ => {});});
if (last) finish();
}
});
}, i * 1000);
}
};
if (BdApi.Plugins.get("BDFDB")) downloadPlugins();
else request.get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (error, response, body) => {
fs.writeFile(path.join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), body, _ => {
let count = 0, interval = setInterval(_ => {
if (window.BDFDB_Global) {
clearInterval(interval);
downloadPlugins();
}
else if (count > 100) clearInterval(interval);
}, 1000);
});
});
}
});