96 lignes
2.5 KiB
JavaScript
96 lignes
2.5 KiB
JavaScript
function injectUsbMenu() {
|
|
if (document.getElementById("usbControlItem")) return;
|
|
|
|
const menu = document.querySelector('.dropdown-menu');
|
|
if (!menu) return;
|
|
|
|
const anchor = document.querySelector('#dashboard-menu-item');
|
|
|
|
const li = document.createElement("li");
|
|
li.id = "usbControlItem";
|
|
li.className = "context-menu";
|
|
|
|
li.innerHTML = `
|
|
<a id="usbControlLink" href="#notarget">
|
|
<i class="fa-solid fa-sharp fa-power-off sx"></i>
|
|
<span>USB Control</span>
|
|
<span id="usbControlDot"
|
|
style="display:inline-block;width:12px;height:12px;border-radius:50%;background:#b22;margin-left:3px;margin-right:3px">
|
|
</span>
|
|
<span id="usbControlLabel">...</span>
|
|
</a>
|
|
`;
|
|
|
|
if (anchor && anchor.parentNode) {
|
|
anchor.parentNode.insertBefore(li, anchor.nextSibling);
|
|
} else {
|
|
menu.appendChild(li);
|
|
}
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", injectUsbMenu);
|
|
setTimeout(injectUsbMenu, 1000);
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const item = document.getElementById('usbControlItem');
|
|
const dot = document.getElementById('usbControlDot');
|
|
const label = document.getElementById('usbControlLabel');
|
|
const link = document.getElementById('usbControlLink');
|
|
|
|
if (!item || !dot || !label || !link) return;
|
|
|
|
let isOn = false;
|
|
let locked = false;
|
|
|
|
// --- Fonction utilitaire : met à jour la pastille
|
|
function updateUI() {
|
|
dot.style.background = isOn ? '#2b2' : '#b22';
|
|
label.textContent = isOn ? 'ON' : 'OFF';
|
|
}
|
|
|
|
// --- Fonction pour changer l’état
|
|
function toggleUSB() {
|
|
event.stopPropagation();
|
|
if (locked) return;
|
|
locked = true;
|
|
|
|
label.textContent = '...';
|
|
fetch(`/usb-toggle.php?action=${isOn ? 'unbind' : 'bind'}`)
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
if (data.status === 'ok') {
|
|
isOn = !isOn;
|
|
updateUI();
|
|
} else {
|
|
alert('Erreur: ' + data.msg);
|
|
}
|
|
})
|
|
.catch(err => alert('Erreur réseau: ' + err))
|
|
.finally(() => {
|
|
locked = false;
|
|
});
|
|
}
|
|
|
|
// --- Lecture de l’état au démarrage
|
|
fetch('/usb-toggle.php?action=status')
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
if (data.status === 'ok') {
|
|
isOn = (data.state === 'bound');
|
|
updateUI();
|
|
}
|
|
})
|
|
.catch(() => {
|
|
label.textContent = '?';
|
|
});
|
|
|
|
function mouseLeave() {
|
|
link.blur();
|
|
|
|
}
|
|
|
|
item.addEventListener('click', toggleUSB);
|
|
link.addEventListener('mouseleave', mouseLeave);
|
|
});
|