2019-04-07 19:36:46 +02:00
|
|
|
window.global = window;
|
|
|
|
|
2018-10-11 10:21:26 +02:00
|
|
|
window.onload = function () {
|
|
|
|
window.parent.postMessage({origin:"DiscordPreview",reason:"OnLoad"},"*");
|
|
|
|
};
|
|
|
|
window.onkeyup = function (e) {
|
2019-01-14 21:47:27 +01:00
|
|
|
var which = e.which;
|
|
|
|
window.parent.postMessage({origin:"DiscordPreview",reason:"KeyUp",which},"*");
|
2018-10-11 10:21:26 +02:00
|
|
|
};
|
|
|
|
window.onmessage = function (e) {
|
2019-04-07 19:02:55 +02:00
|
|
|
if (typeof e.data === "object" && (e.data.origin == "PluginRepo" || e.data.origin == "ThemeRepo")) {
|
2018-10-11 10:21:26 +02:00
|
|
|
switch (e.data.reason) {
|
|
|
|
case "OnLoad":
|
2019-01-28 11:35:10 +01:00
|
|
|
document.body.innerHTML = document.body.innerHTML.replace(/\t|\n|\r/g, "");
|
2019-04-07 19:02:55 +02:00
|
|
|
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_USERNAME/gi, e.data.username);
|
|
|
|
}
|
|
|
|
if (e.data.id) document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_USERID/gi, e.data.id);
|
2019-07-29 18:26:48 +02:00
|
|
|
if (e.data.avatar) document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_AVATAR/gi, e.data.avatar.split('"').join('') + "?size=");
|
2019-04-07 19:02:55 +02:00
|
|
|
if (e.data.discriminator) document.body.innerHTML = document.body.innerHTML.replace(/REPLACE_DISCRIMINATOR/gi, e.data.discriminator);
|
2018-10-11 10:21:26 +02:00
|
|
|
if (e.data.nativecss) {
|
|
|
|
var theme = document.createElement("link");
|
|
|
|
theme.classList.add(e.data.reason);
|
|
|
|
theme.rel = "stylesheet";
|
|
|
|
theme.href = e.data.nativecss;
|
|
|
|
document.head.appendChild(theme);
|
|
|
|
}
|
2019-09-23 12:01:10 +02:00
|
|
|
if (e.data.html) document.documentElement.className = e.data.html;
|
2019-05-29 16:00:55 +02:00
|
|
|
if (e.data.titlebar) document.querySelector(".preview-titlebar").outerHTML = e.data.titlebar;
|
2019-01-14 18:22:22 +01:00
|
|
|
document.body.firstElementChild.style.removeProperty("display");
|
2018-10-11 10:21:26 +02:00
|
|
|
break;
|
2019-04-07 19:16:43 +02:00
|
|
|
case "Eval":
|
2019-04-07 19:29:17 +02:00
|
|
|
window.evalResult = null;
|
2019-04-07 19:32:03 +02:00
|
|
|
if (e.data.jsstring) window.eval(`(() => {${e.data.jsstring}})()`);
|
2019-04-07 19:16:43 +02:00
|
|
|
window.parent.postMessage({origin:"DiscordPreview",reason:"EvalResult",result:window.evalResult},"*");
|
|
|
|
break;
|
2018-10-11 10:21:26 +02:00
|
|
|
case "NewTheme":
|
|
|
|
case "CustomCSS":
|
|
|
|
case "ThemeFixer":
|
|
|
|
document.querySelectorAll("style." + e.data.reason).forEach(theme => theme.remove());
|
|
|
|
if (e.data.checked) {
|
|
|
|
var theme = document.createElement("style");
|
|
|
|
theme.classList.add(e.data.reason);
|
|
|
|
theme.innerText = e.data.css;
|
|
|
|
document.head.appendChild(theme);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "DarkLight":
|
2019-01-22 13:16:06 +01:00
|
|
|
if (e.data.checked) document.body.innerHTML = document.body.innerHTML.replace(new RegExp(e.data.dark, "g"), e.data.light);
|
|
|
|
else document.body.innerHTML = document.body.innerHTML.replace(new RegExp(e.data.light, "g"), e.data.dark);
|
|
|
|
break;
|
|
|
|
case "Normalize":
|
2019-01-22 13:19:47 +01:00
|
|
|
var oldhtml = document.body.innerHTML.split('class="');
|
|
|
|
var newhtml = oldhtml.shift();
|
2019-01-22 13:16:06 +01:00
|
|
|
for (let html of oldhtml) {
|
|
|
|
html = html.split('"');
|
2019-01-22 13:40:16 +01:00
|
|
|
newhtml += 'class="' + (e.data.checked ? html[0].replace(/([A-z0-9]+?)-([A-z0-9_-]{6})/g, "$1-$2 da-$1") : html[0].split(" ").filter(n => n.indexOf("da-") != 0).join(" ")) + '"' + html.slice(1).join('"');
|
2018-10-11 10:21:26 +02:00
|
|
|
}
|
2019-01-22 13:26:58 +01:00
|
|
|
document.body.innerHTML = newhtml;
|
2018-10-11 10:21:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-07 19:56:48 +02:00
|
|
|
window.require = function () {
|
|
|
|
return () => {};
|
|
|
|
};
|
2019-04-07 19:07:10 +02:00
|
|
|
window.getString = function (obj) {
|
|
|
|
var string = "";
|
|
|
|
if (typeof obj == "string") string = obj;
|
|
|
|
else if (obj && obj.props) {
|
|
|
|
if (typeof obj.props.children == "string") string = obj.props.children;
|
|
|
|
else if (Array.isArray(obj.props.children)) for (let c of obj.props.children) string += typeof c == "string" ? c : getString(c);
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
};
|
2019-04-07 20:02:30 +02:00
|
|
|
window.webpackJsonp = function () {
|
|
|
|
return {default:{m:{},c:{}}};
|
|
|
|
};
|
2019-04-07 19:07:10 +02:00
|
|
|
window.WebModulesFind = function (filter) {
|
|
|
|
const id = "PluginRepo-WebModules";
|
2019-04-07 19:43:31 +02:00
|
|
|
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]]]);
|
2019-04-07 19:07:10 +02:00
|
|
|
delete req.m[id];
|
|
|
|
delete req.c[id];
|
|
|
|
for (let m in req.c) {
|
|
|
|
if (req.c.hasOwnProperty(m)) {
|
|
|
|
var module = req.c[m].exports;
|
|
|
|
if (module && module.__esModule && module.default && filter(module.default)) return module.default;
|
|
|
|
if (module && filter(module)) return module;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.WebModulesFindByProperties = function (properties) {
|
|
|
|
properties = Array.isArray(properties) ? properties : Array.from(arguments);
|
|
|
|
var module = WebModulesFind(module => properties.every(prop => module[prop] !== undefined));
|
|
|
|
if (!module) {
|
|
|
|
module = {};
|
|
|
|
for (let property of properties) module[property] = property;
|
|
|
|
}
|
|
|
|
return module;
|
|
|
|
};
|
|
|
|
window.WebModulesFindByName = function (name) {
|
2019-05-07 21:20:29 +02:00
|
|
|
return WebModulesFind(module => module.displayName === name) || "";
|
2019-04-07 19:07:10 +02:00
|
|
|
};
|
|
|
|
window.BDV2 = {};
|
|
|
|
window.BDV2.react = window.React;
|
|
|
|
window.BDV2.reactDom = window.ReactDOM;
|
|
|
|
window.BDV2.WebpackModules = {};
|
|
|
|
window.BDV2.WebpackModules.find = window.WebModulesFind;
|
|
|
|
window.BDV2.WebpackModules.findByUniqueProperties = window.WebModulesFindByProperties;
|
|
|
|
window.BDV2.WebpackModules.findByDisplayName = window.WebModulesFindByName;
|
|
|
|
window.BdApi = {};
|
|
|
|
window.BdApi.React = window.React;
|
|
|
|
window.BdApi.ReactDOM = window.ReactDOM;
|
|
|
|
window.BdApi.findModule = window.WebModulesFind;
|
|
|
|
window.BdApi.findModuleByProps = window.WebModulesFindByProperties;
|