From e020bcebec52a1b5fb9ef313d8c1562a2e13ebc3 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Fri, 16 Feb 2024 12:42:44 +0000 Subject: [PATCH] feat(extension): initial commit --- extension/manifest.json | 23 +++++++++++++++++++ extension/src/background.js | 46 +++++++++++++++++++++++++++++++++++++ extension/src/content.js | 1 + 3 files changed, 70 insertions(+) create mode 100644 extension/manifest.json create mode 100644 extension/src/background.js create mode 100644 extension/src/content.js diff --git a/extension/manifest.json b/extension/manifest.json new file mode 100644 index 0000000..c94a505 --- /dev/null +++ b/extension/manifest.json @@ -0,0 +1,23 @@ + +{ + "manifest_version":2, + "name": "aci", + "version": "0.0.1", + "description": "aci", + "content_scripts":[ + { + "matches": ["*://checkin.york.ac.uk/selfregistration"], + "js": ["src/content.js"] + } + ], + "permissions": [ + "webRequest", + "*://aci-api.ashhhleyyy.dev/*", + "*://checkin.york.ac.uk/*" + ], + "background": { + "scripts": ["src/background.js"], + "persistent": false, + "type": "module" + } +} diff --git a/extension/src/background.js b/extension/src/background.js new file mode 100644 index 0000000..1fd82da --- /dev/null +++ b/extension/src/background.js @@ -0,0 +1,46 @@ +console.log('i am a background script'); + +const codeCache = new Map(); + +function extractActivityId(url) { + const m = url.match(/^https:\/\/checkin\.york\.ac\.uk\/api\/selfregistration\/([0-9]+)\/present$/m); + return m[1]; +} + +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); + return; + } + const code = codeCache.get(activityId); + codeCache.delete(activityId); + console.log('request for activity', activityId, 'the code', code, 'was', codeCorrect ? 'correct' : 'incorrect'); +} + +function interceptCode(details) { + const activityId = extractActivityId(details.url); + const body = details.requestBody; + if (body.error) { + console.error('failed to obtain request body', body.error); + return; + } + if (!body.formData) { + console.error('request body did not have form data'); + return; + } + if (!body.formData['code']) { + console.error('request did not contain a code'); + return; + } + const code = body.formData['code'][0]; + codeCache.set(activityId, code); +} + +browser.webRequest.onBeforeRequest.addListener(interceptCode, { + urls: ['https://checkin.york.ac.uk/api/selfregistration/*/present'], +}, ['requestBody']); +browser.webRequest.onCompleted.addListener(interceptSubmit, { + urls: ['https://checkin.york.ac.uk/api/selfregistration/*/present'], +}); diff --git a/extension/src/content.js b/extension/src/content.js new file mode 100644 index 0000000..e921523 --- /dev/null +++ b/extension/src/content.js @@ -0,0 +1 @@ +console.log('hello');