-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared-utils.js
More file actions
36 lines (33 loc) · 1.13 KB
/
shared-utils.js
File metadata and controls
36 lines (33 loc) · 1.13 KB
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
// SideLlama Shared Utilities - Consolidates exact duplicate functions
// This eliminates code duplication across service-worker.js, sidepanel.js, and settings.js
class SharedUtils {
/**
* Format bytes to human readable format
* @param {number} bytes - Number of bytes
* @returns {string} Formatted string (e.g., "1.5 MB")
*/
static formatBytes(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}
/**
* Escape HTML to prevent XSS attacks
* @param {string} text - Text to escape
* @returns {string} HTML-escaped text
*/
static escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
}
// Export for both ES6 modules and CommonJS
if (typeof module !== 'undefined' && module.exports) {
module.exports = SharedUtils;
}
if (typeof window !== 'undefined') {
window.SharedUtils = SharedUtils;
}