4 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
4f83907506 Don't ask for address when getting dates 2024-04-29 18:43:17 -07:00
3 changed files with 37 additions and 60 deletions

View File

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

View File

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

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;
"cache-control": "no-cache",
}});
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);
@@ -393,17 +382,6 @@ async function runner() {
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) {
// don't do anything if appointment is found and manual booking is required
if (isAppointmentPage()) {
@@ -625,6 +603,7 @@ async function runner() {
if (!(currentHourUTC === 23 && currentMinuteUTC < 5)) {
ctx.consulates[c].nextCheckAt = getFutureDate(SOFT_BAN_TIMEOUT, getJitter(cfg.frequency));
}
ctx.consulates[c].currentDate = null;
continue;
}