Skip to content

Commit

Permalink
fix: Use full hostname & unify pause logic (#2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban authored Jan 8, 2025
1 parent 2a00e75 commit 84ec1c5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
10 changes: 6 additions & 4 deletions src/background/adblocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/background/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down
8 changes: 2 additions & 6 deletions src/pages/panel/views/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -130,7 +126,7 @@ export default {
layout="row gap:2px items:center"
>
<ui-text type="label-m"
>${tail(stats.hostname)}</ui-text
>${stats.displayHostname}</ui-text
>
${!options.managed &&
html`<ui-icon
Expand All @@ -142,7 +138,7 @@ export default {
</ui-action>
</panel-managed>
`
: tail(stats.hostname))}
: stats.displayHostname)}
<ui-action slot="icon">
<a href="https://www.ghostery.com" onclick="${openTabWithUrl}">
<ui-icon name="logo"></ui-icon>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/store/hostname.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
}
return {
...model,
value: parsed.hostname.replace(/^www\./, ''),
value: parsed.hostname,
};
},
},
Expand Down
8 changes: 5 additions & 3 deletions src/store/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
10 changes: 6 additions & 4 deletions src/store/tab-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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' &&
Expand Down

0 comments on commit 84ec1c5

Please sign in to comment.