dependency injection

This commit is contained in:
Zack Rauen 2020-07-18 20:54:55 -04:00
parent 640c5110f1
commit 6fa2960674
4 changed files with 52 additions and 4 deletions

File diff suppressed because one or more lines are too long

2
js/main.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -14,6 +14,23 @@ import DOM from "./domtools";
import BDLogo from "../ui/bdLogo";
import TooltipWrap from "../ui/tooltipWrap";
const dependencies = [
{
name: "jquery",
type: "script",
url: "//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js",
backup: "//cdn.jsdelivr.net/gh/jquery/jquery@2.0.0/jquery.min.js",
local: null
},
{
name: "bd-stylesheet",
type: "style",
url: "//cdn.staticaly.com/gh/{{repo}}/BetterDiscordApp/{{hash}}/css/main{{minified}}.css",
backup: "//rauenzi.github.io/BetterDiscordApp/css/main{{minified}}.css",
local: "{{localServer}}/BetterDiscordApp/css/main.css"
}
];
const {ipcRenderer} = require("electron");
function Core() {
ipcRenderer.invoke("bd-config", "get").then(injectorConfig => {
@ -83,7 +100,7 @@ Core.prototype.init = async function() {
});
this.injectExternals();
await this.injectExternals();
await this.checkForGuilds();
BDV2.initialize();
@ -147,6 +164,19 @@ Core.prototype.checkForGuilds = function() {
Core.prototype.injectExternals = async function() {
await DOM.addScript("ace-script", "https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js");
if (window.require.original) window.require = window.require.original;
if (window.$ && window.jQuery) return; // Dependencies already loaded
const jqueryUrl = Utils.formatString((bdConfig.local && dependencies[0].local != null) ? dependencies[0].local : dependencies[0].url, {repo: bdConfig.repo, hash: bdConfig.hash, minified: bdConfig.minified ? ".min" : "", localServer: bdConfig.localServer});
try {await DOM.addScript("jquery", jqueryUrl);}
catch (_) {
try {
const backup = Utils.formatString(dependencies[0].backup, {minified: bdConfig.minified ? ".min" : ""});
await DOM.addScript("jquery", backup);
}
catch (__) {
Utils.alert("jQuery Not Loaded", "Unable to load jQuery, some plugins may fail to work. Proceed at your own risk.");
}
}
document.head.append(DOM.createElement(`<link rel="stylesheet" href=${Utils.formatString((bdConfig.local && dependencies[1].local != null) ? dependencies[1].local : dependencies[1].url, {repo: bdConfig.repo, hash: bdConfig.hash, minified: bdConfig.minified ? ".min" : "", localServer: bdConfig.localServer})}>`));
};
Core.prototype.initSettings = function () {

View File

@ -71,6 +71,24 @@ export default class Utils {
}
}
/**
* Format strings with placeholders (`{{placeholder}}`) into full strings.
* Quick example: `PluginUtilities.formatString("Hello, {{user}}", {user: "Zerebos"})`
* would return "Hello, Zerebos".
* @param {string} string - string to format
* @param {object} values - object literal of placeholders to replacements
* @returns {string} the properly formatted string
*/
static formatString(string, values) {
for (const val in values) {
let replacement = values[val];
if (Array.isArray(replacement)) replacement = JSON.stringify(replacement);
if (typeof(replacement) === "object" && replacement !== null) replacement = replacement.toString();
string = string.replace(new RegExp(`{{${val}}}`, "g"), replacement);
}
return string;
}
static escape(s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
}