flash.moe/assets/makai.js/utility.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-10-13 20:33:17 +00:00
const $i = document.getElementById.bind(document);
const $c = document.getElementsByClassName.bind(document);
const $q = document.querySelector.bind(document);
const $qa = document.querySelectorAll.bind(document);
const $r = function(element) {
if(element && element.parentNode)
element.parentNode.removeChild(element);
};
const $ri = function(name) {
$r($i(name));
};
const $ib = function(ref, elem) {
ref.parentNode.insertBefore(elem, ref);
};
const $rc = function(element) {
while(element.lastChild)
element.removeChild(element.lastChild);
};
const $rp = function(target, replace) {
$ib(target, replace);
$r(target);
};
2023-10-13 20:33:17 +00:00
const $ar = function(array, index) {
array.splice(index, 1);
};
const $ari = function(array, item) {
let index;
while(array.length > 0 && (index = array.indexOf(item)) >= 0)
$ar(array, index);
};
const $arf = function(array, predicate) {
let index;
while(array.length > 0 && (index = array.findIndex(predicate)) >= 0)
$ar(array, index);
};
const $as = function(array) {
if(array.length < 2)
return;
for(let i = array.length - 1; i > 0; --i) {
let j = Math.floor(Math.random() * (i + 1)),
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
};
const $csrfp = (function() {
const elem = $q('meta[name="csrfp-token"]');
return {
get: () => elem?.content ?? '',
set: token => {
if(elem != null)
elem.content = token?.toString() ?? '';
},
};
})();