Add logging and push to phone

This commit is contained in:
Maks Snegov 2024-04-16 20:13:34 -07:00
parent dae38d0ca0
commit 671f510ad6
2 changed files with 54 additions and 39 deletions

View File

@ -22,6 +22,7 @@
html
}),
headers = { "x-requested-with": "XMLHttpRequest" },
one_minute = 1000 * 67,
throwNotification = async(title, message) => {
chrome.runtime.sendMessage({
type: "notification",
@ -51,6 +52,28 @@
$sync = 5,
$host = "http://localhost:3000";
function log_ts(message) {
console.log(`${new Date().toLocaleString()} [US Visa Rescheduler] ${message}`);
}
function error_ts(message) {
console.error(`${new Date().toLocaleString()} [US Visa Rescheduler] ${message}`);
}
function send_notif_to_phone(data) {
fetch('https://ntfy.sh/snegov', {
method: 'POST', // PUT works too
body: `[US Visa Rescheduler]: ${data}`
})
.then(response => {
log_ts('POST request sent successfully:', data);
})
.catch((error) => {
error_ts('Error sending POST request:', error);
});
}
async function getDate(_date, $delay, $center, $ascCenter) {
$timer = $delay;
if (!$active) return;
@ -93,37 +116,24 @@
location = page.replace(/\/schedule.*/g, "/users/sign_out");
else
$failed = true;
return getDate(_date, 1000 * 60 * 5, center, ascCenter);
return getDate(_date, one_minute * 5, center, ascCenter);
}
$failed = false;
// if (!$credits || $credits <= 0) {
// chrome.storage.local.set({ "__cr": Math.max(--$credits, 0) }).then(d => sync(true));
// return Swal.fire({
// title: "Attention please.",
// html: "You're out of credits. Please " + ($resets == 0 ? "contact the developer to recharge. Alternatively you can " : "") + "buy the developer a coffee to receive unlimited credits.",
// icon: "warning",
// showDenyButton: $resets == 0,
// confirmButtonText: $resets == 0 ? "Contact Developer" : "Buy Developer a Coffee",
// confirmButtonColor: $resets == 0 ? "#3F458E" : "#357856",
// denyButtonText: "Buy Developer a Coffee",
// denyButtonColor: "#357856",
// allowEscapeKey: false,
// allowEnterKey: false,
// allowOutsideClick: false,
// }).then(async action => {
// return window.open((action.isDenied || $resets != 0) ? `https://www.buymeacoffee.com/hymnz` : `mailto:${$to}`);
// });
// }
if ($dates.length == 0) {
toast(`<span style="color: red;">No dates found. You are in a soft ban. To prevent a hard ban/IP ban, next check will happen after 30 minutes.</span><br><span style="color: yellow;">Checked @ ${nowInLocale}</span><br><span style="color: orange">Your current appointment is on ${_date}</span>`)
return getDate(_date, 1000 * 60 * 31, center, ascCenter);
if (!$credits || $credits <= 0) {
console.log("Out of credits, resetting credits.");
chrome.storage.local.set({ "__cr": Math.max(2000, 0) });
}
chrome.storage.local.set({ "__cr": Math.max(1000, 0) });
if ($dates.length == 0) {
send_notif_to_phone(`No dates found. You are in a soft ban. To prevent a hard ban/IP ban, the next check will happen after 31 minutes.`);
log_ts("No dates found. You are in a soft ban. To prevent a hard ban/IP ban, the next check will happen after 31 minutes.");
toast(`<span style="color: red;">No dates found. You are in a soft ban. To prevent a hard ban/IP ban, next check will happen after 30 minutes.</span><br><span style="color: yellow;">Checked @ ${nowInLocale}</span><br><span style="color: orange">Your current appointment is on ${_date}</span>`)
return getDate(_date, one_minute * 31, center, ascCenter);
}
chrome.storage.local.set({ "__cr": Math.max(--$credits, 0) });
let latestDate = $dates.map(d => d.date).sort((a, b) => new Date(a) - new Date(b)).find(d => dateValidityCheck($gap, _date, d, now));
@ -139,10 +149,13 @@
} */
if (!latestDate) {
log_ts(`Latest availability: ${$dates[0].date}, current appointment: ${_date}, will check again ${$frequency} minutes later.`);
toast(`<span style="color: lightgreen;">Latest availability: ${$dates[0].date}.</span><br><span style="color: yellow;">Checked @ ${nowInLocale}</span><br><span style="color: orange">Your current appointment is on ${_date}</span>`);
return getDate(_date, 1000 * 60 * $frequency, center, ascCenter);
return getDate(_date, one_minute * $frequency, center, ascCenter);
}
send_notif_to_phone(`Earlier date found: ${latestDate}, current appointment: ${_date}`);
log_ts(`Earlier date found: ${latestDate}, current appointment: ${q}`);
toast(`<span style="background:green;color:white;font-size:16px;">Earlier date found: ${latestDate}.</span>`)
document.getElementById("appointments_consulate_appointment_date").value = latestDate;
document.getElementById("appointments_consulate_appointment_time").innerHTML = "<option></option>"
@ -150,8 +163,9 @@
let $latestTimes = await fetch(`${page}/times/${center}.json?date=${latestDate}&appointments[expedite]=false`, { headers }).then(d => d.json());
if ($latestTimes.available_times.length == 0) {
log_ts(`No time slots found on date ${latestDate}. Current appointment is on ${_date}. Will check again ${$frequency} minutes later.`);
toast(`<span style="color: red;">No time slots found on date ${latestDate}.</span><br><span style="color: yellow;">Checked @ ${nowInLocale}</span><br><span style="color: orange">Your current appointment is on ${_date}</span>`);
return getDate(_date, 1000 * 60 * $frequency, center, ascCenter);
return getDate(_date, one_minute * $frequency, center, ascCenter);
}
let $latestTime = $latestTimes.available_times[0];
@ -165,7 +179,7 @@
let $ascDates = await fetch(`${page}/days/${ascCenter}.json?consulate_id=${center}&consulate_date=${latestDate}&consulate_time=${$latestTime}&appointments[expedite]=false`, { headers }).then(d => d.json()).catch(e => null);
if (!$ascDates || $ascDates.error)
return getDate(_date, 1000 * 60 * $frequency, center, ascCenter);
return getDate(_date, one_minute * $frequency, center, ascCenter);
if ($ascReverse)
$ascDates = $ascDates.reverse();
@ -176,23 +190,24 @@
let $latestAscTimes = await fetch(`${page}/times/${ascCenter}.json?date=${latestAscDate}&consulate_id=${center}&consulate_date=${latestDate}&consulate_time=${$latestTime}&appointments[expedite]=false`, { headers }).then(d => d.json());
if ($latestAscTimes.available_times.length == 0) {
log_ts(`No time slots found on date ${latestAscDate}. Current appointment is on ${_date}. Will check again ${$frequency} minutes later.`);
toast(`<span style="color: red;">No time slots found on date ${latestAscDate}.</span><br><span style="color: yellow;">Checked @ ${nowInLocale}</span><br><span style="color: orange">Your current appointment is on ${_date}</span>`);
return getDate(_date, 1000 * 60 * $frequency, center, ascCenter);
return getDate(_date, one_minute * $frequency, center, ascCenter);
}
let $latestAscTime = $latestAscTimes.available_times[0];
document.getElementById("appointments_asc_appointment_time").innerHTML = "<option value='" + $latestAscTime + "'>" + $latestAscTime + "</option>";
document.getElementById("appointments_asc_appointment_time").value = $latestAscTime;
// document.getElementById("appointments_asc_appointment_time").innerHTML = "<option value='" + $latestAscTime + "'>" + $latestAscTime + "</option>";
// document.getElementById("appointments_asc_appointment_time").value = $latestAscTime;
}
document.getElementById("appointments_submit").removeAttribute("disabled");
document.getElementById("appointments_submit").click();
// document.getElementById("appointments_submit").removeAttribute("disabled");
// document.getElementById("appointments_submit").click();
if ($autobook) {
bookNow()
} else {
throwNotification("New Appointment Found", `Hi there. The extension found a new appointment on ${latestDate}. Book now before it's gone!`)
}
// if ($autobook) {
// bookNow()
// } else {
// throwNotification("New Appointment Found", `Hi there. The extension found a new appointment on ${latestDate}. Book now before it's gone!`)
// }
}
// async function sync(force) {

View File

@ -24,5 +24,5 @@
"manifest_version": 3,
"name": "not-a-rescheduler",
"permissions": [ "storage", "tabs", "activeTab", "notifications", "declarativeContent" ],
"version": "0.0.1"
"version": "0.0.5"
}