Update DiscordPreview.js

This commit is contained in:
Mirco Wittrien 2020-04-11 17:11:08 +02:00
parent 105557df50
commit 9c056f4391
1 changed files with 190 additions and 154 deletions

View File

@ -1,38 +1,50 @@
(_ => {
let DiscordClassModules, DiscordClasses, userId;
window.global = window; window.global = window;
window.onload = function () { window.onload = function () {
window.parent.postMessage({origin:"DiscordPreview",reason:"OnLoad"},"*"); window.parent.postMessage({
origin: "DiscordPreview",
reason: "OnLoad"
}, "*");
}; };
window.onkeyup = function (e) { window.onkeyup = function (e) {
var which = e.which; window.parent.postMessage({
window.parent.postMessage({origin:"DiscordPreview",reason:"KeyUp",which},"*"); origin: "DiscordPreview",
reason: "KeyUp",
which: e.which
} ,"*");
}; };
window.onmessage = function (e) { window.onmessage = function (e) {
if (typeof e.data === "object" && (e.data.origin == "PluginRepo" || e.data.origin == "ThemeRepo")) { if (typeof e.data === "object" && (e.data.origin == "PluginRepo" || e.data.origin == "ThemeRepo")) {
switch (e.data.reason) { switch (e.data.reason) {
case "OnLoad": case "OnLoad":
document.body.innerHTML = document.body.innerHTML.replace(/\t|\n|\r/g, ""); document.body.innerHTML = document.body.innerHTML.replace(/\t|\n|\r/g, "");
if (e.data.username) { if (e.data.username) {
document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_USERNAMESMALL/gi, e.data.username.toLowerCase()); document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_USERNAMESMALL/gi, e.data.username.toLowerCase());
document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_USERNAME/gi, e.data.username); document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_USERNAME/gi, e.data.username);
} }
if (e.data.id) { if (e.data.id) {
window.userId = e.data.id; userId = e.data.id;
document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_USERID/gi, e.data.id); document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_USERID/gi, e.data.id);
} }
if (e.data.avatar) document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_AVATAR/gi, e.data.avatar.split('"').join('') + "?size="); if (e.data.avatar) document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_AVATAR/gi, e.data.avatar.split('"').join('') + "?size=");
if (e.data.discriminator) document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_DISCRIMINATOR/gi, e.data.discriminator); if (e.data.discriminator) document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_DISCRIMINATOR/gi, e.data.discriminator);
if (e.data.classes) window.DiscordClasses = JSON.parse(e.data.classes); if (e.data.classes) DiscordClasses = JSON.parse(e.data.classes);
if (e.data.classmodules) window.DiscordClassModules = JSON.parse(e.data.classmodules); if (e.data.classmodules) DiscordClassModules = JSON.parse(e.data.classmodules);
if (window.disCN != undefined && window.DiscordClasses != undefined && window.DiscordClassModules != undefined) {
var oldhtml = document.body.innerHTML.split("REPLACE_CLASS_"); if (disCN != undefined && DiscordClasses != undefined && DiscordClassModules != undefined) {
var newhtml = oldhtml.shift(); var oldHTML = document.body.innerHTML.split("REPLACE_CLASS_");
for (let html of oldhtml) { var newHTML = oldHTML.shift();
for (let html of oldHTML) {
let reg = /([A-z0-9_]+)(.+)/.exec(html); let reg = /([A-z0-9_]+)(.+)/.exec(html);
newhtml += window.disCN[reg[1]] + reg[2]; newHTML += disCN[reg[1]] + reg[2];
} }
document.body.innerHTML = newhtml; document.body.innerHTML = newHTML;
} }
if (e.data.nativecss) { if (e.data.nativecss) {
var theme = document.createElement("link"); var theme = document.createElement("link");
theme.classList.add(e.data.reason); theme.classList.add(e.data.reason);
@ -40,15 +52,23 @@ window.onmessage = function (e) {
theme.href = e.data.nativecss; theme.href = e.data.nativecss;
document.head.appendChild(theme); document.head.appendChild(theme);
} }
if (e.data.html) document.documentElement.className = e.data.html; if (e.data.html) document.documentElement.className = e.data.html;
if (e.data.titlebar) document.querySelector(".preview-titlebar").outerHTML = e.data.titlebar;
document.body.firstElementChild.style.removeProperty("display");
document.documentElement.classList.add("mouse-mode"); document.documentElement.classList.add("mouse-mode");
document.documentElement.classList.add("full-motion");
if (e.data.titlebar) document.querySelector(".preview-titlebar").outerHTML = e.data.titlebar;
document.body.firstElementChild.style.removeProperty("display");
break; break;
case "Eval": case "Eval":
window.evalResult = null; window.evalResult = null;
if (e.data.jsstring) window.eval(`(_ => {${e.data.jsstring}})()`); if (e.data.jsstring) window.eval(`(_ => {${e.data.jsstring}})()`);
window.parent.postMessage({origin:"DiscordPreview",reason:"EvalResult",result:window.evalResult},"*"); window.parent.postMessage({
origin: "DiscordPreview",
reason: "EvalResult",
result: window.evalResult
}, "*");
break; break;
case "NewTheme": case "NewTheme":
case "CustomCSS": case "CustomCSS":
@ -62,26 +82,55 @@ window.onmessage = function (e) {
} }
break; break;
case "DarkLight": case "DarkLight":
if (e.data.checked) document.body.innerHTML = document.body.innerHTML.replace(new RegExp(window.disCN.themedark, "g"), window.disCN.themelight); if (e.data.checked) document.body.innerHTML = document.body.innerHTML.replace(new RegExp(disCN.themedark, "g"), disCN.themelight);
else document.body.innerHTML = document.body.innerHTML.replace(new RegExp(window.disCN.themelight, "g"), window.disCN.themedark); else document.body.innerHTML = document.body.innerHTML.replace(new RegExp(disCN.themelight, "g"), disCN.themedark);
break; break;
case "Normalize": case "Normalize":
var oldhtml = document.body.innerHTML.split('class="'); var oldHTML = document.body.innerHTML.split('class="');
var newhtml = oldhtml.shift(); var newHTML = oldHTML.shift();
for (let html of oldhtml) { for (let html of oldHTML) {
html = html.split('"'); html = html.split('"');
newhtml += 'class="' + (e.data.checked ? html[0].split(" ").map(n => n.replace(/([A-z0-9]+?)-([A-z0-9_-]{6})/g, "$1-$2 da-$1")).join(" ") : html[0].split(" ").filter(n => n.indexOf("da-") != 0).join(" ")) + '"' + html.slice(1).join('"'); newHTML += 'class="' + (e.data.checked ? html[0].split(" ").map(n => n.replace(/([A-z0-9]+?)-([A-z0-9_-]{6})/g, "$1-$2 da-$1")).join(" ") : html[0].split(" ").filter(n => n.indexOf("da-") != 0).join(" ")) + '"' + html.slice(1).join('"');
} }
document.body.innerHTML = newhtml; document.body.innerHTML = newHTML;
break; break;
} }
} }
}; };
let disCN = new Proxy({}, {
get: function (list, item) {
return getDiscordClass(item).replace('#', '');
}
});
let getDiscordClass = function (item) {
let className = "Preview_undefined";
if (DiscordClasses === undefined || DiscordClassModules === undefined) return className;
else if (DiscordClasses[item] === undefined) {
if (userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', item + ' not found in DiscordClasses');
return className;
}
else if (!Array.isArray(DiscordClasses[item]) || DiscordClasses[item].length != 2) {
if (userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', item + ' is not an Array of Length 2 in DiscordClasses');
return className;
}
else if (DiscordClassModules[DiscordClasses[item][0]] === undefined) {
if (userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', DiscordClasses[item][0] + ' not found in DiscordClassModules');
return className;
}
else if (DiscordClassModules[DiscordClasses[item][0]][DiscordClasses[item][1]] === undefined) {
if (userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', DiscordClasses[item][1] + ' not found in ' + DiscordClasses[item][0] + ' in DiscordClassModules');
return className;
}
else return className = DiscordClassModules[DiscordClasses[item][0]][DiscordClasses[item][1]];
};
window.require = function () { window.require = function () {
return _ => {}; return _ => {};
}; };
window.getString = function (obj) { window.getString = function (obj) {
var string = ""; let string = "";
if (typeof obj == "string") string = obj; if (typeof obj == "string") string = obj;
else if (obj && obj.props) { else if (obj && obj.props) {
if (typeof obj.props.children == "string") string = obj.props.children; if (typeof obj.props.children == "string") string = obj.props.children;
@ -89,10 +138,16 @@ window.getString = function (obj) {
} }
return string; return string;
}; };
window.webpackJsonp = function () { window.webpackJsonp = function () {
return {default:{m:{},c:{}}}; return {
default: {
m: {},
c: {}
}
}; };
window.WebModulesFind = function (filter) { };
let WebModulesFind = function (filter) {
const id = "PluginRepo-WebModules"; const id = "PluginRepo-WebModules";
const req = typeof(window.webpackJsonp) == "function" ? window.webpackJsonp([], {[id]: (module, exports, req) => exports.default = req}, [id]).default : window.webpackJsonp.push([[], {[id]: (module, exports, req) => module.exports = req}, [[id]]]); const req = typeof(window.webpackJsonp) == "function" ? window.webpackJsonp([], {[id]: (module, exports, req) => exports.default = req}, [id]).default : window.webpackJsonp.push([[], {[id]: (module, exports, req) => module.exports = req}, [[id]]]);
delete req.m[id]; delete req.m[id];
@ -105,56 +160,37 @@ window.WebModulesFind = function (filter) {
} }
} }
}; };
window.WebModulesFindByProperties = function (properties) { let WebModulesFindByProperties = function (properties) {
properties = Array.isArray(properties) ? properties : Array.from(arguments); properties = Array.isArray(properties) ? properties : Array.from(arguments);
var module = WebModulesFind(module => properties.every(prop => module[prop] !== undefined)); let module = WebModulesFind(module => properties.every(prop => module[prop] !== undefined));
if (!module) { if (!module) {
module = {}; module = {};
for (let property of properties) module[property] = property; for (let property of properties) module[property] = property;
} }
return module; return module;
}; };
window.WebModulesFindByName = function (name) { let WebModulesFindByName = function (name) {
return WebModulesFind(module => module.displayName === name) || ""; return WebModulesFind(module => module.displayName === name) || "";
}; };
window.disCN = new Proxy({}, {
get: function (list, item) { if (!window.BDV2) {
return window.getDiscordClass(item).replace('#', '');
}
});
window.getDiscordClass = function (item) {
var classname = "Preview_undefined";
if (window.DiscordClasses === undefined || window.DiscordClassModules === undefined) return classname;
else if (window.DiscordClasses[item] === undefined) {
if (window.userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', item + ' not found in window.DiscordClasses');
return classname;
}
else if (!Array.isArray(window.DiscordClasses[item]) || window.DiscordClasses[item].length != 2) {
if (window.userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', item + ' is not an Array of Length 2 in window.DiscordClasses');
return classname;
}
else if (window.DiscordClassModules[window.DiscordClasses[item][0]] === undefined) {
if (window.userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', window.DiscordClasses[item][0] + ' not found in DiscordClassModules');
return classname;
}
else if (window.DiscordClassModules[window.DiscordClasses[item][0]][window.DiscordClasses[item][1]] === undefined) {
if (window.userId == "278543574059057154") console.warn(`%c[Preview]%c`, 'color:#3a71c1; font-weight:700;', '', window.DiscordClasses[item][1] + ' not found in ' + window.DiscordClasses[item][0] + ' in DiscordClassModules');
return classname;
}
else return classname = window.DiscordClassModules[window.DiscordClasses[item][0]][window.DiscordClasses[item][1]];
};
window.BDV2 = {}; window.BDV2 = {};
window.BDV2.react = window.React; window.BDV2.react = window.React;
window.BDV2.reactDom = window.ReactDOM; window.BDV2.reactDom = window.ReactDOM;
window.BDV2.WebpackModules = {}; window.BDV2.WebpackModules = {};
window.BDV2.WebpackModules.find = window.WebModulesFind; window.BDV2.WebpackModules.find = WebModulesFind;
window.BDV2.WebpackModules.findByUniqueProperties = window.WebModulesFindByProperties; window.BDV2.WebpackModules.findByUniqueProperties = WebModulesFindByProperties;
window.BDV2.WebpackModules.findByDisplayName = window.WebModulesFindByName; window.BDV2.WebpackModules.findByDisplayName = WebModulesFindByName;
}
if (!window.BdApi) {
window.BdApi = {}; window.BdApi = {};
window.BdApi.getData = _ => {return {};}; window.BdApi.getData = _ => {return {};};
window.BdApi.loadData = _ => {return {};}; window.BdApi.loadData = _ => {return {};};
window.BdApi.saveData = _ => {}; window.BdApi.saveData = _ => {};
window.BdApi.React = window.React; window.BdApi.React = window.React;
window.BdApi.ReactDOM = window.ReactDOM; window.BdApi.ReactDOM = window.ReactDOM;
window.BdApi.findModule = window.WebModulesFind; window.BdApi.findModule = WebModulesFind;
window.BdApi.findModuleByProps = window.WebModulesFindByProperties; window.BdApi.findModuleByProps = WebModulesFindByProperties;
window.BdApi.findModuleByDisplayName = WebModulesFindByName;
}
})();