-
Notifications
You must be signed in to change notification settings - Fork 358
/
CustomJs.plugin.js
229 lines (164 loc) · 6.71 KB
/
CustomJs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//META{"name":"CustomJs","website":"https://metalloriff.github.io/toms-discord-stuff/","source":"https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/CustomJs.plugin.js"}*//
class CustomJs {
getName() { return "CustomJs"; }
getDescription() { return "Allows you to specify a custom JavaScript file similar to custom CSS."; }
getVersion() { return "0.0.1"; }
getAuthor() { return "Metalloriff"; }
getChanges() {
return {
};
}
load() {}
start() {
let libLoadedEvent = () => {
try{ this.onLibLoaded(); }
catch(err) { console.error(this.getName(), "fatal error, plugin could not be started!", err); try { this.stop(); } catch(err) { console.error(this.getName() + ".stop()", err); } }
};
let lib = document.getElementById("NeatoBurritoLibrary");
if(!lib) {
lib = document.createElement("script");
lib.id = "NeatoBurritoLibrary";
lib.type = "text/javascript";
lib.src = "https://rawgit.com/Metalloriff/BetterDiscordPlugins/master/Lib/NeatoBurritoLibrary.js";
document.head.appendChild(lib);
}
this.forceLoadTimeout = setTimeout(libLoadedEvent, 30000);
if(typeof window.NeatoLib !== "undefined") libLoadedEvent();
else lib.addEventListener("load", libLoadedEvent);
}
getSettingsPanel() {
const fs = require("fs");
setTimeout(() => {
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createNewTextField("Custom JS file path", this.settings.filepath, e => {
if(e.target.value && !fs.existsSync(e.target.value)) return NeatoLib.showToast("File does not exist", "error");
this.settings.filepath = e.target.value;
this.saveSettings();
}), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createButton("Browse", () => {
NeatoLib.browseForFile(filepath => {
if(!filepath) return NeatoLib.showToast("No file selected", "error");
this.settings.filepath = filepath;
this.saveSettings();
})
}), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createButton("Reload", () => this.reloadJs(), "float:right"), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.DOM.createElement({ innerHTML : `
Example #1
<pre><div style="background:#202225;display:block;padding:10px;margin-top:10px;border-radius:5px">
return new class {
start() {
console.log("start");
document.addEventListener("click", this.clickEvent = e => this.onClick(e));
}
onClick(e) {
console.log("clicked on", e.target);
}
stop() {
document.removeEventListener("click", this.clickEvent);
console.log("stop");
}
onSwitch() {
console.log(NeatoLib.getSelectedTextChannel());
}
}
</div></pre>
`, style : "color:white" }, { type : "p" }), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.DOM.createElement({ innerHTML : `
Example #2
<pre><div style="background:#202225;display:block;padding:10px;margin-top:10px;border-radius:5px">
return {
clickEvent : function(e) {
console.log("clicked on", e.target);
},
start : function() {
console.log("start");
document.addEventListener("click", this.clickEvent);
},
stop : function() {
document.removeEventListener("click", this.clickEvent);
console.log("stop");
},
onSwitch() {
console.log(NeatoLib.getSelectedTextChannel());
}
}
</div></pre>
`, style : "color:white" }, { type : "p" }), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.DOM.createElement({ innerHTML : `
Events and variables
<pre><div style="background:#202225;display:block;padding:10px;margin-top:10px;border-radius:5px">
start() - Called when the script is loaded. Use this for initialization.
stop() - Called before the script is stopped. Use this for de-initialization.
onSwitch() - Called when switching servers and channels.
loader - This plugin object. Example usage: loader.reloadJs()
</div></pre>
`, style : "color:white" }, { type : "p" }), this.getName());
NeatoLib.Settings.pushElement(NeatoLib.Settings.Elements.createHint("Note: My library, 'NeatoLib', is included. You can use this for various things in your script. As of now, there are no docs for it, but you can enter 'NeatoLib' into the console (Ctrl/Cmd + Shift + I) to view all of its functions."), this.getName());
NeatoLib.Settings.pushChangelogElements(this);
}, 0);
return NeatoLib.Settings.Elements.pluginNameLabel(this.getName());
}
saveSettings() {
this.rewatch();
NeatoLib.Settings.save(this);
}
onLibLoaded() {
this.settings = NeatoLib.Settings.load(this, {
displayUpdateNotes : true,
filepath : ""
});
NeatoLib.Updates.check(this);
//if(this.settings.displayUpdateNotes) NeatoLib.Changelog.compareVersions(this.getName(), this.getChanges());
NeatoLib.Events.onPluginLoaded(this);
this.reloadJs();
this.rewatch();
NeatoLib.Events.attach("switch", this.switchEvent = () => {
if(this.script && typeof this.script.onSwitch == "function") {
try { this.script.onSwitch(); }
catch(err) {
console.error(err);
NeatoLib.showToast("[Custom JS]: error in onSwitch(), check the console for more details.");
}
}
});
}
rewatch() {
const fs = require("fs");
if(this.watcher) this.watcher.close();
if(this.settings.filepath) this.watcher = fs.watch(this.settings.filepath, () => {
const current = fs.readFileSync(this.settings.filepath, "utf-8");
if(!current) return;
if(this.last != current) this.reloadJs();
});
}
reloadJs() {
if(this.script && typeof this.script.stop == "function") this.script.stop();
this.script = null;
const fs = require("fs");
if(!this.settings.filepath) return;
else if(!fs.existsSync(this.settings.filepath)) return NeatoLib.showToast("[Custom JS]: file not found", "error");
try {
const js = this.script = eval(`(function(){${this.last = fs.readFileSync(this.settings.filepath, "utf-8")}})();`);
js.loader = this;
try { if(typeof js.start == "function") js.start(); }
catch(err) {
console.error(err);
NeatoLib.showToast("[Custom JS]: error in start(), check the console for more details.");
}
} catch(err) {
console.error(err);
NeatoLib.showToast("[Custom JS]: error evaluating JS, " + err);
}
}
stop() {
if(this.script && typeof this.script.stop == "function") {
try { this.script.stop(); }
catch(err) {
console.error(err);
NeatoLib.showToast("[Custom JS]: error in stop(), check the console for more details.");
}
}
if(this.watcher) this.watcher.close();
NeatoLib.Events.detach("switch", this.switchEvent);
}
}