feat(extension): capture activity information alongside code

This commit is contained in:
Ashhhleyyy 2024-02-16 13:01:53 +00:00
parent 7377e1bb95
commit fe70b5692d
Signed by: ash
GPG key ID: 83B789081A0878FB

View file

@ -1,6 +1,22 @@
console.log('i am a background script');
const codeCache = new Map();
const datastore = new Map();
function attachActivityDetails(activityId, date, time, name) {
if (datastore.has(activityId)) {
const activity = datastore.get(activityId);
activity.name = name;
activity.date = date;
activity.time = time;
} else {
datastore.set(activityId, {
date,
time,
name,
code: null,
});
}
}
function extractActivityId(url) {
const m = url.match(/^https:\/\/checkin\.york\.ac\.uk\/api\/selfregistration\/([0-9]+)\/present$/m);
@ -10,13 +26,18 @@ function extractActivityId(url) {
function interceptSubmit(details) {
const activityId = extractActivityId(details.url);
const codeCorrect = details.statusCode >= 200 && details.statusCode <= 299;
if (!codeCache.has(activityId)) {
console.error('we did not manage to log a code for activity', activityId);
if (!datastore.has(activityId)) {
console.error('we do not have information about the activity', activityId);
return;
}
const code = codeCache.get(activityId);
codeCache.delete(activityId);
const activity = datastore.get(activityId);
if (!activity.date || !activity.time || !activity.name || !activity.code) {
console.error('we do not have all the information about the activity', activityId);
return;
}
console.log('request for activity', activityId, 'the code', code, 'was', codeCorrect ? 'correct' : 'incorrect');
// TODO: Send data to server.
}
function interceptCode(details) {
@ -35,7 +56,16 @@ function interceptCode(details) {
return;
}
const code = body.formData['code'][0];
codeCache.set(activityId, code);
if (datastore.has(activityId)) {
datastore.get(activityId).code = code;
} else {
datastore.set(activityId, {
date: null,
time: null,
name: null,
code,
});
}
}
browser.webRequest.onBeforeRequest.addListener(interceptCode, {