Fix JS code quality and safety issues in fossbilling.js (#3277)

* Fix JS code quality issues in fossbilling.js

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* Refactor complex clipboard handler into focused helper functions

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
This commit is contained in:
Copilotandadmdly authored and GitHub committed 2026-02-27 21:36:08 +00:00
1 parent 882912345d
commit 97d8a92408
1 file changed
+98 -58
+98 -58
View File
@@ -2,7 +2,7 @@ import './js/ui/modals';
import { initAvatars } from './js/avatar.js';
import { coloris, init } from '@melloware/coloris';
import * as tabler from '@tabler/core/js/tabler.js';
import './js/tomselect'
import './js/tomselect';
import './js/datepicker'
import ApexCharts from 'apexcharts';
import './js/ui/theme_settings';
@@ -19,14 +19,104 @@ coloris({
});
/**
* Extracts text from the clipboard target element referenced by the button.
* Returns null if the selector is missing, invalid, or the element is not found.
*/
function getClipboardTargetText(button) {
const targetSelector = button.dataset.clipboardTarget;
if (typeof targetSelector !== 'string') return null;
// Only allow simple ID selectors such as "#element-id"
if (!/^#[A-Za-z0-9_\-.]+$/.test(targetSelector)) {
return null;
}
const targetElement = document.querySelector(targetSelector);
if (!targetElement) return null;
return targetElement.value || targetElement.textContent;
}
/**
* Attempts to copy the given text to the clipboard.
* Falls back to a legacy execCommand approach when the Clipboard API is unavailable.
*/
async function copyTextToClipboard(text) {
if (navigator.clipboard?.writeText) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (err) {
// Fall through to legacy fallback
}
}
let textarea;
try {
textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
return document.execCommand('copy');
} catch (err) {
return false;
} finally {
if (textarea && textarea.parentNode) {
textarea.parentNode.removeChild(textarea);
}
}
}
/**
* Shows a "Copied" tooltip on success or an error message on failure.
*/
function handleClipboardResult(button, success) {
if (success) {
let tooltip = bootstrap.Tooltip.getInstance(button);
if (!tooltip) {
tooltip = new bootstrap.Tooltip(button, { trigger: 'manual' });
}
const originalTitle = button.dataset.bsOriginalTitle;
button.dataset.bsOriginalTitle = 'Copied';
tooltip.show();
setTimeout(() => {
button.dataset.bsOriginalTitle = originalTitle;
tooltip.hide();
}, 2000);
} else {
if (typeof FOSSBilling !== 'undefined' && FOSSBilling.message) {
FOSSBilling.message('Failed to copy to clipboard', 'error');
}
}
}
document.addEventListener('DOMContentLoaded', () => {
initAvatars();
document.querySelectorAll('.js-theme-toggler').forEach(element => {
element.addEventListener('click', event => {
event.preventDefault();
localStorage.setItem('theme', element.getAttribute('href').split('=')[1]);
document.documentElement.setAttribute("data-bs-theme", localStorage.getItem('theme'))
const href = element.getAttribute('href') || '';
let theme = null;
// Try to extract theme value safely from href
if (href.includes('=')) {
const parts = href.split('=');
if (parts.length > 1 && parts[1]) {
theme = parts[1];
}
}
if (!theme) {
return;
}
localStorage.setItem('theme', theme);
document.documentElement.setAttribute('data-bs-theme', theme);
});
});
@@ -34,7 +124,7 @@ document.addEventListener('DOMContentLoaded', () => {
/**
* Enable Bootstrap Tooltip
*/
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
const tooltipTriggerList = Array.from(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.forEach(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl, {
'trigger': 'hover'
@@ -49,60 +139,10 @@ document.addEventListener('DOMContentLoaded', () => {
const button = event.target.closest('.clipboard-copy');
if (!button) return;
const targetSelector = button.dataset.clipboardTarget;
const targetElement = document.querySelector(targetSelector);
if (!targetElement) return;
const text = getClipboardTargetText(button);
if (text === null) return;
const text = targetElement.value || targetElement.textContent || targetElement.innerText;
let success = false;
if (navigator.clipboard?.writeText) {
try {
await navigator.clipboard.writeText(text);
success = true;
} catch (err) {
// Fall through to legacy fallback
}
}
if (!success) {
let textarea;
try {
textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
success = document.execCommand('copy');
} catch (err) {
// Fall through to error
} finally {
if (textarea && textarea.parentNode) {
textarea.parentNode.removeChild(textarea);
}
}
}
if (success) {
let tooltip = bootstrap.Tooltip.getInstance(button);
if (!tooltip) {
tooltip = new bootstrap.Tooltip(button, {
trigger: 'manual'
});
}
const originalTitle = button.dataset.bsOriginalTitle;
button.dataset.bsOriginalTitle = 'Copied';
tooltip.show();
setTimeout(() => {
button.dataset.bsOriginalTitle = originalTitle;
tooltip.hide();
}, 2000);
} else {
if (typeof FOSSBilling !== 'undefined' && FOSSBilling.message) {
FOSSBilling.message('Failed to copy to clipboard', 'error');
}
}
const success = await copyTextToClipboard(text);
handleClipboardResult(button, success);
});
});