This commit is contained in:
Mirco Wittrien 2020-04-11 22:10:10 +02:00
parent 7ac1bf7662
commit 8e7890132d
2 changed files with 73 additions and 101 deletions

View File

@ -69,13 +69,17 @@ var PluginRepo = (_ => {
return class PluginRepo { return class PluginRepo {
getName () {return "PluginRepo";} getName () {return "PluginRepo";}
getVersion () {return "1.9.6";} getVersion () {return "1.9.7";}
getAuthor () {return "DevilBro";} getAuthor () {return "DevilBro";}
getDescription () {return "Allows you to look at all plugins from the plugin repo and download them on the fly. Repo button is in the plugins settings.";} getDescription () {return "Allows you to look at all plugins from the plugin repo and download them on the fly. Repo button is in the plugins settings.";}
constructor () { constructor () {
this.changelog = {
"improved":[["Loading","Switched from using and iFrame to using an extra Browser Window to load the plugin list, should be faster too"]]
};
this.patchedModules = { this.patchedModules = {
after: { after: {
V2C_ContentColumn: "render" V2C_ContentColumn: "render"
@ -260,12 +264,6 @@ var PluginRepo = (_ => {
BDFDB.DOMUtils.remove(".bd-pluginrepobutton", ".pluginrepo-notice", ".pluginrepo-loadingicon"); BDFDB.DOMUtils.remove(".bd-pluginrepobutton", ".pluginrepo-notice", ".pluginrepo-loadingicon");
let frame = document.querySelector("iframe.discordSandbox");
if (frame) {
window.removeEventListener("message", frame.messageReceived);
BDFDB.DOMUtils.remove(frame);
}
BDFDB.PluginUtils.clear(this); BDFDB.PluginUtils.clear(this);
} }
} }
@ -301,8 +299,8 @@ var PluginRepo = (_ => {
} }
getCustomList () { getCustomList () {
let customlist = BDFDB.DataUtils.load(this, "custom"); let customList = BDFDB.DataUtils.load(this, "custom");
return BDFDB.ArrayUtils.is(customlist) ? customlist : []; return BDFDB.ArrayUtils.is(customList) ? customList : [];
} }
openPluginRepoModal (options = {}) { openPluginRepoModal (options = {}) {
@ -494,16 +492,16 @@ var PluginRepo = (_ => {
name: BDFDB.LibraryComponents.SvgIcon.Names.GITHUB, name: BDFDB.LibraryComponents.SvgIcon.Names.GITHUB,
className: BDFDB.disCN._repoicon, className: BDFDB.disCN._repoicon,
onClick: _ => { onClick: _ => {
let giturl = null; let gitUrl = null;
if (plugin.url.indexOf("https://raw.githubusercontent.com") == 0) { if (plugin.url.indexOf("https://raw.githubusercontent.com") == 0) {
let temp = plugin.url.replace("//raw.githubusercontent", "//github").split("/"); let temp = plugin.url.replace("//raw.githubusercontent", "//github").split("/");
temp.splice(5, 0, "blob"); temp.splice(5, 0, "blob");
giturl = temp.join("/"); gitUrl = temp.join("/");
} }
else if (plugin.url.indexOf("https://gist.githubusercontent.com/") == 0) { else if (plugin.url.indexOf("https://gist.githubusercontent.com/") == 0) {
giturl = plugin.url.replace("//gist.githubusercontent", "//gist.github").split("/raw/")[0]; gitUrl = plugin.url.replace("//gist.githubusercontent", "//gist.github").split("/raw/")[0];
} }
if (giturl) BDFDB.DiscordUtils.openLink(giturl, BDFDB.DataUtils.get(this, "settings", "useChromium")); if (gitUrl) BDFDB.DiscordUtils.openLink(gitUrl, BDFDB.DataUtils.get(this, "settings", "useChromium"));
} }
}) })
}) })
@ -546,10 +544,10 @@ var PluginRepo = (_ => {
} }
loadPlugins () { loadPlugins () {
BDFDB.DOMUtils.remove("iframe.discordSandbox", ".pluginrepo-loadingicon"); BDFDB.DOMUtils.remove(".pluginrepo-loadingicon");
let settings = BDFDB.DataUtils.load(this, "settings"); let settings = BDFDB.DataUtils.load(this, "settings");
let getPluginInfo, extractConfigInfo, createFrame, runInFrame; let getPluginInfo, extractConfigInfo, createSandbox, runInSandbox;
let frame, frameRunning = false, frameQueue = [], outdated = 0, newentries = 0, i = 0; let sandbox, sandboxRunning = false, sandboxQueue = [], outdated = 0, newentries = 0, i = 0;
let tags = ["getName", "getVersion", "getAuthor", "getDescription"]; let tags = ["getName", "getVersion", "getAuthor", "getDescription"];
let seps = ["\"", "\'", "\`"]; let seps = ["\"", "\'", "\`"];
let newentriesdata = BDFDB.DataUtils.load(this, "newentriesdata"), customList = this.getCustomList(); let newentriesdata = BDFDB.DataUtils.load(this, "newentriesdata"), customList = this.getCustomList();
@ -571,32 +569,31 @@ var PluginRepo = (_ => {
} }
},1200000), amount:loading.amount+1}; },1200000), amount:loading.amount+1};
let loadingicon = BDFDB.DOMUtils.create(pluginRepoIcon); let loadingIcon = BDFDB.DOMUtils.create(pluginRepoIcon);
BDFDB.DOMUtils.addClass(loadingicon, "pluginrepo-loadingicon"); BDFDB.DOMUtils.addClass(loadingIcon, "pluginrepo-loadingicon");
loadingicon.addEventListener("mouseenter", _ => { loadingIcon.addEventListener("mouseenter", _ => {
BDFDB.TooltipUtils.create(loadingicon, this.getLoadingTooltipText(), { BDFDB.TooltipUtils.create(loadingIcon, this.getLoadingTooltipText(), {
type: "left", type: "left",
delay: 500, delay: 500,
style: "max-width: unset;", style: "max-width: unset;",
selector: "pluginrepo-loading-tooltip" selector: "pluginrepo-loading-tooltip"
}); });
}); });
BDFDB.PluginUtils.addLoadingIcon(loadingicon); BDFDB.PluginUtils.addLoadingIcon(loadingIcon);
createFrame().then(_ => { createSandbox().then(_ => {
getPluginInfo(_ => { getPluginInfo(_ => {
if (!this.started) { if (!this.started) {
BDFDB.TimeUtils.clear(loading.timeout); BDFDB.TimeUtils.clear(loading.timeout);
BDFDB.DOMUtils.remove(frame); BDFDB.WindowUtils.close(sandbox);
if (frame && frame.messageReceived) window.removeEventListener("message", frame.messageReceived);
return; return;
} }
let finishCounter = 0, finishInterval = BDFDB.TimeUtils.interval(_ => { let finishCounter = 0, finishInterval = BDFDB.TimeUtils.interval(_ => {
if ((frameQueue.length == 0 && !frameRunning) || finishCounter > 300 || !loading.is) { if ((sandboxQueue.length == 0 && !sandboxRunning) || finishCounter > 300 || !loading.is) {
BDFDB.TimeUtils.clear(loading.timeout); BDFDB.TimeUtils.clear(loading.timeout);
BDFDB.TimeUtils.clear(finishInterval); BDFDB.TimeUtils.clear(finishInterval);
BDFDB.DOMUtils.remove(frame, loadingicon, ".pluginrepo-loadingicon"); BDFDB.WindowUtils.close(sandbox);
if (frame && frame.messageReceived) window.removeEventListener("message", frame.messageReceived); BDFDB.DOMUtils.remove(loadingIcon, ".pluginrepo-loadingicon");
loading = {is:false, timeout:null, amount:loading.amount}; loading = {is:false, timeout:null, amount:loading.amount};
BDFDB.LogUtils.log("Finished fetching Plugins.", this.name); BDFDB.LogUtils.log("Finished fetching Plugins.", this.name);
@ -658,7 +655,7 @@ var PluginRepo = (_ => {
} }
}); });
getPluginInfo = (callback) => { getPluginInfo = callback => {
if (i >= foundPlugins.length || !this.started || !loading.is) { if (i >= foundPlugins.length || !this.started || !loading.is) {
callback(); callback();
return; return;
@ -717,9 +714,9 @@ var PluginRepo = (_ => {
if (this.isPluginOutdated(plugin, url)) outdated++; if (this.isPluginOutdated(plugin, url)) outdated++;
if (!cachedPlugins.includes(url)) newentries++; if (!cachedPlugins.includes(url)) newentries++;
} }
else if (frame && frame.contentWindow) { else if (sandbox) {
frameQueue.push({body, url}); sandboxQueue.push({body, url});
runInFrame(); runInSandbox();
} }
} }
i++; i++;
@ -746,82 +743,66 @@ var PluginRepo = (_ => {
} }
}; };
createFrame = _ => { createSandbox = _ => {
return new Promise(callback => { return new Promise(callback => {
frame = BDFDB.DOMUtils.create(`<iframe class="discordSandbox" style="position: absolute !important; opacity: 0 !important; pointer-events: none !important;" src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/ThemeRepo/res/DiscordPreview.html"></iframe>`); let loadTimeout = BDFDB.TimeUtils.timeout(_ => {
frame.startTimeout = BDFDB.TimeUtils.timeout(_ => {
callback(); callback();
},600000); }, 600000);
frame.messageReceived = e => { sandbox = BDFDB.WindowUtils.open(this, "https://mwittrien.github.io/BetterDiscordAddons/Plugins/ThemeRepo/res/DiscordPreview.html", {
if (!document.contains(frame)) { onLoad: _ => {
window.removeEventListener("message", frame.messageReceived); BDFDB.TimeUtils.clear(loadTimeout);
sandbox.executeJavaScriptSafe(`window.onmessage({
origin: "PluginRepo",
reason: "OnLoad",
classes: ${JSON.stringify(JSON.stringify(BDFDB.DiscordClasses))},
classModules: ${JSON.stringify(JSON.stringify(BDFDB.DiscordClassModules))}
})`);
callback();
} }
else if (typeof e.data === "object" && e.data.origin == "DiscordPreview") { });
switch (e.data.reason) {
case "OnLoad":
frame.contentWindow.postMessage({
origin: "PluginRepo",
reason: "OnLoad",
classes: JSON.stringify(BDFDB.DiscordClasses),
classmodules: JSON.stringify(BDFDB.DiscordClassModules)
}, "*");
callback();
break;
}
}
};
window.addEventListener("message", frame.messageReceived);
document.body.appendChild(frame);
}); });
} }
runInFrame = _ => { runInSandbox = _ => {
if (frameRunning) return; if (sandboxRunning) return;
let frameData = frameQueue.shift(); let sandboxData = sandboxQueue.shift();
if (!frameData) return; if (!sandboxData) return;
let {body, url} = frameData; let {body, url} = sandboxData;
let name = (body.replace(/\s*:\s*/g, ":").split('"name":"')[1] || "").split('"')[0]; let name = (body.replace(/\s*:\s*/g, ":").split('"name":"')[1] || "").split('"')[0];
name = name ? name : (body.replace(/ {2,}/g, " ").replace(/\r/g, "").split("@name ")[1] || "").split("\n")[0]; name = name ? name : (body.replace(/ {2,}/g, " ").replace(/\r/g, "").split("@name ")[1] || "").split("\n")[0];
if (name) { if (name) {
frameRunning = true; let messageId = (this.name + name + BDFDB.NumberUtils.generateId()).replace(/\s/g, "").trim();
let processResult = plugin => { sandboxRunning = true;
if (BDFDB.ObjectUtils.is(plugin)) { BDFDB.WindowUtils.addListener(this, messageId, (event, messageData) => {
plugin.url = url; BDFDB.WindowUtils.removeListener(this, messageId);
loadedPlugins[url] = plugin; if (BDFDB.ObjectUtils.is(messageData.plugin)) {
if (this.isPluginOutdated(plugin, url)) outdated++; messageData.plugin.url = url;
loadedPlugins[url] = messageData.plugin;
if (this.isPluginOutdated(messageData.plugin, url)) outdated++;
if (!cachedPlugins.includes(url)) newentries++; if (!cachedPlugins.includes(url)) newentries++;
} }
frameRunning = false; sandboxRunning = false;
runInFrame(); runInSandbox();
}; });
let evalResultReceived = e => { sandbox.executeJavaScriptSafe(`
if (typeof e.data === "object" && e.data.origin == "DiscordPreview") { let result = null;
switch (e.data.reason) {
case "EvalResult":
window.removeEventListener("message", evalResultReceived);
processResult(e.data.result);
break;
}
}
};
window.addEventListener("message", evalResultReceived);
if (frame.contentWindow) frame.contentWindow.postMessage({origin:"PluginRepo",reason:"Eval",jsstring:`
try { try {
${body}; ${body};
let p = new ${name}(); let p = new ${name}();
let data = { result = {
"getName":getString(p.getName()), "getName": getString(p.getName()),
"getAuthor":getString(p.getAuthor()), "getAuthor": getString(p.getAuthor()),
"getVersion":getString(p.getVersion()), "getVersion": getString(p.getVersion()),
"getDescription":getString(p.getDescription()) "getDescription": getString(p.getDescription())
}; };
window.evalResult = data;
} }
catch (err) { catch (err) {}
window.evalResult = null; window.respondToParent({
}` hostId: ${BDFDB.LibraryRequires.electron.remote.getCurrentWindow().webContents.id},
},"*"); hostName: "${messageId}",
plugin: result
});
`);
} }
} }
} }

View File

@ -86,15 +86,6 @@
}); });
} }
break; break;
case "Eval":
window.evalResult = null;
if (data.jsstring) window.eval(`(_ => {${data.jsstring}})()`);
window.respondToParent({
origin: "DiscordPreview",
reason: "EvalResult",
result: window.evalResult
});
break;
case "NewTheme": case "NewTheme":
case "CustomCSS": case "CustomCSS":
case "ThemeFixer": case "ThemeFixer":