Newton_Editor/user_config/usr/share/newton/context_path/resources/js/newton/utils.js

111 lines
3.4 KiB
JavaScript

// Message handler
const displayMessage = (message, type, timeout, msgWindow = "page-alert-zone") => {
let alertField = document.getElementById(msgWindow);
let divElm = document.createElement("DIV");
let btnElm = document.createElement("BUTTON");
let spnElm = document.createElement("SPAN");
let textElm = document.createTextNode(message);
divElm.setAttribute("class", "alert alert-dismissible fade show alert-" + type);
divElm.setAttribute("role", "alert");
divElm.appendChild(textElm);
btnElm.type = "button";
textElm = document.createTextNode("X");
btnElm.setAttribute("class", "btn-dark btn-close");
btnElm.setAttribute("data-bs-dismiss", "alert");
btnElm.setAttribute("aria-label", "close");
spnElm.setAttribute("aria-hidden", "true");
spnElm.appendChild(textElm);
btnElm.appendChild(spnElm);
divElm.appendChild(btnElm);
alertField.appendChild(divElm);
if (timeout > 0) {
setTimeout(function () {
clearChildNodes(alertField);
}, timeout * 1000);
}
}
const sendMessage = (topic = null, ftype = "", fhash = "", fpath = "", content = "") => {
if ( !isNotNullOrUndefined(topic) ) {
const msg = "No 'topic' passed in 'sendMessage' method...";
displayMessage(msg, "danger");
return;
}
const messageBody = {
"topic": topic,
"ftype": ftype,
"fhash": fhash,
"fpath": fpath,
"content": btoa(content)
};
messenger.backend.postMessage( JSON.stringify(messageBody) );
}
const sleep = ms => new Promise(r => setTimeout(r, ms));
const isDict = (dict) => { return typeof dict === "object" && !Array.isArray(dict); };
const getSHA256Hash = async (input) => {
let textAsBuffer = new TextEncoder().encode(input);
let hashBuffer = await window.crypto.subtle.digest("SHA-256", textAsBuffer);
let hashArray = Array.from( new Uint8Array(hashBuffer) );
let fhash = hashArray.map(
(item) => item.toString(16).padStart(2, "0")
).join("");
return fhash;
};
const clearChildNodes = (parent) => {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
const getSiblingElm = (elm) => {
return ( isNotNullOrUndefined(elm.nextElementSibling) )
?
elm.nextElementSibling
:
elm.previousElementSibling;
}
const isNotNullOrUndefined = (val) => {
return (val !== undefined && val !== null)
}
// Cache Buster
const clearCache = () => {
const rep = /.*\?.*/;
let links = document.getElementsByTagName('link'),
scripts = document.getElementsByTagName('script'),
video = document.getElementsByTagName('video'),
process_scripts = false;
for (let i = 0; i < links.length; i++) {
let link = links[i],
href = link.href;
if(rep.test(href)) {
link.href = href + '&' + Date.now();
} else {
link.href = href + '?' + Date.now();
}
}
if(process_scripts) {
for (let i = 0; i < scripts.length; i++) {
let script = scripts[i],
src = script.src;
if(rep.test(src)) {
script.src = src+'&'+Date.now();
} else {
script.src = src+'?'+Date.now();
}
}
}
}