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