[GM > Plugin] Add addCSS(css), source tweaks

This commit is contained in:
Ducko 2021-06-16 19:49:45 +01:00 committed by Alyxia Sother
parent 8b347972ac
commit 42e2c02719
No known key found for this signature in database
GPG Key ID: 355968D14144B739
1 changed files with 15 additions and 5 deletions

View File

@ -3,12 +3,23 @@ import showToast from '@goosemod/toast';
export default class Plugin { export default class Plugin {
constructor() { constructor() {
this.patches = []; this.patches = [];
this.stylesheets = [];
} }
enqueueUnpatch(unpatch) { enqueueUnpatch(unpatch) {
this.patches.push(unpatch); this.patches.push(unpatch);
} }
addCss(css) {
const el = document.createElement('style');
el.appendChild(document.createTextNode(css)); // Load the stylesheet via style element w/ CSS text
document.head.appendChild(el);
this.stylesheets.push(el); // Push to internal array so we can remove the elements on unload
}
toast(content, options) { toast(content, options) {
showToast(content, { showToast(content, {
subtext: this.name, subtext: this.name,
@ -17,14 +28,13 @@ export default class Plugin {
} }
goosemodHandlers = { goosemodHandlers = {
onImport() { onImport: () => {
this.onImport(); this.onImport();
}, },
onRemove() { onRemove: () => {
for (const unpatch of this.patches) { this.patches.forEach((x) => x());
unpatch(); this.stylesheets.forEach((x) => x.remove());
}
this.onRemove(); this.onRemove();
} }