|
|
|
@@ -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()) {
|
|
|
|
|