1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2024-12-22 23:42:50 +01:00
Discord-History-Tracker/app/Resources/Viewer/scripts/dom.js

55 lines
1.4 KiB
JavaScript

const HTML_ENTITY_MAP = {
"&": "&",
"<": "&lt;",
">": "&gt;",
"\"": "&quot;",
"'": "&#39;"
};
const HTML_ENTITY_REGEX = /[&<>"']/g;
class DOM {
/**
* Returns a child element by its ID. Parent defaults to the entire document.
*/
static id(id, parent) {
return (parent || document).getElementById(id);
}
/**
* Returns an array of all child elements containing the specified class. Parent defaults to the entire document.
*/
static cls(cls, parent) {
return Array.prototype.slice.call((parent || document).getElementsByClassName(cls));
}
/**
* Returns an array of all child elements that have the specified tag. Parent defaults to the entire document.
*/
static tag(tag, parent) {
return Array.prototype.slice.call((parent || document).getElementsByTagName(tag));
}
/**
* Returns the first child element containing the specified class. Parent defaults to the entire document.
*/
static fcls(cls, parent) {
return (parent || document).getElementsByClassName(cls)[0];
}
/**
* Converts characters to their HTML entity form.
*/
static escapeHTML(html) {
return String(html).replace(HTML_ENTITY_REGEX, s => HTML_ENTITY_MAP[s]);
}
/**
* Converts a timestamp into a human readable time, using the browser locale.
*/
static getHumanReadableTime(timestamp) {
const date = new Date(timestamp);
return date.toLocaleDateString() + ", " + date.toLocaleTimeString();
};
}