-
Notifications
You must be signed in to change notification settings - Fork 3
/
yokto.js
47 lines (47 loc) · 1.3 KB
/
yokto.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
const $ = (query, return_list) => {
const elems = document.querySelectorAll(query);
if (elems.length === 1 || !return_list) {
return elems[0];
};
return Array.prototype.slice.call(elems);
};
const __ = (obj) => {
if (typeof(obj) === "object" && obj.length) { // WARN: Will also return undefined when the Array is empty.
return true;
};
};
const _ = (parentSelector, tag, attrs, innerText) => {
var parentElem = $(parentSelector);
let elem = document.createElement(tag);
if ( !(__(typeof(attrs))) ) {
for (key in attrs) {
elem.setAttribute(key, attrs[key]);
};
};
if (innerText) {
elem.innerText = innerText.toString();
};
parentElem.appendChild(elem);
};
const $$ = (callback) => {
window.addEventListener("DOMContentLoaded", () => {
callback();
});
};
const $_ = async (method, url, data) => {
var headers = {};
if (data) {
headers['Content-Type'] = 'application/json';
};
const resp = await fetch(url, {
method: method,
mode: 'same-origin',
cache: 'no-cache',
credentials: 'same-origin',
headers: headers,
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data)
});
return await resp.json();
};