function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
function login_menu(basic_auth) {
topFunction();
var html = '
Admin Login
';
html += '';
document.getElementById("setup").innerHTML = html;
}
function log_in(basic_auth) {
// Disable button
document.getElementById("log_in_button").disabled = true;
document.getElementById("log_in_button").style.opacity = '0.5';
// Get variables
if(!basic_auth) {
password = document.getElementById('password').value;
username = document.getElementById('username').value;
admin_login_form = {"admin_password" : password, "admin_username" : username};
var admin_login_data = JSON.stringify(admin_login_form);
} else {
var admin_login_data = ""
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
console.log('Failed to parse API response. Response: ' + this.responseText)
document.getElementById("log_in_button").disabled = false;
document.getElementById("log_in_button").style.opacity = '1';
document.getElementById('password').value = '';
return;
}
if(result.error) {
try {
document.getElementById("password_login_form_error").innerHTML = result.error;
document.getElementById("log_in_button").disabled = false;
document.getElementById("log_in_button").style.opacity = '1';
document.getElementById('password').value = '';
} catch(e) {
console.log("Failed to reset input fields. Error: " + e)
}
} else {
tokenPrefix = "Bearer "
set_cookie("wrapperr-admin", tokenPrefix+result.data, 3);
location.reload();
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "login/admin");
xhttp.send(admin_login_data);
return;
}
function set_password_form() {
topFunction();
var html = 'Create admin
';
html += '';
document.getElementById("setup").innerHTML = html;
}
function set_password() {
// Disable button
document.getElementById("create_admin_button").disabled = true;
document.getElementById("create_admin_button").style.opacity = '0.5';
// Check password match
if(document.getElementById('password').value != document.getElementById('password_2').value) {
alert("The passwords must match.");
document.getElementById('password').value = "";
document.getElementById('password_2').value = "";
document.getElementById('password').focus();
document.getElementById("create_admin_button").disabled = false;
document.getElementById("create_admin_button").style.opacity = '1';
return false;
} else {
password = document.getElementById('password').value;
username = document.getElementById('username').value;
}
admin_create_form = {"admin_password" : password, "admin_username" : username};
var admin_create_data = JSON.stringify(admin_create_form);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
console.log('Failed to parse API response. Response: ' + this.responseText)
document.getElementById("create_admin_button").disabled = false;
document.getElementById("create_admin_button").style.opacity = '1';
return;
}
if(result.error) {
document.getElementById("password_form_error").innerHTML = result.error;
document.getElementById("create_admin_button").disabled = false;
document.getElementById("create_admin_button").style.opacity = '1';
} else {
location.reload();
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "create/admin");
xhttp.send(admin_create_data);
return;
}
function update_password_form() {
topFunction();
var html = '';
html += ``;
html += '
';
html += '';
html += '
';
html += '';
html += '';
document.getElementById("setup").innerHTML = html;
}
function update_password() {
// Disable button
document.getElementById("update_admin_button").disabled = true;
document.getElementById("update_admin_button").style.opacity = '0.5';
// Check password match
if(document.getElementById('password').value != document.getElementById('password_2').value) {
alert("The passwords must match.");
document.getElementById('password').value = "";
document.getElementById('password_2').value = "";
document.getElementById('password').focus();
document.getElementById("update_admin_button").disabled = false;
document.getElementById("update_admin_button").style.opacity = '1';
return false;
} else {
password = document.getElementById('password').value;
password_original = document.getElementById('password_original').value;
username = document.getElementById('username').value;
}
admin_create_form = {"admin_password" : password, "admin_password_original" : password_original, "admin_username" : username};
var admin_create_data = JSON.stringify(admin_create_form);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
document.getElementById("password_form_error").innerHTML = 'Failed to parse API response.';
console.log('Failed to parse API response. Response: ' + this.responseText)
document.getElementById("update_admin_button").disabled = false;
document.getElementById("update_admin_button").style.opacity = '1';
return;
}
if(result.error) {
document.getElementById("password_form_error").innerHTML = result.error;
document.getElementById("update_admin_button").disabled = false;
document.getElementById("update_admin_button").style.opacity = '1';
} else {
set_cookie("wrapperr-admin", "", 1);
location.reload();
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "update/admin");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", cookie);
xhttp.send(admin_create_data);
return;
}
function loadAdminPage() {
var html = `
`;
html += ``;
html += `
`;
html += ``;
html += ``;
html += ``;
html += ``;
html += ``;
html += ``;
html += `
`;
html += ``;
html += ``;
html += `
`;
html += ``;
html += ``;
html += `
`;
document.getElementById("setup").innerHTML = html;
if(timezone === '') {
document.getElementById("set_wrapperr_settings").style.border = '0.15em dashed var(--red)';
document.getElementById("caching_menu").disabled = true;
document.getElementById("caching_menu").style.opacity = '0.5';
}
if(wrapped_start === '' || wrapped_end === '') {
document.getElementById("set_wrapperr_customization").style.border = '0.15em dashed var(--red)';
document.getElementById("caching_menu").disabled = true;
document.getElementById("caching_menu").style.opacity = '0.5';
}
if(tautulli[0].tautulli_ip === '' || tautulli[0].tautulli_apikey === '' || tautulli[0].tautulli_length === '' || tautulli[0].tautulli_port == 0 || tautulli[0].tautulli_length == 0) {
document.getElementById("set_tautulli_settings").style.border = '0.15em dashed var(--red)';
document.getElementById("caching_menu").disabled = true;
document.getElementById("caching_menu").style.opacity = '0.5';
}
if(!use_logs) {
document.getElementById("log_menu").disabled = true;
document.getElementById("log_menu").style.opacity = '0.5';
}
}