Files
holesail-browser/extension/dashboard/core/utils.js
T
Raven Scott f1e98a7edd
CI / Build & Test (push) Successful in 2m54s
docs: add CONTRIBUTING.md, CHANGELOG.md, and JSDoc to entire codebase
Add docs/CONTRIBUTING.md covering the build system, dev workflow, all
npm scripts, how to add new native host message types, code style, and
debugging guidance.

Add CHANGELOG.md at the project root documenting all features and fixes
across the 1.0.0 release.

Add JSDoc (@param, @returns) to all previously undocumented exported
functions across 35 JS files:
- native-host/holesail-manager/ (index, virtual-hosts, service-tunnels,
  servers, port-allocator)
- native-host top-level managers (startup, connect-proxy, https-proxy,
  certificate-authority, ssh-manager, rdp-manager)
- extension/background/ (logs, native-messaging, proxy, message-router)
- extension/dashboard/core/ (utils, navigation, init)
- extension/dashboard/ui/ (modal, toast, state-tag)
- extension/dashboard/pages/ (all 10 page files)
- extension/dashboard/refresh.js, events.js
- extension/dashboard/data/hostname-validator.js
- scripts/ (build-host, run-install)
2026-03-01 00:40:53 -05:00

71 lines
2.2 KiB
JavaScript

/**
* Shared utility functions for the Holesail dashboard.
* Provides DOM helpers, logging, time formatting, string utilities, and HTML escaping.
* Loaded first — no dependencies on other dashboard modules.
*/
/**
* Shorthand for `document.getElementById`.
* @param {string} id - Element ID.
* @returns {HTMLElement|null}
*/
function $(id) { return document.getElementById(id); }
/**
* Log a message to the browser console with a `[Holesail-dashboard]` prefix.
* @param {...*} args - Values to log.
*/
function log(...args) { console.log('[Holesail-dashboard]', ...args); }
/**
* Format a Unix timestamp as a human-readable relative time string (e.g. "5m ago").
* @param {number} timestamp - Unix timestamp in milliseconds.
* @returns {string}
*/
function timeAgo(timestamp) {
const seconds = Math.floor((Date.now() - timestamp) / 1000);
if (seconds < 60) return seconds + 's ago';
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return minutes + 'm ago';
const hours = Math.floor(minutes / 60);
if (hours < 24) return hours + 'h ago';
return Math.floor(hours / 24) + 'd ago';
}
/**
* Format a duration in milliseconds as a human-readable uptime string (e.g. "2h 15m").
* @param {number} ms - Duration in milliseconds.
* @returns {string}
*/
function formatUptime(ms) {
const seconds = Math.floor(ms / 1000);
if (seconds < 60) return seconds + 's';
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return minutes + 'm ' + (seconds % 60) + 's';
const hours = Math.floor(minutes / 60);
return hours + 'h ' + (minutes % 60) + 'm';
}
/**
* Truncate a string to `len` characters, appending an ellipsis if truncated.
* @param {string} str
* @param {number} [len=20] - Maximum length before truncation.
* @returns {string}
*/
function truncate(str, len = 20) {
if (!str) return '';
return str.length > len ? str.slice(0, len) + '…' : str;
}
/**
* Escape a string for safe insertion into HTML content.
* Uses a temporary DOM element to leverage the browser's own escaping.
* @param {string} str - Raw string that may contain HTML special characters.
* @returns {string} HTML-escaped string.
*/
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}