//META{"name":"ThemeSettings","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ThemeSettings","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ThemeSettings/ThemeSettings.plugin.js"}*// class ThemeSettings { getName () {return "ThemeSettings";} getVersion () {return "1.1.0";} getAuthor () {return "DevilBro";} getDescription () {return "Allows you to change Theme Variables within BetterDiscord. Adds a Settings button (similar to Plugins) to customizable Themes in your Themes Page.";} initConstructor () { this.patchModules = { "V2C_ThemeCard":"componentDidMount" }; } //legacy load () {} start () { if (!global.BDFDB) global.BDFDB = {myPlugins:{}}; if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this; var libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]'); if (!libraryScript || performance.now() - libraryScript.getAttribute("date") > 600000) { if (libraryScript) libraryScript.remove(); libraryScript = document.createElement("script"); libraryScript.setAttribute("type", "text/javascript"); libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"); libraryScript.setAttribute("date", performance.now()); libraryScript.addEventListener("load", () => {if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();}); document.head.appendChild(libraryScript); } else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize(); this.startTimeout = setTimeout(() => {this.initialize();}, 30000); } initialize () { if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) { if (this.started) return; BDFDB.loadMessage(this); this.fs = require("fs"); this.path = require("path"); this.dir = BDFDB.getThemesFolder(); BDFDB.WebModules.forceAllUpdates(this); } else { console.error(`%c[${this.getName()}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!'); } } stop () { if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) { BDFDB.removeEles(".themes-settings-button",".themes-settings-footer"); BDFDB.unloadMessage(this); } } // begin of own functions processV2CThemeCard (instance, wrapper) { if (instance.props && instance.props.theme && !wrapper.querySelector(BDFDB.dotCN._reposettingsbutton + ".themes-settings-button")) { let vars = this.getThemeVars(instance.props.theme.css); if (vars.length) { let footer = wrapper.querySelector(BDFDB.dotCN._repofooter); if (!footer) { footer = document.createElement("div"); footer.className = BDFDB.disCNS._repofooter + "themes-settings-footer"; wrapper.appendChild(footer); } let button = document.createElement("button"); button.className = BDFDB.disCNS._reposettingsbutton + "themes-settings-button"; button.innerText = "Settings"; footer.appendChild(button); button.addEventListener("click", () => { BDFDB.addClass(wrapper, BDFDB.disCN._reposettingsopen); BDFDB.removeClass(wrapper, BDFDB.disCN._reposettingsclosed); let children = []; while (wrapper.childElementCount) { children.push(wrapper.firstChild); wrapper.firstChild.remove(); } let closebutton = BDFDB.htmlToElement(`
`); wrapper.appendChild(closebutton); closebutton.addEventListener("click", () => { BDFDB.removeClass(wrapper, BDFDB.disCN._reposettingsopen); BDFDB.addClass(wrapper, BDFDB.disCN._reposettingsclosed); while (wrapper.childElementCount) wrapper.firstChild.remove(); while (children.length) wrapper.appendChild(children.shift()); }) this.createThemeSettings(wrapper, instance.props.theme, vars); }); } } } getThemeVars (css) { let vars = css.split(":root"); if (vars.length > 1) { vars = vars[1].replace(/\t| {2,}/g,"").replace(/\n\/\*.*?\*\//g,"").replace(/[\n\r]/g,""); vars = vars.split("{"); vars.shift(); vars = vars.join("{").replace(/\s*(:|;|--|\*)\s*/g,"$1"); vars = vars.split("}")[0]; return vars.slice(2).split(/;--|\*\/--/); } return []; } createThemeSettings (wrapper, theme, vars) { if (!global.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return; var settingshtml = `
${BDFDB.encodeToHTML(theme.name)}

Update all variables

`; let settingspanel = BDFDB.htmlToElement(settingshtml); var settingspanelinner = settingspanel.querySelector(".BDFDB-settings-inner"); var maxwidth = BDFDB.getRects(wrapper).width - 80; for (let varstr of vars) { varstr = varstr.split(":"); let varname = varstr.shift().trim(); varstr = varstr.join(":").split(/;[^A-z0-9]|\/\*/); let varvalue = varstr.shift().trim(); let vardescription = varstr.join("").replace(/\*\/|\/\*/g, "").replace(/:/g, ": ").replace(/: \//g, ":/").replace(/--/g, " --").replace(/\( --/g, "(--").trim(); vardescription = vardescription && vardescription.indexOf("*") == 0 ? vardescription.slice(1) : vardescription; var varcontainer = BDFDB.htmlToElement(`

${varname[0].toUpperCase() + varname.slice(1)}:

${vardescription ? '
' + BDFDB.encodeToHTML(vardescription) + '
' : ""}${vars[vars.length-1] == varstr ? '' : '
'}
`); let varinput = varcontainer.querySelector(BDFDB.dotCN.input); varinput.value = varvalue || ""; varinput.setAttribute("placeholder", varvalue || ""); settingspanelinner.appendChild(varcontainer); } BDFDB.initElements(settingspanel, this); BDFDB.addEventListener(this, settingspanel, "click", ".update-button", () => { let path = this.path.join(this.dir, theme.filename); let css = this.fs.readFileSync(path).toString(); if (css) { let amount = 0; for (let input of settingspanel.querySelectorAll(BDFDB.dotCN.input)) { let oldvalue = input.getAttribute("placeholder"); let newvalue = input.value; if (newvalue && newvalue.trim() && newvalue != oldvalue) { let varname = input.getAttribute("option"); css = css.replace(new RegExp(`--${BDFDB.regEscape(varname)}(\\s*):(\\s*)${BDFDB.regEscape(oldvalue)}`,"g"),`--${varname}$1:$2${newvalue}`); amount++; } } if (amount > 0) { this.fs.writeFileSync(path, css); BDFDB.showToast(`Updated ${amount} variable${amount == 1 ? "" : "s"} in ${theme.filename}`, {type:"success"}); } else BDFDB.showToast(`There are no changed variables to be updated in ${theme.filename}`, {type:"warning"}); } else BDFDB.showToast(`Could not find themefile: ${theme.filename}`, {type:"error"}); }); wrapper.appendChild(settingspanel); } }