From 0bee055a147ba089f149ea387f10af3aebfeb3a3 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Fri, 8 Mar 2024 17:13:55 +0000 Subject: [PATCH] feat(extension): support fetching codes from reject via proxy --- extension/manifest.json | 4 ++++ extension/src/config-page.js | 12 ++++++++++++ extension/src/config.html | 22 ++++++++++++++++++++++ extension/src/config.js | 4 ++-- extension/src/popup.js | 33 ++++++++++++++++++++++++++++++--- 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 extension/src/config-page.js create mode 100644 extension/src/config.html diff --git a/extension/manifest.json b/extension/manifest.json index bcdd624..bcb2c41 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -11,6 +11,7 @@ } ], "permissions": [ + "storage", "webRequest", "*://aci-api.ashhhleyyy.dev/*", "*://checkin.york.ac.uk/*" @@ -22,6 +23,9 @@ "browser_action": { "default_popup": "src/popup.html" }, + "options_ui": { + "page": "src/config.html" + }, "browser_specific_settings": { "gecko": { "update_url": "https://git.ashhhleyyy.dev/ash/aci/raw/branch/main/addon_updates.json", diff --git a/extension/src/config-page.js b/extension/src/config-page.js new file mode 100644 index 0000000..8fa1796 --- /dev/null +++ b/extension/src/config-page.js @@ -0,0 +1,12 @@ +(async function() { + const rejectCheckbox = document.querySelector('#enable-reject'); + chrome.storage.sync.get('enable_reject', (data) => { + rejectCheckbox.checked = data.enable_reject; + }); + rejectCheckbox.addEventListener('change', async function() { + await chrome.storage.sync.set({ + enable_reject: rejectCheckbox.checked, + }); + }); + rejectCheckbox.disabled = false; +})(); diff --git a/extension/src/config.html b/extension/src/config.html new file mode 100644 index 0000000..043366a --- /dev/null +++ b/extension/src/config.html @@ -0,0 +1,22 @@ + + + + + + Checkin Codes + + + + + + +
+
+ +
+
+ + diff --git a/extension/src/config.js b/extension/src/config.js index 54469e2..ca7ffa8 100644 --- a/extension/src/config.js +++ b/extension/src/config.js @@ -1,2 +1,2 @@ -this.SERVER_URL = 'https://aci-api.ashhhleyyy.dev'; -//this.SERVER_URL = 'http://localhost:8000'; +//this.SERVER_URL = 'https://aci-api.ashhhleyyy.dev'; +this.SERVER_URL = 'http://localhost:8000'; diff --git a/extension/src/popup.js b/extension/src/popup.js index 89df7d6..172967a 100644 --- a/extension/src/popup.js +++ b/extension/src/popup.js @@ -1,4 +1,4 @@ -(async function() { +chrome.storage.sync.get(['enable_reject'], async function(config) { const root = document.querySelector('#root'); function codeTable(codes) { @@ -97,12 +97,39 @@ message(`Something went wrong: ${e}`); console.error(e); }); - removeLoading(); if (!res.ok) { + removeLoading(); message('Server returned error code ' + res.status + ': ' + res.statusText, true); return; } const activity = await res.json(); + + if (config.enable_reject) { + const rejectRes = await fetch(SERVER_URL + '/3p-proxy/reject?' + query).catch((e) => { + message(`Something went wrong: ${e}`); + console.error(e); + }); + if (!rejectRes.ok) { + message('Warning: failed to fetch codes from reject'); + } else { + const extraActivity = await rejectRes.json(); + for (const code of extraActivity.codes) { + let found = false; + for (const code2 of activity.codes) { + if (code.code === code2.code) { + code2.score += code.score; + found = true; + break; + } + } + if (!found) { + activity.codes.push(code); + } + } + } + } + + removeLoading(); if (activity.codes.length === 0) { message('No codes are available right now :/'); } else { @@ -133,4 +160,4 @@ } } } -})(); +});