From 671f510ad6bcbf460e0d2ae3ad7cc6273cc06b9b Mon Sep 17 00:00:00 2001 From: Maks Snegov Date: Tue, 16 Apr 2024 20:13:34 -0700 Subject: [PATCH] Add logging and push to phone --- js/rescheduler.js | 91 +++++++++++++++++++++++++++-------------------- manifest.json | 2 +- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/js/rescheduler.js b/js/rescheduler.js index fd39eb4..56e0fb9 100644 --- a/js/rescheduler.js +++ b/js/rescheduler.js @@ -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(`No dates found. You are in a soft ban. To prevent a hard ban/IP ban, next check will happen after 30 minutes.
Checked @ ${nowInLocale}
Your current appointment is on ${_date}`) - 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(`No dates found. You are in a soft ban. To prevent a hard ban/IP ban, next check will happen after 30 minutes.
Checked @ ${nowInLocale}
Your current appointment is on ${_date}`) + 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(`Latest availability: ${$dates[0].date}.
Checked @ ${nowInLocale}
Your current appointment is on ${_date}`); - 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(`Earlier date found: ${latestDate}.`) document.getElementById("appointments_consulate_appointment_date").value = latestDate; document.getElementById("appointments_consulate_appointment_time").innerHTML = "" @@ -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(`No time slots found on date ${latestDate}.
Checked @ ${nowInLocale}
Your current appointment is on ${_date}`); - 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(`No time slots found on date ${latestAscDate}.
Checked @ ${nowInLocale}
Your current appointment is on ${_date}`); - 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 = ""; - document.getElementById("appointments_asc_appointment_time").value = $latestAscTime; + // document.getElementById("appointments_asc_appointment_time").innerHTML = ""; + // 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) { diff --git a/manifest.json b/manifest.json index 103028f..acd7cc3 100644 --- a/manifest.json +++ b/manifest.json @@ -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" }