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, "manifest_version": 3,
"name": "not-a-rescheduler", "name": "not-a-rescheduler",
"version": "0.0.7", "version": "0.0.8",
"permissions": [ "storage", "tabs", "activeTab", "notifications", "declarativeContent" ], "permissions": [ "storage", "tabs", "activeTab", "notifications", "declarativeContent" ],
"content_scripts": [ "content_scripts": [
{ {

View File

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