From 753f7ce9e95962a2659a6d3687d0522112ae1ba7 Mon Sep 17 00:00:00 2001 From: dominik-korsa <29484605+dominik-korsa@users.noreply.github.com> Date: Sun, 27 Oct 2024 22:55:44 +0100 Subject: [PATCH 1/2] Migrate to Manifest V3 --- ext/js/background.js | 86 +++++++++++++++++++++++++------------------- ext/js/general.js | 6 ++-- ext/manifest.json | 21 ++++++----- 3 files changed, 66 insertions(+), 47 deletions(-) diff --git a/ext/js/background.js b/ext/js/background.js index cd56dd4..3b89f9c 100644 --- a/ext/js/background.js +++ b/ext/js/background.js @@ -30,20 +30,21 @@ * variable. * @see setUpLastContestRedirect */ - function retrieveLastContestID() { - storage.get('lastContestID').then((response) => { - lastContestID = response.lastContestID; - }); + async function retrieveLastContestID() { + const response = await storage.get('lastContestID'); + lastContestID = response.lastContestID; } /** - * Save last contest ID both in Storage and our local variable. + * Save last contest ID both in Storage and our local variable + * and update redirect rules. * @param {string} contestID last contest ID * @see setUpLastContestRedirect */ function saveLastContestID(contestID) { storage.set({ lastContestID: contestID }); lastContestID = contestID; + updateLastContestRedirectRules(); } /** @@ -114,13 +115,48 @@ } /** - * Add an onBeforeRequest listener that redirects to the last contest + * Setups a declarative net request rules to redirect to the last contest * if the user just entered Satori webpage. + */ + function updateLastContestRedirectRules() { + const addRules = []; + if (typeof lastContestID !== 'undefined' && lastContestID !== null) { + addRules.push( + ...[ + { id: 1, urlFilter: '||satori.tcs.uj.edu.pl/|' }, + { id: 2, urlFilter: '||satori.tcs.uj.edu.pl/news' }, + ].map(({ id, urlFilter }) => ({ + id, + condition: { + urlFilter, + // Redirect only if the user just opened Satori on a given tab + excludedTabIds: [...satoriTabs.values()], + resourceTypes: ['main_frame'], + }, + action: { + type: 'redirect', + redirect: { + url: SATORI_URL_CONTEST + lastContestID + '/', + }, + }, + })) + ); + } + browser.declarativeNetRequest.updateSessionRules({ + removeRuleIds: [1, 2], + addRules, + }); + } + + /** + * Enables the last contest redirect. * * Also, the function adds webNavigation.onCommitted and tabs.onRemoved * listeners to keep the list of Satori tabs. */ function setUpLastContestRedirect() { + updateLastContestRedirectRules(); + // Store which tabs have Satori open browser.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if ( @@ -132,36 +168,13 @@ } else { satoriTabs.delete(tabId); } + updateLastContestRedirectRules(); }); - browser.tabs.onRemoved.addListener((tabId) => satoriTabs.delete(tabId)); - - browser.webRequest.onBeforeRequest.addListener( - function (details) { - if ( - typeof lastContestID === 'undefined' || - lastContestID === null || - satoriTabs.has(details.tabId) - ) { - // If we haven't saved any contest yet, then do nothing - // Also, don't redirect if the user is already browsing - // Satori - return; - } - - return { - redirectUrl: SATORI_URL_CONTEST + lastContestID + '/', - }; - }, - { - urls: [ - '*://satori.tcs.uj.edu.pl/', - '*://satori.tcs.uj.edu.pl/news', - ], - types: ['main_frame'], - }, - ['blocking'], - ); + browser.tabs.onRemoved.addListener((tabId) => { + satoriTabs.delete(tabId); + updateLastContestRedirectRules(); + }); } /** @@ -298,8 +311,9 @@ return (await storage.get('contestList')).contestList ?? []; } - retrieveLastContestID(); - setUpLastContestRedirect(); + retrieveLastContestID().then(() => { + setUpLastContestRedirect(); + }); setUpSessionCookies(); browser.runtime.onMessage.addListener((request, sender) => { diff --git a/ext/js/general.js b/ext/js/general.js index 16a5f0f..75efb05 100644 --- a/ext/js/general.js +++ b/ext/js/general.js @@ -3,9 +3,9 @@ browser.runtime.sendMessage({ action: 'enablePageAction' }); - const BANNER_URL = browser.extension.getURL('images/satori_banner.png'); - const TCS_LOGO_URL = browser.extension.getURL('images/tcslogo.svg'); - const ALT_TCS_LOGO_URL = browser.extension.getURL('images/alttcslogo.png'); + const BANNER_URL = browser.runtime.getURL('images/satori_banner.png'); + const TCS_LOGO_URL = browser.runtime.getURL('images/tcslogo.svg'); + const ALT_TCS_LOGO_URL = browser.runtime.getURL('images/alttcslogo.png'); let storage = browser.storage.sync || browser.storage.local; diff --git a/ext/manifest.json b/ext/manifest.json index 76d490b..412973d 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,5 +1,5 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "Satori Enhancements", "description": "Adds a few useful enhancements to Satori Online Judge website.", @@ -21,17 +21,17 @@ }, "options_ui": { - "page": "options.html", - "chrome_style": true, - "browser_style": true + "page": "options.html" }, "permissions": [ "storage", "notifications", - "webRequest", - "webRequestBlocking", - "cookies", + "declarativeNetRequestWithHostAccess", + "cookies" + ], + + "host_permissions": [ "*://satori.tcs.uj.edu.pl/*" ], @@ -50,7 +50,12 @@ ] }, - "web_accessible_resources": ["images/*.png", "images/*.svg"], + "web_accessible_resources": [ + { + "resources": ["images/*.png", "images/*.svg"], + "matches": ["*://satori.tcs.uj.edu.pl/*"] + } + ], "content_scripts": [ { From 5c5760c7c556f23af6cbb4ecc058a416f5ff2894 Mon Sep 17 00:00:00 2001 From: dominik-korsa <29484605+dominik-korsa@users.noreply.github.com> Date: Sun, 27 Oct 2024 22:57:53 +0100 Subject: [PATCH 2/2] Fix annoying typo in comment --- ext/js/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/js/background.js b/ext/js/background.js index 3b89f9c..f647453 100644 --- a/ext/js/background.js +++ b/ext/js/background.js @@ -115,7 +115,7 @@ } /** - * Setups a declarative net request rules to redirect to the last contest + * Sets up a declarative net request rules to redirect to the last contest * if the user just entered Satori webpage. */ function updateLastContestRedirectRules() {