From e626ff5eaee316c1e11f78edb98208343c9fa8cf Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Mon, 11 Mar 2024 18:37:44 +0000 Subject: [PATCH] feat(extension): support fetching codes from checkout.theshrine.net via proxy --- extension/src/config-page.js | 22 +++++++++------- extension/src/config.html | 7 ++++++ extension/src/popup.js | 49 +++++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/extension/src/config-page.js b/extension/src/config-page.js index 8fa1796..d9caec4 100644 --- a/extension/src/config-page.js +++ b/extension/src/config-page.js @@ -1,12 +1,16 @@ (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, + function initCheckbox(selector, configName) { + const checkbox = document.querySelector(selector); + chrome.storage.sync.get(configName, (data) => { + checkbox.checked = data[configName]; }); - }); - rejectCheckbox.disabled = false; + checkbox.addEventListener('change', async function() { + conf = {} + conf[configName] = checkbox.checked; + await chrome.storage.sync.set(conf); + }); + checkbox.disabled = false; + } + initCheckbox('#enable-reject', 'enable_reject'); + initCheckbox('#enable-shrine', 'enable_shrine'); })(); diff --git a/extension/src/config.html b/extension/src/config.html index 043366a..7e37ec7 100644 --- a/extension/src/config.html +++ b/extension/src/config.html @@ -16,6 +16,13 @@ Enable fetching codes from rejectdopamine.com (3rd party service) + +
+ + diff --git a/extension/src/popup.js b/extension/src/popup.js index 172967a..a163612 100644 --- a/extension/src/popup.js +++ b/extension/src/popup.js @@ -1,6 +1,26 @@ -chrome.storage.sync.get(['enable_reject'], async function(config) { +chrome.storage.sync.get([ + 'enable_reject', + 'enable_shrine' +], async function(config) { const root = document.querySelector('#root'); + function mergeCodes(activity, extraActivity) { + 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); + } + } + + } + function codeTable(codes) { const table = document.createElement('table'); table.innerHTML = 'CodeScore'; @@ -113,19 +133,20 @@ chrome.storage.sync.get(['enable_reject'], async function(config) { 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); - } - } + mergeCodes(activity, extraActivity); + } + } + + if (config.enable_shrine) { + const shrineRes = await fetch(SERVER_URL + '/3p-proxy/shrine-checkout?' + query).catch((e) => { + message(`Something went wrong: ${e}`); + console.error(e); + }); + if (!shrineRes.ok) { + message('Warning: failed to fetch codes from checkout.theshrine.net'); + } else { + const extraActivity = await shrineRes.json(); + mergeCodes(activity, extraActivity); } }