Fix HTTP401 error

This commit is contained in:
Maks Snegov 2024-04-30 16:42:00 -07:00
parent 425ff34ea0
commit 2aaaa96dcf
2 changed files with 26 additions and 37 deletions

View File

@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "not-a-rescheduler",
"version": "0.0.7",
"version": "0.0.8",
"permissions": [ "storage", "tabs", "activeTab", "notifications", "declarativeContent" ],
"content_scripts": [
{

View File

@ -37,11 +37,12 @@ async function delay(ms) {
}
async function sendNotification(message, channel = NOTIF_CHANNEL) {
const msg = "[US visa bot] " + message
await fetch('https://ntfy.sh/' + channel, {
method: 'POST',
body: "[US visa bot] " + message,
body: msg,
})
.then(() => console.log('Notification sent'))
.then(() => console.log('Notification sent: ' + msg))
.catch(e => console.error(e));
}
@ -155,17 +156,6 @@ 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();
} else {
console.error(e);
}
}
async function switchToEnglishPage() {
window.location.href(window.location.pathname.replace(/^\/\w{2}-{2}/, '/en-us'));
await delay(PAGE_WAIT_TIME);
@ -244,28 +234,31 @@ async function getConsulates() {
}
async function getAvailableDates(consulateId) {
// let addressUri = window.location.pathname + `/address/${consulateId}`
// fetch(addressUri, { headers: { "x-requested-with": "XMLHttpRequest" } })
// .catch(error => console.error('Error:', error));
let datesUri = window.location.pathname + `/days/${consulateId}.json?appointments[expedite]=false`
let dates = fetch(datesUri, { headers: {
const datesUri = window.location.pathname + `/days/${consulateId}.json?appointments[expedite]=false`
try {
const response = await fetch(datesUri, { headers: {
"x-requested-with": "XMLHttpRequest",
"accept": "application/json, text/javascript, */*; q=0.01",
"cache-control": "no-cache",
}})
.catch(async e => {
await handleHttpError(e);
throw e;
})
.then(d => d.json())
.then(data => {
let dateList = data.map(item => item.date);
dateList.sort();
return dateList;
})
.catch(e => null);
return dates;
}});
if (!response.ok) {
if (response.status === 401) {
await sendNotification('Logged out due to 401 error');
await logOut();
}
const body = await response.text();
throw new Error(`HTTP error ${response.status}: ${body}`);
}
const data = await response.json();
const dateList = data.map(item => item.date);
dateList.sort();
return dateList;
} catch (e) {
console.error('Error:', e);
return null;
}
}
async function filterDates(dates, currentAppt, deltaFromAppt, deltaFromNow) {
@ -280,10 +273,6 @@ async function filterDates(dates, currentAppt, deltaFromAppt, deltaFromNow) {
async function getAvailableTimes(consulateId, date) {
let uri = window.location.pathname + `/times/${consulateId}.json?date=${date}&appointments[expedite]=false`
let times = fetch(uri, { headers: { "x-requested-with": "XMLHttpRequest" } })
.catch(async e => {
await handleHttpError(e);
throw e;
})
.then(d => d.json())
.then(data => data.available_times)
.catch(e => null);