82 lines
3.1 KiB
JavaScript
82 lines
3.1 KiB
JavaScript
/**
|
|
* UrBackup API Test JavaScript
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
function testApi(serverId, resultBox) {
|
|
if (!serverId || !resultBox) return;
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('POST', '/plugins/urbackup/front/server_test.ajax.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 {
|
|
resultBox.innerHTML = '<span class="text-danger fw-bold"><i class="ti ti-x"></i> API connection failed</span><br><small class="text-muted">' + (data.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-danger fw-bold"><i class="ti ti-x"></i> API connection failed</span><br><small class="text-muted">Connection timeout</small>';
|
|
};
|
|
|
|
xhr.onerror = function () {
|
|
resultBox.innerHTML = '<span class="text-danger fw-bold"><i class="ti ti-x"></i> API connection failed</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);
|
|
}
|
|
})(); |