Skip to content

Commit

Permalink
AG-27583 fix multiple injection to the pages
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-27583-1 to master

Squashed commit of the following:

commit c93c6f5
Author: Maxim Topciu <[email protected]>
Date:   Tue Nov 19 20:10:08 2024 +0200

    AG-27583 set cosmeticsInjectedOnStartup after all pages processed

commit 22745ff
Author: Maxim Topciu <[email protected]>
Date:   Tue Nov 19 19:09:48 2024 +0200

    AG-27583 remove package tgz

commit 806ddf5
Author: Maxim Topciu <[email protected]>
Date:   Tue Nov 19 17:49:13 2024 +0200

    AG-27583 fix tests

commit 28a37b7
Author: Maxim Topciu <[email protected]>
Date:   Tue Nov 19 17:33:22 2024 +0200

    AG-27583 update changelog

commit 11f9175
Author: Maxim Topciu <[email protected]>
Date:   Tue Nov 19 17:31:44 2024 +0200

    AG-27583 do not inject styles to the open tabs on every service worker restart

commit 78208ad
Author: Maxim Topciu <[email protected]>
Date:   Tue Nov 19 18:14:45 2024 +0300

    AG-27583 fix multiple injection to the pages

    Merge in ADGUARD-FILTERS/tsurlfilter from fix/AG-27583 to release/v3.1

    Squashed commit of the following:

    commit 9ff2244
    Merge: 7acc3cb 8b099ef
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 17:13:32 2024 +0200

        Merge branch 'release/v3.1' into fix/AG-27583

    commit 7acc3cb
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 16:37:10 2024 +0200

        AG-27583 update tswebextension.tgz

    commit 7c0e941
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 16:33:45 2024 +0200

        AG-27583 make code more secure

    commit 0507368
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 13:59:28 2024 +0200

        AG-27583 improve documentation

    commit 9b7d12e
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 13:55:25 2024 +0200

        AG-27583 add todo comment

    commit 3716839
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 12:50:59 2024 +0200

        AG-27583 add note to changelog

    commit a388590
    Author: Atlassian Bamboo <[email protected]>
    Date:   Tue Nov 19 12:41:34 2024 +0300

        skipci: Automatic update tsurlfilter and tswebextension tgz files

    commit 8bfd6e4
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 11:31:02 2024 +0200

        AG-27583 add package for testing

    commit d15e142
    Merge: 27664ce 3b874cb
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 11:30:33 2024 +0200

        Merge branch 'release/v3.1' into fix/AG-27583

    commit 27664ce
    Author: Maxim Topciu <[email protected]>
    Date:   Tue Nov 19 11:28:05 2024 +0200

        AG-27583 fix multiple injection to the pages for mv2

    commit 8cf7d96
    Author: Maxim Topciu <[email protected]>
    Date:   Mon Nov 18 21:15:13 2024 +0200

        AG-27583 fix multiple injection to the pages
  • Loading branch information
maximtop committed Nov 20, 2024
1 parent 46ecda6 commit 843de7d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/tswebextension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: manually add compare links for version changes -->
<!-- e.g. [0.1.2]: https://github.com/AdguardTeam/tsurlfilter/compare/tswebextension-v0.1.1...tswebextension-v0.1.2 -->

## [2.0.7] - 2024-11-19

### Fixed

- Memory leak caused by multiple script injections on the same pages after a service worker or event page restart [AdguardBrowserExtension#2594].

[AdguardBrowserExtension#2594]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2594

[2.0.7]: https://github.com/AdguardTeam/tsurlfilter/releases/tag/tswebextension-v2.0.7

## [2.0.6] - 2024-11-19

- Updated `@adguard/tsurlfilter` to `v3.0.7`.
Expand Down
6 changes: 6 additions & 0 deletions packages/tswebextension/src/lib/mv2/background/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export class AppContext {
*/
@sessionDecorator(SessionStorageKey.StartTimeMs)
accessor startTimeMs: number | undefined;

/**
* Flag that indicates that cosmetics were injected on startup.
*/
@sessionDecorator(SessionStorageKey.CosmeticsInjectedOnStartup)
accessor cosmeticsInjectedOnStartup!: boolean;
}

export const appContext = new AppContext();
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export const enum SessionStorageKey {
IsAppStarted = 'isAppStarted',
Configuration = 'configuration',
StartTimeMs = 'startTimeMs',
CosmeticsInjectedOnStartup = 'cosmeticsInjectedOnStartup',
}

export type SessionStorageSchema = {
[SessionStorageKey.IsAppStarted]: boolean,
[SessionStorageKey.Configuration]: ConfigurationMV2Context | undefined,
[SessionStorageKey.StartTimeMs]: number | undefined,
[SessionStorageKey.CosmeticsInjectedOnStartup]: boolean,
};

/**
Expand All @@ -25,6 +27,7 @@ export class ExtSessionStorage extends ExtensionStorage<SessionStorageSchema> {
isAppStarted: false,
configuration: undefined,
startTimeMs: undefined,
cosmeticsInjectedOnStartup: false,
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TabContext } from './tab-context';
import type { EngineApi } from '../engine-api';
import type { DocumentApi } from '../document-api';
import type { TabsApi } from './tabs-api';
import { appContext } from '../context';

/**
* Injects cosmetic rules into tabs, opened before app initialization.
Expand Down Expand Up @@ -45,6 +46,8 @@ export class TabsCosmeticInjector {
logger.error(promise.reason);
}
});

appContext.cosmeticsInjectedOnStartup = true;
}

/**
Expand Down Expand Up @@ -99,6 +102,18 @@ export class TabsCosmeticInjector {
return;
}

// TODO: Instead of this, it’s better to use the runtime.onStartup and runtime.onInstalled
// events to inject cosmetics once during the extension's initialization
// and browser startup without flags.
// However, this would require big refactoring of the extension.
/**
* This condition prevents applying cosmetic rules to the tab multiple times.
* Applying them once after the extension's initialization is sufficient.
*/
if (appContext.cosmeticsInjectedOnStartup) {
return;
}

const cosmeticOption = frame.matchingResult.getCosmeticOption();

frame.cosmeticResult = this.engineApi.getCosmeticResult(frameUrl, cosmeticOption);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export class MockAppContext implements AppContext {
configuration: ConfigurationMV2Context | undefined = undefined;

startTimeMs: number | undefined = undefined;

cosmeticsInjectedOnStartup: boolean = false;
}

0 comments on commit 843de7d

Please sign in to comment.