BetterDiscordApp-rauenzi/src/modules/bdApi.js

278 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-03-25 05:19:02 +01:00
import {pluginCookie, themeCookie, bdplugins, bdthemes, settingsCookie, settings, bdEmotes} from "../0globals";
2020-02-27 08:01:51 +01:00
import mainCore from "./core";
import Utils from "./utils";
import BDV2 from "./v2";
import DataStore from "./dataStore";
2020-03-15 00:47:37 +01:00
import pluginModule from "./pluginModule";
import themeModule from "./themeModule";
import settingsPanel from "./settingsPanel";
2020-03-29 11:17:12 +02:00
import DOM from "./domtools";
2020-02-27 08:01:51 +01:00
const BdApi = {
2020-03-15 00:47:37 +01:00
get React() { return BDV2.React; },
get ReactDOM() { return BDV2.ReactDom; },
get ReactComponent() {return BDV2.ReactComponent;},
2020-03-28 04:31:28 +01:00
get WindowConfigFile() {return Utils.WindowConfigFile;},
2020-03-21 05:27:56 +01:00
get settings() {return settings;},
2020-03-15 00:47:37 +01:00
get emotes() {return bdEmotes;},
get screenWidth() { return Math.max(document.documentElement.clientWidth, window.innerWidth || 0); },
get screenHeight() { return Math.max(document.documentElement.clientHeight, window.innerHeight || 0); }
2020-02-27 08:01:51 +01:00
};
BdApi.getAllWindowPreferences = function() {
2020-03-28 04:31:28 +01:00
return Utils.getAllWindowPreferences();
2020-02-27 08:01:51 +01:00
};
BdApi.getWindowPreference = function(key) {
2020-03-28 04:31:28 +01:00
return Utils.getWindowPreference(key);
2020-02-27 08:01:51 +01:00
};
BdApi.setWindowPreference = function(key, value) {
2020-03-28 04:31:28 +01:00
return Utils.setWindowPreference(key, value);
2020-02-27 08:01:51 +01:00
};
//Inject CSS to document head
//id = id of element
//css = custom css
BdApi.injectCSS = function (id, css) {
2020-03-29 11:17:12 +02:00
DOM.addStyle(DOM.escapeID(id), css);
2020-02-27 08:01:51 +01:00
};
//Clear css/remove any element
//id = id of element
BdApi.clearCSS = function (id) {
2020-03-29 11:17:12 +02:00
DOM.removeStyle(DOM.escapeID(id));
2020-02-27 08:01:51 +01:00
};
//Inject CSS to document head
//id = id of element
//css = custom css
BdApi.linkJS = function (id, url) {
2020-03-29 11:17:12 +02:00
DOM.addScript(DOM.escapeID(id), url);
2020-02-27 08:01:51 +01:00
};
//Clear css/remove any element
//id = id of element
BdApi.unlinkJS = function (id) {
2020-03-29 11:17:12 +02:00
DOM.removeScript(DOM.escapeID(id));
2020-02-27 08:01:51 +01:00
};
//Get another plugin
//name = name of plugin
BdApi.getPlugin = function (name) {
if (bdplugins.hasOwnProperty(name)) {
return bdplugins[name].plugin;
}
return null;
};
//Get BetterDiscord Core
BdApi.getCore = function () {
2020-03-28 03:44:59 +01:00
Utils.warn("Deprecation Notice", `BdApi.getCore() will be removed in future versions.`);
2020-02-27 08:01:51 +01:00
return mainCore;
};
/**
* Shows a generic but very customizable modal.
* @param {string} title - title of the modal
* @param {string} content - a string of text to display in the modal
*/
BdApi.alert = function (title, content) {
const ModalStack = BdApi.findModuleByProps("push", "update", "pop", "popWithKey");
const AlertModal = BdApi.findModuleByPrototypes("handleCancel", "handleSubmit", "handleMinorConfirm");
if (!ModalStack || !AlertModal) return mainCore.alert(title, content);
ModalStack.push(function(props) {
return BdApi.React.createElement(AlertModal, Object.assign({
title: title,
body: content,
}, props));
});
};
/**
* Shows a generic but very customizable confirmation modal with optional confirm and cancel callbacks.
* @param {string} title - title of the modal
* @param {(string|ReactElement|Array<string|ReactElement>)} children - a single or mixed array of react elements and strings. Everything is wrapped in Discord's `TextElement` component so strings will show and render properly.
* @param {object} [options] - options to modify the modal
* @param {boolean} [options.danger=false] - whether the main button should be red or not
* @param {string} [options.confirmText=Okay] - text for the confirmation/submit button
* @param {string} [options.cancelText=Cancel] - text for the cancel button
* @param {callable} [options.onConfirm=NOOP] - callback to occur when clicking the submit button
* @param {callable} [options.onCancel=NOOP] - callback to occur when clicking the cancel button
*/
BdApi.showConfirmationModal = function (title, content, options = {}) {
const ModalStack = BdApi.findModuleByProps("push", "update", "pop", "popWithKey");
const TextElement = BdApi.findModuleByProps("Sizes", "Weights");
const ConfirmationModal = BdApi.findModule(m => m.defaultProps && m.key && m.key() == "confirm-modal");
if (!ModalStack || !ConfirmationModal || !TextElement) return mainCore.alert(title, content);
const {onConfirm, onCancel, confirmText, cancelText, danger = false} = options;
if (typeof(content) == "string") content = TextElement({color: TextElement.Colors.PRIMARY, children: [content]});
else if (Array.isArray(content)) content = TextElement({color: TextElement.Colors.PRIMARY, children: content});
content = [content];
const emptyFunction = () => {};
ModalStack.push(function(props) {
return BdApi.React.createElement(ConfirmationModal, Object.assign({
header: title,
children: content,
red: danger,
confirmText: confirmText ? confirmText : "Okay",
cancelText: cancelText ? cancelText : "Cancel",
onConfirm: onConfirm ? onConfirm : emptyFunction,
onCancel: onCancel ? onCancel : emptyFunction
}, props));
});
};
//Show toast alert
BdApi.showToast = function(content, options = {}) {
Utils.showToast(content, options);
};
// Finds module
BdApi.findModule = function(filter) {
return BDV2.WebpackModules.find(filter);
};
// Finds module
BdApi.findAllModules = function(filter) {
return BDV2.WebpackModules.findAll(filter);
};
// Finds module
BdApi.findModuleByProps = function(...props) {
return BDV2.WebpackModules.findByUniqueProperties(props);
};
BdApi.findModuleByPrototypes = function(...protos) {
return BDV2.WebpackModules.findByPrototypes(protos);
};
BdApi.findModuleByDisplayName = function(name) {
return BDV2.WebpackModules.findByDisplayName(name);
};
// Gets react instance
BdApi.getInternalInstance = function(node) {
if (!(node instanceof window.jQuery) && !(node instanceof Element)) return undefined;
if (node instanceof jQuery) node = node[0];
return BDV2.getInternalInstance(node);
};
// Gets data
BdApi.loadData = function(pluginName, key) {
return DataStore.getPluginData(pluginName, key);
};
BdApi.getData = BdApi.loadData;
// Sets data
BdApi.saveData = function(pluginName, key, data) {
return DataStore.setPluginData(pluginName, key, data);
};
BdApi.setData = BdApi.saveData;
// Deletes data
BdApi.deleteData = function(pluginName, key) {
return DataStore.deletePluginData(pluginName, key);
};
// Patches other functions
BdApi.monkeyPatch = function(what, methodName, options) {
return Utils.monkeyPatch(what, methodName, options);
};
// Event when element is removed
BdApi.onRemoved = function(node, callback) {
return Utils.onRemoved(node, callback);
};
// Wraps function in try..catch
BdApi.suppressErrors = function(method, message) {
return Utils.suppressErrors(method, message);
};
// Tests for valid JSON
BdApi.testJSON = function(data) {
return Utils.testJSON(data);
};
BdApi.isPluginEnabled = function(name) {
return !!pluginCookie[name];
};
BdApi.isThemeEnabled = function(name) {
return !!themeCookie[name];
};
BdApi.isSettingEnabled = function(id) {
return !!settingsCookie[id];
};
2020-03-15 00:47:37 +01:00
BdApi.enableSetting = function(id) {
return settingsPanel.onChange(id, true);
};
BdApi.disableSetting = function(id) {
return settingsPanel.onChange(id, false);
};
BdApi.toggleSetting = function(id) {
return settingsPanel.onChange(id, !settingsCookie[id]);
};
2020-02-27 08:01:51 +01:00
// Gets data
BdApi.getBDData = function(key) {
return DataStore.getBDData(key);
};
// Sets data
BdApi.setBDData = function(key, data) {
return DataStore.setBDData(key, data);
};
2020-03-15 00:47:37 +01:00
2020-03-24 04:21:22 +01:00
const makeAddonAPI = (cookie, list, manager) => new class AddonAPI {
2020-03-21 05:27:56 +01:00
2020-03-15 00:47:37 +01:00
isEnabled(name) {
2020-03-24 04:21:22 +01:00
return !!cookie[name];
2020-03-15 00:47:37 +01:00
}
enable(name) {
2020-03-24 04:21:22 +01:00
return manager.enable(name);
2020-03-15 00:47:37 +01:00
}
disable(name) {
2020-03-24 04:21:22 +01:00
return manager.disable(name);
2020-03-15 00:47:37 +01:00
}
toggle(name) {
2020-03-24 04:21:22 +01:00
if (cookie[name]) this.disable(name);
2020-03-15 00:47:37 +01:00
else this.enable(name);
}
2020-03-23 14:35:49 +01:00
reload(name) {
2020-03-24 04:21:22 +01:00
return manager.reload(name);
2020-03-23 14:35:49 +01:00
}
2020-03-15 00:47:37 +01:00
get(name) {
2020-03-24 04:21:22 +01:00
if (list.hasOwnProperty(name)) {
if (list[name].plugin) return list[name].plugin;
return list[name];
2020-03-15 00:47:37 +01:00
}
return null;
}
getAll() {
2020-03-24 04:21:22 +01:00
return Object.keys(list).map(k => this.get(k)).filter(a => a);
2020-03-15 00:47:37 +01:00
}
2020-03-24 04:21:22 +01:00
};
BdApi.Plugins = makeAddonAPI(pluginCookie, bdplugins, pluginModule);
BdApi.Themes = makeAddonAPI(themeCookie, bdthemes, themeModule);
2020-03-15 00:47:37 +01:00
2020-02-27 08:01:51 +01:00
export default BdApi;