From 4479fd783e4f6e64815ac79f66ea28f8a466c967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Luba=C5=84ski?= Date: Tue, 31 Dec 2024 14:34:25 +0100 Subject: [PATCH] fix: Use full hostname & unify pause logic --- src/background/adblocker.js | 10 ++++++---- src/background/stats.js | 4 ++-- src/pages/panel/views/main.js | 8 ++------ src/pages/settings/store/hostname.js | 2 +- src/store/options.js | 8 +++++--- src/store/tab-stats.js | 10 ++++++---- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/background/adblocker.js b/src/background/adblocker.js index ed0c36acf..89694fb8c 100644 --- a/src/background/adblocker.js +++ b/src/background/adblocker.js @@ -357,15 +357,17 @@ function isTrusted(request, type) { const exception = getException(metadata?.id || request.hostname); if (exception) { - const tabHostname = request.sourceHostname.replace(/^www\./, ''); - // The request is trusted if: // - tracker is blocked, but tab hostname is added to trusted domains // - tracker is not blocked and tab hostname is not found in the blocked domains if ( exception.blocked - ? exception.trustedDomains.includes(tabHostname) - : !exception.blockedDomains.includes(tabHostname) + ? exception.trustedDomains.some((id) => + request.sourceHostname.endsWith(id), + ) + : !exception.blockedDomains.some((id) => + request.sourceHostname.endsWith(id.sourceHostname), + ) ) { return true; } diff --git a/src/background/stats.js b/src/background/stats.js index 7f66360f6..78029e812 100644 --- a/src/background/stats.js +++ b/src/background/stats.js @@ -314,7 +314,7 @@ function setupTabStats(details) { if (request.isHttp || request.isHttps) { tabStats.set(details.tabId, { - hostname: request.hostname.replace('www.', ''), + hostname: request.hostname, url: request.url, trackers: [], timestamp: details.timeStamp, @@ -327,7 +327,7 @@ function setupTabStats(details) { } // Setup stats for the tab when a user navigates to a new page -chrome.webNavigation.onBeforeNavigate.addListener((details) => { +chrome.webNavigation.onCommitted.addListener((details) => { if (details.tabId > -1 && details.parentFrameId === -1) { setupTabStats(details); } diff --git a/src/pages/panel/views/main.js b/src/pages/panel/views/main.js index cc1961fe6..87530daf1 100644 --- a/src/pages/panel/views/main.js +++ b/src/pages/panel/views/main.js @@ -96,10 +96,6 @@ function setStatsType(host, event) { store.set(host.options, { panel: { statsType: type } }); } -function tail(hostname) { - return hostname.length > 24 ? '...' + hostname.slice(-24) : hostname; -} - export default { [router.connect]: { stack: [Menu, TrackerDetails, ProtectionStatus] }, options: store(Options), @@ -130,7 +126,7 @@ export default { layout="row gap:2px items:center" > ${tail(stats.hostname)}${stats.displayHostname} ${!options.managed && html` ` - : tail(stats.hostname))} + : stats.displayHostname)} diff --git a/src/pages/settings/store/hostname.js b/src/pages/settings/store/hostname.js index 5c15dc3b0..a396bfc43 100644 --- a/src/pages/settings/store/hostname.js +++ b/src/pages/settings/store/hostname.js @@ -23,7 +23,7 @@ export default { } return { ...model, - value: parsed.hostname.replace(/^www\./, ''), + value: parsed.hostname, }; }, }, diff --git a/src/store/options.js b/src/store/options.js index 651f5e747..3b667b893 100644 --- a/src/store/options.js +++ b/src/store/options.js @@ -231,9 +231,11 @@ async function manage(options) { return options; } -export function isPaused(options, domain = '') { +export function isPaused(options, hostname = '') { + if (options.paused[GLOBAL_PAUSE_ID]) return true; + return ( - !!options.paused[GLOBAL_PAUSE_ID] || - (domain && !!options.paused[domain.replace(/^www\./, '')]) + !!hostname && + Object.keys(options.paused).some((id) => hostname.endsWith(id)) ); } diff --git a/src/store/tab-stats.js b/src/store/tab-stats.js index c7de9a95a..14c7dccff 100644 --- a/src/store/tab-stats.js +++ b/src/store/tab-stats.js @@ -35,13 +35,16 @@ let tab = undefined; const Stats = { hostname: '', trackers: [StatsTracker], + + displayHostname: ({ hostname }) => { + hostname = hostname.replace(/^www\./, ''); + return hostname.length > 24 ? '...' + hostname.slice(-24) : hostname; + }, trackersBlocked: ({ trackers }) => trackers.reduce((acc, { blocked }) => acc + Number(blocked), 0), trackersModified: ({ trackers }) => trackers.reduce((acc, { modified }) => acc + Number(modified), 0), - categories: ({ trackers }) => trackers.map((t) => t.category), - topCategories: ({ categories }) => { const counts = Object.entries( categories.reduce((acc, category) => { @@ -79,8 +82,7 @@ const Stats = { return tabStats; } - const hostname = parse(tab.url).hostname?.replace(/^www\./, ''); - return { hostname }; + return { hostname: parse(tab.url).hostname }; }, observe: __PLATFORM__ === 'safari' &&