Skip to content

Commit

Permalink
background reccursive job through chrome.alarms; dark mode; tracking …
Browse files Browse the repository at this point in the history
…control; automated browser alert and sound notification when slots found
  • Loading branch information
somangshu committed May 9, 2021
1 parent ce2fcaf commit 0dd8c69
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 37 deletions.
96 changes: 93 additions & 3 deletions dist/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,93 @@
chrome.alarms.onAlarm.addListener(function(alarm) {
alert('beep')
});
const api = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByPin";

const getToday = () => {
let today = new Date();
let dd = String(today.getDate()).padStart(2, "0");
let mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0!
let yyyy = today.getFullYear();

today = dd + "-" + mm + "-" + yyyy;
return today;
};

// searching slots by pin code
// this function exists in the popup script too
// need to find a way to make this modular
// not able to find backgound using popup function
const searchForStock = (pin) => {
const today = getToday();
const params = "?pincode=" + pin + "&date=" + today;
try {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) {
let responseText = xhr.response;
let response = JSON.parse(responseText);
response.centers.forEach((center) => {
center.sessions.forEach((session) => {
if (session.min_age_limit == 18 && session.available_capacity > 0) {
// alert sound
var myAudio = new Audio(chrome.runtime.getURL("/notification.mp3"));
myAudio.play();

chrome.storage.sync.set({ last_found_slot: { center: center, session: session } });

// alert and make a sound
if (
window.confirm(
"A new slot was found!" +
"\n" +
center.name +
" (" +
session.available_capacity +
") " +
" - on " +
session.date +
" - " +
session.vaccine +
" (" +
center.fee_type +
")" +
"\n" +
"Click ok to open covin login page"
)
) {
chrome.browserAction.onClicked.addListener(function (activeTab) {
var newURL = "https://selfregistration.cowin.gov.in/";
chrome.tabs.create({ url: newURL });
});
}
}
});
});
}
};
xhr.open("GET", api + params, true);
xhr.send(null);
chrome.storage.sync.set(
{ last_run: "No slots found yet. Last run on: " + new Date() },
function () {
console.log("Last run saved");
}
);
} catch (error) {
console.log(error);
chrome.storage.sync.set(
{
last_run:
"There was an error in the last check. No slots found yet. Last run on: " + new Date(),
},
function () {
console.log("Last run saved");
}
);
}
};

chrome.alarms.onAlarm.addListener(function (alarm) {
chrome.storage.sync.get(["pin_code"], function (items) {
if (items) {
if (items.pin_code) searchForStock(items.pin_code);
}
});
});
7 changes: 7 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@
<div class="data"></div>
<div class="result-container">
<p><span class="cases"></span></p>
<p class="bd-top"><span class="tracking"><i>Tracking in progress</i></span></p>
<p><span class="cases noSlots"></span></p>
<p style="text-decoration: underline;">Last found slots</p>
<p>
<strong>Available at: </strong> <span class="recovered"></span>
</p>
<p><strong>Slots: </strong><span class="deaths"></span></p>
</div>
</div>

<div class="footer-controls">
<div class="stop-tracking">Stop tracking</div>
<div class="dark-control">Dark mode: off</div>
</div>

<script src="main.js"></script>
</body>
</html>
Loading

0 comments on commit 0dd8c69

Please sign in to comment.