Handle HTTP 401

This commit is contained in:
Maks Snegov 2024-04-25 22:44:54 -07:00
parent 9bbcc6e4a2
commit 84d608a9f4

View File

@ -33,7 +33,7 @@ async function delay(ms) {
async function sendNotification(message, channel = NOTIF_CHANNEL) {
await fetch('https://ntfy.sh/' + channel, {
method: 'POST',
body: message,
body: "[US visa bot] " + message,
})
.then(() => console.log('Notification sent'))
.catch(e => console.error(e));
@ -82,6 +82,13 @@ function diffObjects(obj1, obj2) {
return diff;
}
function getPathnameParts(pathname) {
let pathParts = pathname.split('/');
let locale = pathParts[1] || 'en-us';
let visaType = pathParts[2] || 'niv';
return { locale, visaType };
}
function isSignInPage() {
return Boolean(window.location.pathname.match(/^\/\w{2}-\w{2}\/n?iv\/users\/sign_in/));
}
@ -111,18 +118,49 @@ function isNotEnglishPage() {
) && !window.location.pathname.match(/^\/en-/);
}
async function handleHttpError(e) {
if (e.response && e.response.status === 401) {
msg = "Unauthorized";
console.log(msg);
await sendNotification(msg);
await logOut();
return null;
} else {
console.error(e);
return null;
}
}
async function switchToEnglishPage() {
window.location.href(window.location.pathname.replace(/^\/\w{2}-{2}/, '/en-us'));
await delay(PAGE_WAIT_TIME);
// Should be on English page
}
async function logOut() {
let [locale, visaType] = getPathnameParts(window.location.pathname);
let signOutPath = `/${locale}/${visaType}/users/sign_out`;
window.location.href = window.location.origin + signOutPath;
await delay(PAGE_WAIT_TIME);
return isLoggedOutPage();
}
async function goToSignInPage() {
document.querySelector(".homeSelectionsContainer a[href*='/sign_in']").click();
let [locale, visaType] = getPathnameParts(window.location.pathname);
let signInPath = `/${locale}/${visaType}/users/sign_in`;
window.location.href = window.location.origin + signInPath;
await delay(PAGE_WAIT_TIME);
return isSignInPage();
}
async function goToDashboardPage() {
let [locale, visaType] = getPathnameParts(window.location.pathname);
let dashboardPath = `/${locale}/${visaType}/account`;
window.location.href = window.location.origin + dashboardPath;
await delay(PAGE_WAIT_TIME);
return isDashboardPage();
}
async function enterCredentials() {
document.getElementById("user_email").value = config.username;
document.getElementById("user_password").value = config.password;
@ -173,8 +211,7 @@ async function getAvailableDates(consulateId) {
dateList.sort();
return dateList;
})
.catch(e => null);
// TODO catch for unauthorized
.catch(async e => handleHttpError(e));
return dates;
}
@ -193,8 +230,7 @@ async function getAvailableTimes(consulateId, date) {
let times = await fetch(uri, { headers: { "x-requested-with": "XMLHttpRequest" } })
.then(d => d.json())
.then(data => data.available_times)
.catch(e => null);
// TODO catch for unauthorized
.catch(e => handleHttpError(e));
return times;
}
@ -387,8 +423,7 @@ async function runner() {
// if no apptDate, fetch it from dashboard page
if (!config.currentAppt.date) {
console.log('No appointment date is set, going back to dashboard');
window.location = window.location.pathname.replace(/schedule.*/g, "/account");
await delay(PAGE_WAIT_TIME);
await goToDashboardPage();
isRunning = false;
return;
}
@ -544,7 +579,7 @@ async function runner() {
}
else if (isConfirmationPage) {
// go back to schedule after successful reschedule
// go back to dashboard after successful reschedule
await delay(PAGE_WAIT_TIME);
config.currentAppt = { consulate: null, date: null};
await chrome.storage.local.set({"__currentAppt": config.currentAppt});
@ -555,8 +590,7 @@ async function runner() {
config.consulates[consulate].autobook = false;
}
window.location = window.location.pathname.replace(/schedule.*/g, "");
await delay(PAGE_WAIT_TIME);
await goToDashboardPage();
}
// console.log('runner done');