-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoreapi.js
429 lines (361 loc) · 9.5 KB
/
coreapi.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Global variables
let alerts = []
let intervals = []
// Chain api
class Chain {
constructor() {
this.funcs = []
}
async call() {
for(let i = 0; i < this.funcs.length; i++) {
let res = await this.funcs[i]();
if(!res) {
return false
}
}
return true
}
add(f) {
this.funcs.push(f)
}
}
// Main rump class (session)
class Session {
constructor() {
// Core variables
this.title = null
this.middleware = new Chain()
this.service = null
this.firstload = null
this.path = null
if(localStorage.session) {
try {
this._state = JSON.parse(localStorage.session)
} catch {
this._state = {}
}
} else {
this._state = {}
}
this._h = {
get: (obj, i) => {
return this._state[i] || undefined
},
set: (obj, i, v) => {
this._state[i] = v
localStorage.session = JSON.stringify(this._state)
}
}
this.state = new Proxy({}, this._h);
}
_clearService() {
this.title = null
this.service = null
this.path = null
}
getState() {
return this._state
}
clearState() {
localStorage.session = "{}"
this._state = "{}"
}
}
var $rump = new Session()
// APIs
const log = (...args) => {
console[args[0]](`%c[MAIN ${Date.now()}]%c[${args[1]}]%c`, "color:red;font-weight:bold;", "color:purple;font-weight:bold;", "", ...args.slice(2))
}
// Custom logger
["log", "debug", "info", "warn", "error"].forEach((method) => {
self[method] = function(...args) {
args.unshift(method)
log(...args)
}
})
// Simple polyfill for promisify and sleep
const promisify = f => (...args) => new Promise((a,b)=>f(...args, (err, res) => err ? b(err) : a(res)));
const sleep = ms => new Promise(r => setTimeout(r, ms));
// Public API to try returning data
function tryReturn(f, ...args) {
try {
return f(...args)
} catch (err) {
console.error(err)
console.error("coreapi.js failed with a critical error")
}
}
// Public API to replace current url without reload, pass empty string to use path.url
function setAddressBar(url = "") {
window.history.replaceState({"prev": window.location.href}, "Panel", url || $rump.path.url);
}
// Public API to set status
function setStatus(text) {
document.querySelector("#status").innerHTML = marked.marked(text)
}
// Public API to set main body, special variables are unwrapped as well
// $n => unwraps to a nonce
// $data => unwraps to ``path.data``
function setBody(text) {
let n = Math.floor(Math.random() * 100000);
document.querySelector("#body").innerHTML = marked.marked(text.replaceAll("$data", $rump.path.data).replaceAll("$n", n))
setStatus("")
}
// Public API to run code on ready
function ready(callback){
// in case the document is already rendered
if (document.readyState!='loading') callback();
// modern browsers
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
// IE <= 8
else document.attachEvent('onreadystatechange', function(){
if (document.readyState=='complete') callback();
});
}
// Global eval API
function globalEval(text) {
(1, eval)(text);
};
// Get path info API
function getPathInfo(path) {
console.log(`getPathInfo: ${path}`)
if(path.startsWith("https://")) {
path = path.replace(window.location.origin, "")
}
if(path == "/" || !path) {
path = ""
} else {
if(!path.startsWith("/")) {
path = `/${path}`
}
if(path.endsWith("/")) {
path = path.slice(0, -1);
}
}
return {
service: `${path}/+route/service.min.js`,
aux: `${path}/+route/aux.min.js`, // Auxillary data
data: `${path}/+data`,
url: path
}
}
// _loadFetch API tries fetching a path
async function _loadFetch(path, critical) {
// Avoid caching
let n = Math.floor(Math.random() * 10000);
let jsServiceResp = await tryReturn(fetch, `${path}?n=${n}`)
if(!jsServiceResp || !jsServiceResp.ok) {
if(critical) {
alert(`loadService error: could not load ${path}`)
return
}
return;
}
return await jsServiceResp.text();
}
// Internal API to modify links
function _linkMod() {
links = document.querySelectorAll("a")
links.forEach(link => {
console.debug("[Sparkpelt]", link, link.href, link.hasPatched, link.hasPatched == undefined)
if(link.href.startsWith(`${window.location.origin}/`) && link.hasPatched == undefined) {
if(link.href == `${window.location.origin}/#`) {
return // Don't patch # elements
}
if(link.href.includes("+data")) {
return // Don't patch internal '+data' routes
}
if(link.href == window.location.href || link.href.endsWith("#") || link.pathname == window.location.pathname) {
return // Don't patch if same url
}
link.hasPatched = true
console.debug("[Sparkpelt] Add patch")
link.addEventListener('click', event => {
if ( event.preventDefault ) event.preventDefault();
// Now add some special code
window.history.pushState(window.location.pathname, 'Loading...', link.href);
handler = async () => {
let activeEls = document.querySelectorAll(".active")
for(let i = 0; i < activeEls.length; i++) {
activeEls.classList.toggle("active")
}
await loadService(link.href)
}
handler()
})
}
})
}
// loadService API
async function loadService(path) {
let pathInfo = getPathInfo(path)
$rump._clearService()
$rump.path = pathInfo
setStatus("Loading service [aux.js]")
// Load aux.js
let jsAux = await _loadFetch(pathInfo.aux, false)
if(jsAux) {
globalEval(jsAux)
}
setStatus("Loading service [service.js]")
// Load service.js
let jsService = await _loadFetch(pathInfo.service, true)
if(!jsService) {
return -1;
}
globalEval(jsService);
if(!$rump.service) {
alert(`loadService error: ${pathInfo.service} does not expose a service function`)
return -2
}
if(!$rump.title) {
error(`loadService error: ${pathInfo.service} does not expose a title`)
return -2
}
document.title = $rump.title
setStatus("")
// Call the middleware+service
let res = await $rump.middleware.call()
if(!res) {
return
}
await $rump.service()
setTimeout(_linkMod, 100)
}
// Common service that many services can use optionally
async function commonService(pathOpt) {
let path = pathOpt || "content.md"
// Avoid caching
let n = Math.floor(Math.random() * 10000);
let content = await fetch(`${$rump.path.data}/${path}?n=${n}`)
let text = await content.text()
setBody(text.replaceAll("$data", $rump.path.data))
}
// Public API to parse text substituing our defined variables
function parseText(text) {
return text.replaceAll("$data", $rump.path.data)
}
// Load function
async function load() {
setStatus("Loading core data")
if($rump.firstload) {
setTimeout($rump.firstload, 300)
}
loadService(window.location.pathname)
}
// Alert API
function alert(id, title, content) {
if(!title && !content) {
title = "Alert, Agent P!"
content = id
id = "maint-" + Math.floor(Math.random() * 10000)
}
if(id === "fatal-error") {
intervals.forEach(interval => {
clearInterval(interval)
intervals.splice(intervals.indexOf(interval), 1)
})
alerts = [id];
} else {
if(alerts.length > 0) {
intervals.push(setTimeout(() => {
alert(id, title, content)
}, 300))
return
}
alerts.push(id)
intervals.forEach(interval => {
clearInterval(interval)
intervals.splice(intervals.indexOf(interval), 1)
})
}
document.querySelector("#alert-placer").innerHTML = `
<dialog
id="${id}"
open
role='dialog'
aria-labelledby="${id}-title"
aria-describedby="${id}-content"
>
<section>
<header id="${id}-title">
<strong>
<h2 class="alert-title">${title}</h2>
</strong>
</header>
<div id="${id}-content">
${content}
</div>
<button onclick="closeAlert()" id="alert-close" class="block mx-auto">
Close
</button>
</section>
</dialog>
<style>
dialog {
position: fixed;
top: 0;
right: 0;
left: 0;
bottom: 0;
z-index: 9999;
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background: transparent;
color: black !important;
}
dialog::after {
content: '';
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: black;
opacity: .5;
z-index: -1;
pointer-events: none;
}
section {
width: 500px;
min-height: 200px;
max-height: 500px;
padding: 10px;
border-radius: 4px 4px 4px 4px;
background: white;
}
#alert-close {
position: relative;
text-align: center !important;
top: 0 !important;
bottom: 0 !important;
}
.alert-title {
color: black !important;
}
#alert-close {
background-color: white !important;
color: black !important;
font-weight: bold !important;
border: black solid 1px !important;
margin-top: 3px;
padding: 10px;
}
block {
display: block;
}
</style>
`
}
function closeAlert() {
alerts.forEach(id => {
document.querySelector(`#${id}`).remove()
alerts.splice(alerts.indexOf(id), 1)
})
}
window.alert = alert
ready(load)