From fdadc274539f1502ef9d8342f186be4f2641a515 Mon Sep 17 00:00:00 2001 From: Krzysztof Modras Date: Wed, 11 Oct 2023 14:40:06 +0200 Subject: [PATCH] Adblocker: Do not report requests from previous page --- modules/adblocker/sources/adblocker.es | 6 ++-- modules/adblocker/sources/statistics.es | 38 ++++++++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/modules/adblocker/sources/adblocker.es b/modules/adblocker/sources/adblocker.es index 994dcf5..cf7cb6a 100644 --- a/modules/adblocker/sources/adblocker.es +++ b/modules/adblocker/sources/adblocker.es @@ -290,13 +290,15 @@ export default class Adblocker { * engine decides. */ onBeforeRequest(context, response) { + if (context.frameId === 0 && context.isMainFrame) { + this.stats.addNewPage(context); + } + if (this.shouldProcessRequest(context, response) === false) { return false; } if (context.isMainFrame) { - this.stats.addNewPage(context); - // HTML filtering is a feature from the adblocker which is able to // intercept streaming responses from main documents before they are // parsed by the browser. This means that we have a chance to remove diff --git a/modules/adblocker/sources/statistics.es b/modules/adblocker/sources/statistics.es index e6ff53a..ad9aa12 100644 --- a/modules/adblocker/sources/statistics.es +++ b/modules/adblocker/sources/statistics.es @@ -12,6 +12,8 @@ import { nextTick } from '../core/decorators'; import TrackerCounter from '../core/helpers/tracker-counter'; import events from '../core/events'; import pacemaker from '../core/services/pacemaker'; +import { parse } from '../core/url'; +import { browser } from '../platform/globals'; const FIRSTPARTY = 'First party'; @@ -20,6 +22,7 @@ class PageStats { this.tabUrl = context.tabUrl; this.hostGD = context.tabUrlParts.generalDomain; this.blockedRequests = []; + this.createdAt = Date.now(); // Lazily computed once a report is requested this._counter = new TrackerCounter(); @@ -138,30 +141,51 @@ export default class AdbStats { } init() { + browser.webNavigation.onBeforeNavigate.addListener(this.onBeforeNavigate); this.clearInterval = pacemaker.everyFewMinutes(() => { this.clearStats(); }); } unload() { + browser.webNavigation.onBeforeNavigate.removeListener(this.onBeforeNavigate); pacemaker.clearTimeout(this.clearInterval); } + onBeforeNavigate = (event) => { + if (event.frameId !== 0 || event.frameType === 'outermost_frame') { + return; + } + const urlParts = parse(event.url); + this.addNewPage({ + tabId: event.tabId, + tabUrl: event.url, + tabUrlParts: urlParts, + }); + }; + addBlockedUrl(context) { - // Check if we already have a context for this tab let page = this.tabs.get(context.tabId); - if (page === undefined) { - page = this.addNewPage(context); - } - // If it's a new url in an existing tab - if (context.tabUrl !== page.tabUrl) { + if (!page) { page = this.addNewPage(context); } - page.addBlockedUrl(context); + // ignore requests that may related to previous page + if (context.tabUrl === page.tabUrl) { + page.addBlockedUrl(context); + } } addNewPage(context) { const existingPage = this.tabs.get(context.tabId); + + if ( + existingPage + && existingPage.tabUrl === context.tabUrl + && existingPage.createdAt + 200 > Date.now() + ) { + return existingPage; + } + if (existingPage !== undefined) { // Emit summary of stats from previous page nextTick(() => {