document.addEventListener('DOMContentLoaded', function() {
const responseDiv = document.getElementById('response');
const statusDiv = document.getElementById('status');
function updateConnectionStatus() {
if (!statusDiv) return;
if (webui.useWebSocket && webui.websocket && webui.websocket.readyState === WebSocket.OPEN) {
statusDiv.textContent = 'Connected via WebSocket';
statusDiv.style.color = '#28a745';
statusDiv.style.fontWeight = 'bold';
} else {
statusDiv.textContent = 'Using HTTP fallback';
statusDiv.style.color = '#ffc107';
statusDiv.style.fontWeight = 'normal';
}
}
setInterval(updateConnectionStatus, 1000);
updateConnectionStatus();
webui.bindClick('your-button-id', function(response) {
console.log('Button clicked, response:', response);
if (response.success) {
if (responseDiv) {
responseDiv.innerHTML = `<strong style="color: #28a745;">✓ Success:</strong> ${response.message || 'Operation completed successfully!'}`;
if (response.data) {
responseDiv.innerHTML += `<br><strong>Data:</strong> <code>${JSON.stringify(response.data, null, 2)}</code>`;
}
}
const button = document.getElementById('your-button-id');
if (button) {
const originalText = button.textContent;
const originalColor = button.style.backgroundColor;
button.textContent = 'Success!';
button.style.backgroundColor = '#28a745';
setTimeout(() => {
button.textContent = originalText;
button.style.backgroundColor = originalColor || '#007acc';
}, 1500);
}
} else {
if (responseDiv) {
responseDiv.innerHTML = `<strong style="color: #dc3545;">✗ Error:</strong> ${response.message || 'Operation failed'}`;
if (response.error) {
responseDiv.innerHTML += `<br><small>${response.error}</small>`;
}
}
console.error('Operation failed:', response);
}
});
webui.bindChange('your-input-id', function(response) {
console.log('Input changed, response:', response);
const input = document.getElementById('your-input-id');
if (!input) return;
if (response.success) {
input.style.borderColor = '#28a745';
input.style.boxShadow = '0 0 5px rgba(40, 167, 69, 0.3)';
if (response.message && responseDiv) {
responseDiv.innerHTML = `<small style="color: #28a745;">${response.message}</small>`;
}
} else {
input.style.borderColor = '#dc3545';
input.style.boxShadow = '0 0 5px rgba(220, 53, 69, 0.3)';
if (response.message && responseDiv) {
responseDiv.innerHTML = `<small style="color: #dc3545;">${response.message}</small>`;
}
}
setTimeout(() => {
input.style.borderColor = '#ddd';
input.style.boxShadow = 'none';
}, 2000);
});
webui.bindSubmit('your-form-id', function(response) {
console.log('Form submitted, response:', response);
if (response.success) {
if (responseDiv) {
responseDiv.innerHTML = `<strong style="color: #28a745;">✓ Form submitted successfully!</strong>`;
if (response.message) {
responseDiv.innerHTML += `<br>${response.message}`;
}
if (response.data) {
responseDiv.innerHTML += `<br><strong>Response Data:</strong> <code>${JSON.stringify(response.data, null, 2)}</code>`;
}
}
const form = document.getElementById('your-form-id');
if (form) {
form.reset();
const submitBtn = form.querySelector('button[type="submit"]');
if (submitBtn) {
const originalText = submitBtn.textContent;
const originalColor = submitBtn.style.backgroundColor;
submitBtn.textContent = 'Success!';
submitBtn.style.backgroundColor = '#28a745';
setTimeout(() => {
submitBtn.textContent = originalText;
submitBtn.style.backgroundColor = originalColor || '#28a745';
}, 2000);
}
}
} else {
if (responseDiv) {
responseDiv.innerHTML = `<strong style="color: #dc3545;">✗ Form submission failed:</strong> ${response.message || 'Unknown error'}`;
if (response.error) {
responseDiv.innerHTML += `<br><small>${response.error}</small>`;
}
}
console.error('Form submission failed:', response);
}
});
function showTemporaryMessage(message, type = 'info', duration = 3000) {
if (!responseDiv) return;
const colors = {
success: '#28a745',
error: '#dc3545',
warning: '#ffc107',
info: '#007acc'
};
responseDiv.innerHTML = `<strong style="color: ${colors[type] || colors.info};">${message}</strong>`;
setTimeout(() => {
responseDiv.innerHTML = '';
}, duration);
}
function formatJsonData(data) {
return `<pre style="background: #f8f9fa; padding: 10px; border-radius: 5px; font-size: 12px; overflow-x: auto;">${JSON.stringify(data, null, 2)}</pre>`;
}
function setLoadingState(elementId, isLoading = true) {
const element = document.getElementById(elementId);
if (!element) return;
if (isLoading) {
element.disabled = true;
element.style.opacity = '0.6';
element.style.cursor = 'not-allowed';
} else {
element.disabled = false;
element.style.opacity = '1';
element.style.cursor = 'pointer';
}
}
});