-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
background reccursive job through chrome.alarms; dark mode; tracking …
…control; automated browser alert and sound notification when slots found
- Loading branch information
Showing
6 changed files
with
335 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.