2022-12-28 20:05:10 +01:00
|
|
|
"use strict";
|
2023-01-24 23:31:38 +01:00
|
|
|
/* eslint-env es2017, browser */
|
|
|
|
/* exported BASE_URL, _post */
|
2022-12-28 20:05:10 +01:00
|
|
|
|
|
|
|
function getBaseUrl() {
|
|
|
|
// If the base URL is `https://vaultwarden.example.com/base/path/`,
|
|
|
|
// `window.location.href` should have one of the following forms:
|
|
|
|
//
|
|
|
|
// - `https://vaultwarden.example.com/base/path/`
|
|
|
|
// - `https://vaultwarden.example.com/base/path/#/some/route[?queryParam=...]`
|
|
|
|
//
|
|
|
|
// We want to get to just `https://vaultwarden.example.com/base/path`.
|
|
|
|
const baseUrl = window.location.href;
|
|
|
|
const adminPos = baseUrl.indexOf("/admin");
|
|
|
|
return baseUrl.substring(0, adminPos != -1 ? adminPos : baseUrl.length);
|
|
|
|
}
|
|
|
|
const BASE_URL = getBaseUrl();
|
|
|
|
|
|
|
|
function reload() {
|
|
|
|
// Reload the page by setting the exact same href
|
|
|
|
// Using window.location.reload() could cause a repost.
|
|
|
|
window.location = window.location.href;
|
|
|
|
}
|
|
|
|
|
|
|
|
function msg(text, reload_page = true) {
|
|
|
|
text && alert(text);
|
|
|
|
reload_page && reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _post(url, successMsg, errMsg, body, reload_page = true) {
|
2023-01-24 23:31:38 +01:00
|
|
|
let respStatus;
|
|
|
|
let respStatusText;
|
2022-12-28 20:05:10 +01:00
|
|
|
fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
body: body,
|
|
|
|
mode: "same-origin",
|
|
|
|
credentials: "same-origin",
|
|
|
|
headers: { "Content-Type": "application/json" }
|
|
|
|
}).then( resp => {
|
2023-01-24 23:31:38 +01:00
|
|
|
if (resp.ok) {
|
|
|
|
msg(successMsg, reload_page);
|
|
|
|
// Abuse the catch handler by setting error to false and continue
|
|
|
|
return Promise.reject({error: false});
|
|
|
|
}
|
|
|
|
respStatus = resp.status;
|
|
|
|
respStatusText = resp.statusText;
|
2022-12-28 20:05:10 +01:00
|
|
|
return resp.text();
|
|
|
|
}).then( respText => {
|
|
|
|
try {
|
|
|
|
const respJson = JSON.parse(respText);
|
2023-01-24 23:31:38 +01:00
|
|
|
if (respJson.ErrorModel && respJson.ErrorModel.Message) {
|
|
|
|
return respJson.ErrorModel.Message;
|
|
|
|
} else {
|
|
|
|
return Promise.reject({body:`${respStatus} - ${respStatusText}\n\nUnknown error`, error: true});
|
|
|
|
}
|
2022-12-28 20:05:10 +01:00
|
|
|
} catch (e) {
|
2023-01-24 23:31:38 +01:00
|
|
|
return Promise.reject({body:`${respStatus} - ${respStatusText}\n\n[Catch] ${e}`, error: true});
|
2022-12-28 20:05:10 +01:00
|
|
|
}
|
|
|
|
}).then( apiMsg => {
|
2023-01-24 23:31:38 +01:00
|
|
|
msg(`${errMsg}\n${apiMsg}`, reload_page);
|
2022-12-28 20:05:10 +01:00
|
|
|
}).catch( e => {
|
|
|
|
if (e.error === false) { return true; }
|
2023-01-24 23:31:38 +01:00
|
|
|
else { msg(`${errMsg}\n${e.body}`, reload_page); }
|
2022-12-28 20:05:10 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// onLoad events
|
|
|
|
document.addEventListener("DOMContentLoaded", (/*event*/) => {
|
|
|
|
// get current URL path and assign "active" class to the correct nav-item
|
|
|
|
const pathname = window.location.pathname;
|
|
|
|
if (pathname === "") return;
|
|
|
|
const navItem = document.querySelectorAll(`.navbar-nav .nav-item a[href="${pathname}"]`);
|
|
|
|
if (navItem.length === 1) {
|
|
|
|
navItem[0].className = navItem[0].className + " active";
|
|
|
|
navItem[0].setAttribute("aria-current", "page");
|
|
|
|
}
|
|
|
|
});
|