3 Commits

Author SHA1 Message Date
2aaaa96dcf Fix HTTP401 error 2024-04-30 16:42:52 -07:00
425ff34ea0 Remove operational hours 2024-04-30 16:41:36 -07:00
3c72a20878 Update currentDate in popup to "-" if date not found 2024-04-29 22:34:59 -07:00
3 changed files with 36 additions and 59 deletions

View File

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

View File

@@ -148,21 +148,19 @@ function smoothTextChange(element, newText) {
consCfg[c].autoBook = e.target.checked; consCfg[c].autoBook = e.target.checked;
await chrome.storage.local.set({ "cfg_consulates": consCfg }); await chrome.storage.local.set({ "cfg_consulates": consCfg });
}); });
// update current & best dates // update current & best dates
chrome.storage.onChanged.addListener((changes, area) => { chrome.storage.onChanged.addListener((changes, area) => {
if (changes.ctx_consulates) { if (changes.ctx_consulates) {
const newConsulates = changes.ctx_consulates.newValue; let newCurrentDate = changes.ctx_consulates.newValue[c].currentDate || "-";
let newBestDate = changes.ctx_consulates.newValue[c].bestDate || "-";
if (newConsulates[c].currentDate) {
let el = document.getElementById(`currentDate-${cId}`); let el = document.getElementById(`currentDate-${cId}`);
if (el && el.innerText != newConsulates[c].currentDate) if (el && el.innerText != newCurrentDate)
smoothTextChange(el, newConsulates[c].currentDate); smoothTextChange(el, newCurrentDate);
} el = document.getElementById(`bestDate-${cId}`);
if (newConsulates[c].bestDate) { if (el && el.innerText != newBestDate)
let el = document.getElementById(`bestDate-${cId}`); smoothTextChange(el, newBestDate);
if (el && el.innerText != newConsulates[c].bestDate)
smoothTextChange(el, newConsulates[c].bestDate);
}
} }
}); });
} }

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);
@@ -393,17 +382,6 @@ async function runner() {
return; return;
} }
// Check if current time is between 11pm and 9am UTC (4pm - 2am PST)
let now = new Date();
let currentHourUTC = now.getUTCHours();
if (currentHourUTC >= 23 || currentHourUTC < 9) {
// Continue running the code
} else {
await chrome.storage.local.set({ "ctx_statusMsg": "not operational hours" });
isRunning = false;
return;
}
if (isFoundAppointment) { if (isFoundAppointment) {
// don't do anything if appointment is found and manual booking is required // don't do anything if appointment is found and manual booking is required
if (isAppointmentPage()) { if (isAppointmentPage()) {
@@ -625,6 +603,7 @@ async function runner() {
if (!(currentHourUTC === 23 && currentMinuteUTC < 5)) { if (!(currentHourUTC === 23 && currentMinuteUTC < 5)) {
ctx.consulates[c].nextCheckAt = getFutureDate(SOFT_BAN_TIMEOUT, getJitter(cfg.frequency)); ctx.consulates[c].nextCheckAt = getFutureDate(SOFT_BAN_TIMEOUT, getJitter(cfg.frequency));
} }
ctx.consulates[c].currentDate = null;
continue; continue;
} }