Revert "update mod loader"

This reverts commit 4e853a6185.
new-bloop
Your New SJW Waifu 4 years ago
parent 8af25ed088
commit 5e88bd9a30

@ -11,11 +11,6 @@ function PleromaModLoader () {
function loadMods () { function loadMods () {
for (const mod of this.config.mods) { for (const mod of this.config.mods) {
const modObject = new PleromaMod(mod); const modObject = new PleromaMod(mod);
modObject.enabled = true;
if (localStorage.getItem("pleroma_mod_" + mod + "_enabled") === "false") {
modObject.enabled = false;
}
localStorage.setItem("pleroma_mod_" + mod + "_enabled", modObject.enabled);
modObject.include(); modObject.include();
this.loadedMods[mod] = modObject; this.loadedMods[mod] = modObject;
} }
@ -49,17 +44,9 @@ function PleromaModLoader () {
const loginPanel = document.getElementsByClassName("login-form"); const loginPanel = document.getElementsByClassName("login-form");
if (postPanel.length > 0 || loginPanel.length > 0) { if (postPanel.length > 0 || loginPanel.length > 0) {
for (var modName in this.loadedMods) { for (var modName in this.loadedMods) {
const settings = document.querySelector(".settings div[label]:first-child");
if (settings) {
if (!settings.querySelector(".mod-settings")) {
this.appendModSettings(settings);
}
}
const mod = this.loadedMods[modName]; const mod = this.loadedMods[modName];
if (mod.enabled && mod.instance) { if (mod.instance.onReady) {
if (mod.instance.onReady) { mod.instance.onReady();
mod.instance.onReady();
}
} }
} }
this.createObserver(); this.createObserver();
@ -68,63 +55,6 @@ function PleromaModLoader () {
} }
}, },
function createCheckbox (label, mod) {
const labelElement = document.createElement("label");
labelElement.classList.add("checkbox");
const input = document.createElement("input");
input.setAttribute("type", "checkbox");
input.checked = mod.enabled;
input.addEventListener("change", (event) => {
if (event.target.checked) {
mod.enable();
} else {
mod.disable();
}
});
labelElement.appendChild(input);
const fakeCheckbox = document.createElement("i");
fakeCheckbox.classList.add("checkbox-indicator");
labelElement.appendChild(fakeCheckbox);
const text = document.createElement("span");
text.classList.add("label");
text.innerText = label;
labelElement.appendChild(text);
return labelElement;
},
function appendModSettings (element) {
const container = document.createElement("div");
container.classList.add("setting-item");
container.classList.add("mod-settings");
const title = document.createElement("h2");
title.innerText = "Mods";
container.appendChild(title);
const optionList = document.createElement("ul");
optionList.classList.add("setting-list");
const modNames = Object.keys(this.loadedMods).sort();
for (const mod of modNames) {
const li = document.createElement("li");
const enable = this.createCheckbox("enable " + mod, this.loadedMods[mod]);
li.appendChild(enable);
optionList.appendChild(li);
}
container.appendChild(optionList);
element.appendChild(container);
},
function createObserver () { function createObserver () {
this.containers = { this.containers = {
main: document.getElementsByClassName("main")[0], main: document.getElementsByClassName("main")[0],
@ -133,26 +63,16 @@ function PleromaModLoader () {
const observerConfig = { subtree: true, childList: true }; const observerConfig = { subtree: true, childList: true };
this.observer = new MutationObserver((mutations, observer) => { this.observer = new MutationObserver((mutations, observer) => {
if (
mutations.length > 0 &&
mutations[0].addedNodes.length > 0 &&
mutations[0].addedNodes[0].classList &&
mutations[0].addedNodes[0].classList.contains("settings")
) {
this.appendModSettings(mutations[0].addedNodes[0].querySelector("div[label]:first-child"));
}
for (var modName in this.loadedMods) { for (var modName in this.loadedMods) {
const mod = this.loadedMods[modName]; const mod = this.loadedMods[modName];
if (mod.instance && mod.enabled) { if (mod.instance.onMutation) {
if (mod.instance.onMutation) { for (const mutation of mutations) {
for (const mutation of mutations) { let filter = null;
let filter = null; if (mod.instance.config.filter) {
if (mod.instance.config.filter) { filter = new RegExp(mod.instance.config.filter.join("|"));
filter = new RegExp(mod.instance.config.filter.join("|")); }
} if (!filter || filter.test(mutation.target.className)) {
if (!filter || filter.test(mutation.target.className)) { mod.instance.onMutation(mutation, observer);
mod.instance.onMutation(mutation, observer);
}
} }
} }
} }
@ -208,7 +128,6 @@ function PleromaModLoader () {
function PleromaMod (name) { function PleromaMod (name) {
this.name = name; this.name = name;
this.instance = null; this.instance = null;
this.enabled = localStorage.getItem("pleroma_mod_" + this.name + "_enabled") !== "false";
} }
[ [
function getClassName () { function getClassName () {
@ -221,32 +140,12 @@ function PleromaMod (name) {
return className; return className;
}, },
function enable () {
this.enabled = true;
this.modLoaded();
if (this.instance.onReady) {
this.instance.onReady();
}
localStorage.setItem("pleroma_mod_" + this.name + "_enabled", this.enabled);
},
function disable () {
this.enabled = false;
if (this.instance.onDestroy) {
this.instance.onDestroy();
}
this.instance = null;
localStorage.setItem("pleroma_mod_" + this.name + "_enabled", this.enabled);
},
function include () { function include () {
console.log("loading " + this.name); console.log("loading " + this.name);
PleromaModLoader.includeScript( PleromaModLoader.includeScript(
PleromaModLoader.getModDir() + "pleroma-mod-" + this.name + "/mod.js" PleromaModLoader.getModDir() + "pleroma-mod-" + this.name + "/mod.js"
).then(() => { ).then(() => {
if (this.enabled) { this.modLoaded();
this.modLoaded();
}
}); });
}, },

Loading…
Cancel
Save