forked from p0rtL6/BetterDiscord-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsSwap.plugin.js
80 lines (71 loc) · 2.12 KB
/
CsSwap.plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @name CsSwap
* @author p0rtL
* @description Allows for the usage of multiple custom css files
* @version 1.0
*/
const path = require("path");
const fs = require("fs");
var watcher
const createUpdateWrapper = (Component, valueProp = "value", changeProp = "onChange") => props => {
const [value, setValue] = BdApi.React.useState(props[valueProp]);
return BdApi.React.createElement(Component, {
...props,
[valueProp]: value,
[changeProp]: value => {
if (typeof props[changeProp] === "function") props[changeProp](value);
setValue(value);
}
});
}
const SwitchItem = createUpdateWrapper(BdApi.findModuleByDisplayName("SwitchItem"));
module.exports = class CsSwap {
load() { }
start() {
var toggled = patch()
watchMutate(toggled)
}
stop() {
BdApi.clearCSS("CsSwap");
watcher.close()
}
getSettingsPanel() {
return settingsPanel()
}
}
function settingsPanel() {
return getFiles().map(i => BdApi.React.createElement(SwitchItem, {
value: BdApi.getData("CsSwap", i),
onChange: (value) => {
BdApi.saveData("CsSwap", i, value);
watcher.close()
toggled = patch()
watchMutate(toggled)
}
}, i));
}
function patch() {
let toggled = getFiles().filter(e => BdApi.getData("CsSwap", e));
let contents = toggled.map(e => fs.readFileSync(path.resolve(__dirname, `./css/${e}`)))
injectCSS(contents)
return toggled
}
function getFiles() {
return fs.readdirSync(path.resolve(__dirname, `./css/`))
}
function injectCSS(contents) {
const fullCss = contents.join("\n");
if (document.querySelector("bd-styles #CsSwap")) {
BdApi.clearCSS("CsSwap");
BdApi.injectCSS("CsSwap", `${fullCss}`);
} else {
BdApi.injectCSS("CsSwap", `${fullCss}`);
}
}
function watchMutate(toggled) {
watcher = fs.watch(path.resolve(__dirname, './css'), (eventType, filename) => {
if (toggled.includes(filename)) {
patch()
}
})
}