Stable
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* UrBackup API Test JavaScript
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
function testApi(serverId, resultBox) {
|
||||
if (!serverId || !resultBox) return;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/plugins/urbackup/ajax/server_test.php', true);
|
||||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||||
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||||
xhr.timeout = 8000;
|
||||
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
try {
|
||||
var data = JSON.parse(xhr.responseText);
|
||||
if (data.success) {
|
||||
resultBox.innerHTML = '<span class="text-success fw-bold"><i class="ti ti-check"></i> ' + 'API connection OK' + '</span>';
|
||||
} else {
|
||||
var message = data.message || 'Connection failed';
|
||||
var isNetwork = /timeout|could not resolve|couldn't connect|connection refused|connection timed out|network is unreachable|no route to host|returned HTTP status/i.test(message);
|
||||
var statusClass = isNetwork ? 'text-warning' : 'text-danger';
|
||||
var icon = isNetwork ? 'ti-wifi-off' : 'ti-x';
|
||||
var label = isNetwork ? 'Server unreachable' : 'API connection failed';
|
||||
resultBox.innerHTML = '<span class="' + statusClass + ' fw-bold"><i class="ti ' + icon + '"></i> ' + label + '</span><br><small class="text-muted">' + message + '</small>';
|
||||
}
|
||||
} catch (e) {
|
||||
resultBox.innerHTML = '<span class="text-danger fw-bold"><i class="ti ti-x"></i> Error</span>';
|
||||
}
|
||||
} else {
|
||||
resultBox.innerHTML = '<span class="text-danger fw-bold"><i class="ti ti-x"></i> HTTP ' + xhr.status + '</span>';
|
||||
}
|
||||
};
|
||||
|
||||
xhr.ontimeout = function () {
|
||||
resultBox.innerHTML = '<span class="text-warning fw-bold"><i class="ti ti-wifi-off"></i> Server unreachable</span><br><small class="text-muted">Connection timeout</small>';
|
||||
};
|
||||
|
||||
xhr.onerror = function () {
|
||||
resultBox.innerHTML = '<span class="text-warning fw-bold"><i class="ti ti-wifi-off"></i> Server unreachable</span><br><small class="text-muted">Network error</small>';
|
||||
};
|
||||
|
||||
xhr.send('id=' + encodeURIComponent(serverId));
|
||||
}
|
||||
|
||||
function initApiStatusCheck() {
|
||||
var statusBox = document.getElementById('plugin-urbackup-api-status');
|
||||
if (!statusBox) return;
|
||||
if (statusBox._initialized) return;
|
||||
statusBox._initialized = true;
|
||||
|
||||
var serverId = statusBox.getAttribute('data-server-id');
|
||||
if (serverId) {
|
||||
testApi(serverId, statusBox);
|
||||
}
|
||||
}
|
||||
|
||||
function initApiTestButtons() {
|
||||
var buttons = document.querySelectorAll('.plugin-urbackup-test-api');
|
||||
|
||||
buttons.forEach(function (button) {
|
||||
if (button._initialized) return;
|
||||
button._initialized = true;
|
||||
|
||||
button.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
var serverId = button.getAttribute('data-server-id');
|
||||
var resultBox = document.getElementById('plugin-urbackup-api-test-result');
|
||||
|
||||
testApi(serverId, resultBox);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', initApiStatusCheck);
|
||||
document.addEventListener('DOMContentLoaded', initApiTestButtons);
|
||||
} else {
|
||||
setTimeout(initApiStatusCheck, 100);
|
||||
setTimeout(initApiTestButtons, 100);
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user