forked from twilight-sparkle-irl/EndPwnPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginsrepo.js
317 lines (275 loc) · 12.4 KB
/
pluginsrepo.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Plugins Repo Plugin for EndPwn
Made by Cynthia (Cynosphere)
*/
var baseurl = "https://raw.githubusercontent.com/Cynosphere/EndPwnPlugins/master/plugins/";
var internal = {
print: function (str) {
console.log(`%c[PluginsRepo]%c ` + str, 'font-weight:bold;color:#c8f', '');
}
}
exports = {
manifest: {
author: "Cynosphere",
name: "Plugins Repo",
description: "Download plugins.",
_version: 4
},
getInfo: async function(plugin){
var exports = {};
evaluate(await (await fetch(baseurl+plugin+'?_=' + Date.now())).text(), exports);
window._prPluginStore[plugin.replace(".js","")] = exports;
return {id:plugin.replace(".js",""),data:exports.manifest};
},
updateCheck:async function(){
internal.print("Checking for updates...");
var pr = {};
evaluate(await (await fetch(baseurl.replace("plugins/","pluginsrepo.js")+'?_=' + Date.now())).text(), pr);
if(pr && pr.manifest && pr.manifest._version && pr.manifest._version > this.manifest._version){
internal.print("Update avaliable");
$settingsapi.fancyDialog({
header: 'Plugins Repo: Update',
confirmText: 'Yes', cancelText: 'No',
onConfirm: async () => {
fs.writeFileSync($api.data + '/plugins/pluginsrepo.js', await (await fetch(baseurl.replace("plugins/","pluginsrepo.js")+'?_=' + Date.now())).text());
electron.getCurrentWindow().loadURL(window.location.href);
}
},'Plugins Repo has an update, would you like to restart for it to be installed?');
}else{
internal.print("No update avaliable.");
internal.print("Checking plugins for updates...");
internal.print("Fetching plugins...");
window._prTimeout = Date.now()+120;
let plugins = await (await fetch('https://raw.githubusercontent.com/Cynosphere/EndPwnPlugins/master/plugins.txt?_=' + Date.now())).text();
plugins=plugins.split("\n");
var updated = [];
var toUpdate = {};
for(x in plugins){
var d = await this.getInfo(plugins[x]);
var id = d.id;
var data = d.data;
var info = {};
info.name = data.name ? data.name : (id ? id : "<unnamed plugin???>");
info.description = data.description ? data.description : "Manifest is missing for this plugin.";
info.author = data.author ? data.author : "Unknown";
info.loadAfter = data.loadAfter ? data.loadAfter : [];
info.priority = data.priority ? data.priority : 0;
info.replacements = data.replacements ? data.replacements : [];
if(fs.existsSync(`${$api.data}/plugins/${id}.js`)){
var p = fs.readFileSync(`${$api.data}/plugins/${id}.js`).toString().replace(/\r\n/g,"\n").trim();
var np = await (await fetch(baseurl+id+'.js?_=' + Date.now())).text();
np=np.toString().replace(/\r\n/g,"\n").trim();
if(p !== np){
internal.print(`Update found for ${info.name} (${id}).`);
toUpdate[id] = np;
updated.push(`${info.name} (${id})`);
}
}else{
internal.print(`Skipping ${info.name} (${id}), not installed.`);
}
}
}
if(updated.length > 0){
$settingsapi.fancyDialog({
header: 'Plugins Repo: Updated Plugins',
confirmText: 'Yes',
cancelText: 'No',
onConfirm: async () => {
Object.keys(toUpdate).forEach(x=>{
fs.writeFileSync(`${$api.data}/plugins/${x}.js`,toUpdate[x]);
internal.print(`Wrote plugin: ${x}`);
});
internal.print(`Reloading!`);
electron.getCurrentWindow().loadURL(window.location.href);
}
},`The following plugins need to update: ${updated.join(", ")}. Would you like to restart for these plugins to be updated?`);
}
},
init:async function(){
if(!window.$settingsapi){
internal.print("Asking to install Settings API");
$api.ui.showDialog({
title: 'Plugins Repo: Settings API Installed',
body: 'Settings API was not found, would you like to restart for it to be installed?',
confirmText: 'Yes', cancelText: 'No',
onConfirm: async () => {
fs.writeFileSync($api.data + '/plugins/settingsapi.js', await (await fetch(baseurl+'settingsapi.js?_=' + Date.now())).text());
electron.getCurrentWindow().loadURL(window.location.href);
}
});
return;
}
let em = $settingsapi.elements;
let int = em.internal;
window._prPluginStore = {}
window._prTimeout = 0;
var setupInfo = function(id,data){
var info = {};
info.name = data.name ? data.name : (id ? id : "<unnamed plugin???>");
info.description = data.description ? data.description : "Manifest is missing for this plugin.";
info.author = data.author ? data.author : "Unknown";
info.loadAfter = data.loadAfter ? data.loadAfter : [];
info.priority = data.priority ? data.priority : 0;
info.replacements = data.replacements ? data.replacements : [];
var cont = em.createVerticalPanel();
var head = em.createHorizontalPanel()
.appendTo(cont);
var title = createElement("h3")
.withClass(
int.headers.h3,
int.headers.title,
int.headers.size16,
int.headers.height20,
int.headers.defaultColor
)
.withText(info.name)
.appendTo(head);
em.createH5('By '+info.author)
.appendTo(title);
em.createButton("Download")
.modify(x=>{
if(fs.existsSync(`${$api.data}/plugins/${id}.js`)){
x.disabled = true;
x.className = `${x.className} ${int.buttons.disabled}`;
}else{
x.onclick = async _ => {
internal.print("Downloading Plugin: "+id);
fs.writeFileSync(`${$api.data}/plugins/${id}.js`, await (await fetch(baseurl+id+'.js?_=' + Date.now())).text());
x.disabled = true;
x.className = `${x.className} ${int.buttons.disabled}`;
if(window._prDialogSeen) return;
$api.ui.showDialog({
title: 'New Plugin Installed',
body: 'Would you like to refresh for this plugin to take effect?\nIf you want to get more plugins, click no.',
minorText: 'This message will only show once per session.',
confirmText: 'Yes', cancelText: 'No',
onConfirm: () => {
electron.getCurrentWindow().loadURL(window.location.href);
}
});
window._prDialogSeen = true;
}
}
})
.appendTo(head);
createElement("div")
.withClass(
int.misc3.description,
int.misc3.formText,
int.misc3.modeDefault
)
.withText(info.description)
.appendTo(cont);
var body = em.createHorizontalPanel()
.appendTo(cont);
createElement("div")
.withClass(
int.misc3.description,
int.misc3.formText,
int.misc3.modeDefault
)
.withText(`Replacements: ${info.replacements.length}`)
.appendTo(body);
/*createElement("div")
.withClass(
int.misc3.description,
int.misc3.formText,
int.misc3.modeDefault
)
.modify(x=>x.style.marginLeft = "8px")
.withText(`Priority: ${info.priority}`)
.appendTo(body);
createElement("div")
.withClass(
int.misc3.description,
int.misc3.formText,
int.misc3.modeDefault
)
.modify(x=>x.style.marginLeft = "8px")
.withText(info.loadAfter.length > 0 ? `Depends on: ${info.loadAfter.join(", ")}` : "No dependencies")
.appendTo(body);*/
createElement("div")
.withClass(em.internal.dividers.divider)
.appendTo(cont);
return cont;
}
var settingsPage = async function(pnl){
em.createH2("Plugins Repo")
.appendTo(pnl);
let loading = createElement("div")
.withClass(
int.misc3.description,
int.misc3.formText,
int.misc3.modeDefault
)
.withText(`Loading, please wait...`)
.appendTo(pnl);
var btns = em.createHorizontalPanel()
.appendTo(pnl);
em.createButton("Reload list")
.modify(async x=>{
x.className = `${x.className} ${int.buttons.colorGreen}`;
x.onclick=async ()=>{
if(Date.now() < window._prTimeout){
em.createH5(`Ratelimited, try again in ${Math.floor(window._prTimeout-Date.now())}s.`)
.modify(y=>setTimeout(_=>{y.parentNode.removeChild(y)},2000))
.appendTo(btns);
return;
}
list.purge();
window._prPluginStore = {};
window._prTimeout = Date.now()+120;
internal.print("Fetching plugins...");
let plugins = await (await fetch('https://raw.githubusercontent.com/Cynosphere/EndPwnPlugins/master/plugins.txt?_=' + Date.now())).text();
plugins=plugins.split("\n");
for(x in plugins){
var info = await exports.getInfo(plugins[x]);
setupInfo(info.id,info.data)
.appendTo(list);
}
}
})
.appendTo(btns);
em.createButton("Check for updates")
.modify(async x=>{
x.className = `${x.className} ${int.buttons.colorYellow}`;
x.onclick=async ()=>{
if(Date.now() < window._prTimeout){
em.createH5(`Ratelimited, try again in ${Math.floor(window._prTimeout-Date.now())}s.`)
.modify(y=>setTimeout(_=>{y.parentNode.removeChild(y)},2000))
.appendTo(btns);
return;
}
exports.updateCheck();
}
})
.appendTo(btns);
var list = em.createVerticalPanel()
.appendTo(pnl);
if(Object.keys(_prPluginStore).length <= 0){
internal.print("Fetching plugins...");
window._prTimeout = Date.now()+120;
let plugins = await (await fetch('https://raw.githubusercontent.com/Cynosphere/EndPwnPlugins/master/plugins.txt?_=' + Date.now())).text();
plugins=plugins.split("\n");
loading.modify(x=>x.parentNode.removeChild(x));
for(x in plugins){
var info = await this.getInfo(plugins[x]);
setupInfo(info.id,info.data)
.appendTo(list);
}
}else{
loading.modify(x=>x.parentNode.removeChild(x));
Object.keys(_prPluginStore).forEach(x=>{
info = {id:x,data:_prPluginStore[x].manifest}
setupInfo(info.id,info.data)
.appendTo(list);
});
}
}
$settingsapi.addSection("PluginsRepo","Plugins Repo",null,settingsPage);
},
start:function(){
this.updateCheck();
setTimeout(this.init,200);
}
}