From 1de5ddbdd050f6376e35bf1eaf234276c5cac269 Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Mon, 23 Mar 2020 23:13:36 -0400 Subject: [PATCH] stop plugins using internal structures --- js/main.js | 93 +++++++++++++++++++++++--------------------------- js/main.min.js | 2 +- 2 files changed, 44 insertions(+), 51 deletions(-) diff --git a/js/main.js b/js/main.js index 8a0e3ed..39db626 100644 --- a/js/main.js +++ b/js/main.js @@ -2002,46 +2002,7 @@ ThemeModule.prototype.saveThemeData = function () { * * Plugin Template: https://gist.github.com/Jiiks/71edd5af0beafcd08956 */ -class AddonAPI { - constructor(cookie, list, manager) { - this._manager = manager; - this._cookie = cookie; - this._list = list; - } - isEnabled(name) { - return !!this._cookie[name]; - } - - enable(name) { - return this._manager.enable(name); - } - - disable(name) { - return this._manager.disable(name); - } - - toggle(name) { - if (this._cookie[name]) this.disable(name); - else this.enable(name); - } - - reload(name) { - return this._manager.reload(name); - } - - get(name) { - if (this._list.hasOwnProperty(name)) { - if (this._list[name].plugin) return this._list[name].plugin; - return this._list[name]; - } - return null; - } - - getAll() { - return Object.keys(this._list).map(k => this.get(k)).filter(a => a); - } -} var BdApi = { get React() { return BDV2.React; }, @@ -2063,17 +2024,7 @@ var BdApi = { get settings() {return settings;}, get emotes() {return window.bdEmotes;}, get screenWidth() { return Math.max(document.documentElement.clientWidth, window.innerWidth || 0); }, - get screenHeight() { return Math.max(document.documentElement.clientHeight, window.innerHeight || 0); }, - get Plugins() { - if (this._Plugins) return this._Plugins; - if (!pluginModule) return null; - return this._Plugins = new AddonAPI(pluginCookie, bdplugins, pluginModule); - }, - get Themes() { - if (this._Themes) return this._Themes; - if (!themeModule) return null; - return this._Themes = new AddonAPI(themeCookie, bdthemes, themeModule); - } + get screenHeight() { return Math.max(document.documentElement.clientHeight, window.innerHeight || 0); } }; BdApi.getAllWindowPreferences = function() { @@ -2304,7 +2255,49 @@ BdApi.setBDData = function(key, data) { return DataStore.setBDData(key, data); }; +((API) => { + const createAddonAPI = function(cookie, list, manager) { + return new class AddonAPI { + + isEnabled(name) { + return !!cookie[name]; + } + + enable(name) { + return manager.enable(name); + } + + disable(name) { + return manager.disable(name); + } + + toggle(name) { + if (cookie[name]) this.disable(name); + else this.enable(name); + } + + reload(name) { + return manager.reload(name); + } + + get(name) { + if (list.hasOwnProperty(name)) { + if (list[name].plugin) return list[name].plugin; + return list[name]; + } + return null; + } + + getAll() { + return Object.keys(list).map(k => this.get(k)).filter(a => a); + } + }; + }; + API.Themes = createAddonAPI(themeCookie, bdthemes, themeModule); + API.Plugins = createAddonAPI(pluginCookie, bdplugins, pluginModule); +})(BdApi); +Object.freeze(BdApi); diff --git a/js/main.min.js b/js/main.min.js index c5aa265..f881b47 100644 --- a/js/main.min.js +++ b/js/main.min.js @@ -52,4 +52,4 @@ - `),o=[a(e),a(t)];n.find(".tab-bar-item").on("click",t=>{t.preventDefault(),n.find(".tab-bar-item").removeClass("selected"),$(t.target).addClass("selected"),n.find(".scroller").empty().append(o[$(t.target).index()])}),n.find(".footer button").on("click",()=>{n.addClass("closing"),setTimeout(()=>{n.remove()},300)}),n.find(".bd-backdrop").on("click",()=>{n.addClass("closing"),setTimeout(()=>{n.remove()},300)}),n.appendTo("#app-mount"),e.length?n.find(".tab-bar-item")[0].click():n.find(".tab-bar-item")[1].click()},Core.prototype.showToast=function(e,t={}){if(!bdConfig.deferLoaded)return;if(!document.querySelector(".bd-toasts")){let e=document.createElement("div");e.classList.add("bd-toasts");let t=document.querySelector(".chat-3bRxxu form, #friends, .noChannel-Z1DQK7, .activityFeed-28jde9");e.style.setProperty("left",t?t.getBoundingClientRect().left+"px":"0px"),e.style.setProperty("width",t?t.offsetWidth+"px":"100%"),e.style.setProperty("bottom",(document.querySelector(".chat-3bRxxu form")?document.querySelector(".chat-3bRxxu form").offsetHeight:80)+"px"),document.querySelector(".app, .app-2rEoOp").appendChild(e)}const{type:a="",icon:n=!0,timeout:o=3e3}=t;let s=document.createElement("div");s.classList.add("bd-toast"),a&&s.classList.add("toast-"+a),a&&n&&s.classList.add("icon"),s.innerText=e,document.querySelector(".bd-toasts").appendChild(s),setTimeout(()=>{s.classList.add("closing"),setTimeout(()=>{s.remove(),document.querySelectorAll(".bd-toasts .bd-toast").length||document.querySelector(".bd-toasts").remove()},300)},o)},Core.prototype.showChangelogModal=function(e={}){const t=BdApi.findModuleByProps("push","update","pop","popWithKey"),a=BdApi.findModuleByProps("fixed","improved"),n=BdApi.findModuleByProps("Sizes","Weights"),o=BdApi.findModuleByProps("Child"),s=BdApi.findModuleByProps("Tags","default"),r=BdApi.findModule(e=>e.defaultProps&&!1==e.defaultProps.selectable),l=BdApi.findModuleByProps("defaultRules","parse");if(!r||!t||!a||!n||!o||!s||!l)return;const{image:c="https://repository-images.githubusercontent.com/105473537/957b5480-7c26-11e9-8401-50fa820cbae5",description:p="",changes:m=[],title:u="BandagedBD",subtitle:h=`v${bbdVersion}`,footer:d}=e,g=BdApi.React.createElement,i=[g("img",{src:c})];p&&i.push(g("p",null,l.parse(p)));for(let t=0;tg("li",null,l.parse(e))));i.push(s)}const f=function(){return g(o.Child,{grow:1,shrink:1},g(s.default,{tag:s.Tags.H4},u),g(n,{size:n.Sizes.SMALL,color:n.Colors.PRIMARY,className:a.date},h))},b=()=>{const e=BdApi.findModule(e=>"Anchor"==e.displayName),a=BdApi.findModuleByProps("anchorUnderlineOnHover")||{anchor:"anchor-3Z-8Bb",anchorUnderlineOnHover:"anchorUnderlineOnHover-2ESHQB"},s=e=>{e.preventDefault(),e.stopPropagation(),t.pop(),BDV2.joinBD2()},r=e?g(e,{onClick:s},"Join our Discord Server."):g("a",{className:`${a.anchor} ${a.anchorUnderlineOnHover}`,onClick:s},"Join our Discord Server."),i=g(n,{size:n.Sizes.SMALL,color:n.Colors.PRIMARY},"Need support? ",r);return g(o.Child,{grow:1,shrink:1},d?d:i)};t.push(function(e){return g(r,Object.assign({className:a.container,selectable:!0,onScroll:e=>e,onClose:e=>e,renderHeader:f,renderFooter:b,children:i},e))})},window.emotesFfz={},window.emotesBTTV={},window.emotesBTTV2={},window.emotesTwitch={},window.subEmotesTwitch={},window.bdEmotes={TwitchGlobal:{},TwitchSubscriber:{},BTTV:{},FrankerFaceZ:{},BTTV2:{}},window.bdEmoteSettingIDs={TwitchGlobal:"bda-es-7",TwitchSubscriber:"bda-es-7",BTTV:"bda-es-2",FrankerFaceZ:"bda-es-1",BTTV2:"bda-es-2"};function EmoteModule(){Object.defineProperty(this,"categories",{get:function(){const e=[];for(const t in window.bdEmoteSettingIDs)settingsCookie[window.bdEmoteSettingIDs[t]]&&e.push(t);return e}})}EmoteModule.prototype.init=async function(){this.modifiers=["flip","spin","pulse","spin2","spin3","1spin","2spin","3spin","tr","bl","br","shake","shake2","shake3","flap"],this.overrides=["twitch","bttv","ffz"];let e={TwitchGlobal:{url:`https://rauenzi.github.io/BetterDiscordApp/data/emotedata_twitch_global.json`,variable:"TwitchGlobal",oldVariable:"emotesTwitch",getEmoteURL:t=>`https://static-cdn.jtvnw.net/emoticons/v1/${t}/1.0`},TwitchSubscriber:{url:`https://rauenzi.github.io/BetterDiscordApp/data/emotedata_twitch_subscriber.json`,variable:"TwitchSubscriber",oldVariable:"subEmotesTwitch",getEmoteURL:t=>`https://static-cdn.jtvnw.net/emoticons/v1/${t}/1.0`},FrankerFaceZ:{url:`https://rauenzi.github.io/BetterDiscordApp/data/emotedata_ffz.json`,variable:"FrankerFaceZ",oldVariable:"emotesFfz",getEmoteURL:t=>`https://cdn.frankerfacez.com/emoticon/${t}/1`},BTTV:{url:`https://rauenzi.github.io/BetterDiscordApp/data/emotedata_bttv.json`,variable:"BTTV",oldVariable:"emotesBTTV",getEmoteURL:t=>`https://cdn.betterttv.net/emote/${t}/1x`},BTTV2:{url:`https://rauenzi.github.io/BetterDiscordApp/data/emotedata_bttv2.json`,variable:"BTTV2",oldVariable:"emotesBTTV2",getEmoteURL:t=>`https://cdn.betterttv.net/emote/${t}/1x`}};for(await this.getBlacklist(),await this.loadEmoteData(e);!BDV2.MessageComponent;)await new Promise(e=>setTimeout(e,100));this.cancelEmoteRender||(this.cancelEmoteRender=Utils.monkeyPatch(BDV2.MessageComponent,"default",{before:({methodArguments:e})=>{const t=e[0].childrenMessageContent.props.content;if(t&&t.length){for(let e=0;ei.length||bemotes.includes(i))continue;this.modifiers.includes(l)&&settingsCookie["bda-es-8"]||(l=""),this.overrides.includes(d)?l=d:d="";let c=this.categories[a];if("twitch"===d?window.bdEmotes.TwitchGlobal[i]?c="TwitchGlobal":window.bdEmotes.TwitchSubscriber[i]&&(c="TwitchSubscriber"):"bttv"===d?window.bdEmotes.BTTV[i]?c="BTTV":window.bdEmotes.BTTV2[i]&&(c="BTTV2"):"ffz"===d&&window.bdEmotes.FrankerFaceZ[i]&&(c="FrankerFaceZ"),!window.bdEmotes[c][i]||!settingsCookie[window.bdEmoteSettingIDs[c]])continue;const p=t[e].match(new RegExp(`([\\s]|^)${Utils.escape(l?i+":"+l:i)}([\\s]|$)`));if(!p)continue;const m=t[e].substring(0,p.index+p[1].length),u=t[e].substring(p.index+p[0].length-p[2].length);t[e]=m;const h=BDV2.react.createElement(BDEmote,{name:i,url:window.bdEmotes[c][i],modifier:l});t.splice(e+1,0,u),t.splice(e+1,0,h)}}const e=t.every(e=>"string"==typeof e&&""==e.replace(/\s*/,"")||!!(e.type&&"BDEmote"==e.type.name)||!!(e.props&&e.props.children&&e.props.children.props&&e.props.children.props.emojiName));if(e)for(let e of t)"object"==typeof e&&("BDEmote"==e.type.name?e.props.jumboable=!0:e.props&&e.props.children&&e.props.children.props&&e.props.children.props.emojiName&&(e.props.children.props.jumboable=!0))}}}))},EmoteModule.prototype.disable=function(){this.disableAutoCapitalize();this.cancelEmoteRender||(this.cancelEmoteRender(),this.cancelEmoteRender=null)},EmoteModule.prototype.clearEmoteData=async function(){let e=require("fs"),t=bdConfig.dataPath+"emote_data.json",a=e.existsSync(t);a&&e.unlinkSync(t),DataStore.setBDData("emoteCacheDate",new Date().toJSON()),window.bdEmotes={TwitchGlobal:{},TwitchSubscriber:{},BTTV:{},FrankerFaceZ:{},BTTV2:{}}},EmoteModule.prototype.isCacheValid=function(){const e=DataStore.getBDData("emoteCacheDays")||DataStore.setBDData("emoteCacheDays",7)||7,t=new Date(DataStore.getBDData("emoteCacheDate")||null),a=new Date,n=Math.round(Math.abs((a.getTime()-t.getTime())/86400000));return!(n>e)||(DataStore.setBDData("emoteCacheDate",a.toJSON()),!1)},EmoteModule.prototype.loadEmoteData=async function(t){const a=require("fs"),e=bdConfig.dataPath+"emote_data.json",n=await new Promise(t=>a.exists(e,t));if(n&&this.isCacheValid()){settingsCookie["fork-ps-2"]&&mainCore.showToast("Loading emotes from cache.",{type:"info"}),Utils.log("Emotes","Loading emotes from local cache.");const n=await new Promise(t=>{a.readFile(e,"utf8",(e,a)=>{Utils.log("Emotes","Emote file read."),e&&(a={}),t(a)})}),o=Utils.testJSON(n);let s=!!o;for(const a in s&&(window.bdEmotes=o),t)s=0a.unlink(e,t))}if(settingsCookie["fork-es-3"]){for(let a in settingsCookie["fork-ps-2"]&&mainCore.showToast("Downloading emotes in the background do not reload.",{type:"info"}),t){await new Promise(e=>setTimeout(e,1e3));let e=await this.downloadEmotes(t[a]);window.bdEmotes[t[a].variable]=e}settingsCookie["fork-ps-2"]&&mainCore.showToast("All emotes successfully downloaded.",{type:"success"});try{await new Promise(t=>a.writeFile(e,JSON.stringify(window.bdEmotes),"utf8",t))}catch(e){Utils.err("Emotes","Could not save emote data.",e)}}},EmoteModule.prototype.downloadEmotes=function(e){let t=require("request"),a={url:e.url,timeout:e.timeout?e.timeout:5e3,json:!0};return Utils.log("Emotes",`Downloading: ${e.variable} (${e.url})`),new Promise((n,o)=>{t(a,(t,a,s)=>{if(t)return Utils.err("Emotes","Could not download "+e.variable,t),e.backup?(e.url=e.backup,e.backup=null,e.backupParser&&(e.parser=e.backupParser),n(this.downloadEmotes(e))):o({});for(let n in"function"==typeof e.parser&&(s=e.parser(s)),s){if(4>n.length||bemotes.includes(n)){delete s[n];continue}s[n]=e.getEmoteURL(s[n])}n(s),Utils.log("Emotes","Downloaded: "+e.variable)})})},EmoteModule.prototype.getBlacklist=function(){return new Promise(e=>{$.getJSON(`https://rauenzi.github.io/BetterDiscordApp/data/emotefilter.json`,function(t){e(bemotes=t.blacklist)})})};var bemotes=[];EmoteModule.prototype.autoCapitalize=function(){!settingsCookie["bda-es-4"]||this.autoCapitalizeActive||($("body").on("keyup.bdac change.bdac paste.bdac",$(".channelTextArea-rNsIhG textarea:first"),()=>{var e=$(".channelTextArea-rNsIhG textarea:first").val();if(null!=e){var t=e.split(" ").pop();if(3",t+=" ",t+=" ",t+="