-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkai.js
36 lines (35 loc) · 8.46 KB
/
kai.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
var currentZoom = 1;
function gup(name, loc) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(loc); if (results == null) return ""; else return results[1]; }
function getcookie(cname = "") { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(";"); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == " ") { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return "null"; }
function nl2br(text = "") { return text.replace(/\n/gi, "<br>"); }
function fileExtention(filename = "") { var t = filename.split("."); return t[t.length - 1] }
function baseFileName(filepath = "") { var n = filepath.split("/"); return n[n.length - 1] }
function binaryToString(binarydata = "") { return binarydata.split(" ").map(letter => String.fromCharCode(parseInt(letter, 2))).join(""); }
function uint8arraytostring(unit8data) { return String.fromCharCode.apply(null, unit8data); }
function createTextFile(filename = "", filecontent = "") { var a = document.createElement("a"); a.href = "data:text/plain;charset=UTF-8," + encodeURIComponent(filecontent); a.download = filename; a.style.display = "none"; document.body.appendChild(a); a.click(); document.body.removeChild(a); }
function str_replace(find = "", replace = "", key = "") { return key.replace(find, replace) }
function maxChar(paragraph = "", number = "") { var rep = paragraph.replace(/<br>/gi, " "); var s = rep.split(""); var r = ""; for (var i = 0; i < number; i++) { if (s[i] === undefined) { s[i] = ""; } r += s[i]; } if (s.length > number) { return r + "..."; } else { return r; } }
function getElementsByText(str = "", tag = "a") { return Array.prototype.slice.call(document.getElementsByTagName(tag)).filter(el => el.textContent.trim() === str.trim()); }
function enumtobnum(e) { return new String(e).replace(/0/gi, "০").replace(/1/gi, "১").replace(/2/gi, "২").replace(/3/gi, "৩").replace(/4/gi, "৪").replace(/5/gi, "৫").replace(/6/gi, "৬").replace(/7/gi, "৭").replace(/8/gi, "৮").replace(/9/gi, "৯") }
function openFullScreen(e) { e.requestFullscreen ? e.requestFullscreen() : e.webkitRequestFullscreen ? e.webkitRequestFullscreen() : e.msRequestFullscreen && e.msRequestFullscreen() }
function paginate(array, number_of_items = 20, page_number = 1) { return array.slice((page_number - 1) * number_of_items, page_number * number_of_items); }
function base64FileSize(base64data = "") { return Math.ceil(base64data.replace(/=/gi, "").length * (3 / 4)) - 1 }
function parseStr(r = "") { return r.replace(parseInt(r), "") }
function validLink(n = new URL()) { try { return u = new URL(n), !0 } catch (n) { return !1 } }
function kaiStorage() { try { navigator.getDeviceStorages("sdcard")[0] } catch (r) { return !1 } return !0 }
function scanDir(extentions = ["txt"], outputelement = document.body, nofileelement = document.getElementById("nofilenotice")) { var sdcard = navigator.getDeviceStorages("sdcard"); var ti = 0; for (var i = 0; i < sdcard.length; i++) { var sd = sdcard[i]; if (extentions == "*") { var cursor = sd.enumerate(); cursor.onsuccess = function () { var file = this.result; var filename = file.name; var filepath = filename.replace("/sdcard/", ""); var item = document.createElement("div"); item.className = "listview file focusable"; item.tabIndex = ti; ti++; item.innerHTML = "<img src = \"ext.png\" style=\"width: 20px;vertical-align: middle;margin-right: 5px\">" + baseFileName(file.name.substring(file.name.lastIndexOf("/") + 1)); item.addEventListener("click", function () { machine(file); }); if (outputelement.appendChild(item)) { outputelement.style.display = "block"; nofileelement.style.display = "none"; outputelement.appendChild(item); outputelement.style = "z-index: 1"; } else { outputelement.style.display = "none"; nofileelement.style.display = "block"; outputelement.style = "z-index: 0"; } if (!this.done) { this.continue(); } } } else { extentions.forEach((e) => { var cursor = sd.enumerate(); cursor.onsuccess = function () { if (this.result.name.lastIndexOf(e) === this.result.name.length - e.length) { var file = this.result; var filename = file.name; var filepath = filename.replace("/sdcard/", ""); var item = document.createElement("div"); item.className = "listview file focusable"; item.tabIndex = ti; ti++; item.innerHTML = "<img src = \"ext.png\" style=\"width: 20px;vertical-align: middle;margin-right: 5px\">" + baseFileName(file.name.substring(file.name.lastIndexOf("/") + 1)); item.addEventListener("click", function () { machine(file); }); if (outputelement.appendChild(item)) { outputelement.style.display = "block"; nofileelement.style.display = "none"; outputelement.appendChild(item); outputelement.style = "z-index: 1"; } else { outputelement.style.display = "none"; nofileelement.style.display = "block"; outputelement.style = "z-index: 0"; } } if (!this.done) { this.continue(); } } }); } cursor.onerror = function () { console.warn("No file found: " + this.error); outputelement.innerHTML = "No file found: " + this.error; }; } }
function customRadio(focus_color = "#ddd", focus_text = "#666", body_background = "#efefef", extra_style = "") { if (focus_color == "") { focus_color = "#000"; } if (focus_text == "") { focus_text = "#fff"; } if (body_background == "") { body_background = "#efefef"; } document.body.style = "background: " + body_background; if (extra_style != "") { document.head.innerHTML += "<style>" + extra_style + "</style>"; } var re = document.querySelectorAll(".custom-radio > input[type=radio]"); for (var i = re.length - 1; i >= 0; i--) { var e = document.createElement("div"); e.classList = "option focusable"; e.style = "outline: 0px; border-color: transparent"; e.innerHTML = re[i].value; e.setAttribute("data-name", re[i].name); e.setAttribute("data-num", i); e.addEventListener("click", function () { re[this.getAttribute("data-num")].click(); for (var j = 0; j < document.querySelectorAll("div[data-name=" + this.getAttribute("data-name") + "]").length; j++) { document.querySelectorAll("div[data-name=" + this.getAttribute("data-name") + "]")[j].style = "background: transpharent"; } this.style = "background: " + focus_color + ";color: " + focus_text; }); var parent = re[i].parentNode; parent.insertBefore(e, parent.childNodes[0]); parent.insertBefore(document.querySelectorAll(".custom-radio > question")[0], parent.childNodes[0]); } for (var k = 0; k < document.querySelectorAll(".focusable").length; k++) { document.querySelectorAll(".focusable")[k].tabIndex = k; } }
function scanFiles(extensions = ["txt"]) { return new Promise((resolve, reject) => { var files = []; var sdcard = navigator.getDeviceStorages("sdcard"); if (extensions === "*") { var pendingEnumerations = sdcard.length; sdcard.forEach(sd => { var cursor = sd.enumerate(); cursor.onsuccess = function () { files.push(cursor.result); if (!cursor.done) { cursor.continue(); } else { pendingEnumerations--; if (pendingEnumerations === 0) { resolve(files); } } }; cursor.onerror = function (error) { reject(error); }; }); } else { var pendingEnumerations = sdcard.length * extensions.length; sdcard.forEach(sd => { extensions.forEach(ext => { var cursor = sd.enumerate(); cursor.onsuccess = function () { if (cursor.result && cursor.result.name.toLowerCase().endsWith(ext.toLowerCase())) { files.push(cursor.result); } if (!cursor.done) { cursor.continue(); } else { pendingEnumerations--; if (pendingEnumerations === 0) { resolve(files); } } }; cursor.onerror = function (error) { reject(error); }; }); }); } }); }
/* Example usage:
scanFiles([".bin",".txt"])
.then(files => {
console.log(files.length); // Output the number of files found
console.log(files); // Output the list of files
})
.catch(error => {
console.error("Error scanning files:", error);
});
*/
function newTab(URL = window.location.href, name = "newTab", width = 240, height = 320) { window.open(URL, name, "width=" + width + ",height=" + height); }
function showToast(e){var t=document.createElement("div");t.classList="toasttext",t.innerHTML=e,document.body.appendChild(t),setTimeout((function(){document.body.removeChild(t)}),5e3)}
function showSize(bytes) { if(bytes >= 1024 && bytes <= 1048575) { return (bytes/1024).toFixed(2)+' kb'; } if(bytes >= 1048576 && bytes <= 10473741823) { return (bytes/1048576).toFixed(2)+' mb'; } if(bytes >= 1073741824) { return (bytes/1073741824).toFixed(2)+' gb'; } else { return bytes+' bytes'; } }