From 3d6c256bc5ed8377b3156a8528da3abbf39cbe22 Mon Sep 17 00:00:00 2001 From: Slava Leleka Date: Thu, 30 Nov 2023 15:03:34 +0300 Subject: [PATCH] add settings.debugScriptlets. AG-27413 https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2584 Squashed commit of the following: commit d32feec305c04b6c43668eae283b27d4d85615b8 Author: Slava Leleka Date: Thu Nov 30 12:55:45 2023 +0200 no extra settings.debugScriptlets checking commit d5871d3e7dce519f44edeb7864777fe687a33196 Author: Vladimir Zhelvis Date: Thu Nov 30 13:46:22 2023 +0300 add default value for debugScriptlets by zod commit b949ebe0d6f4c7073310a2f15162bcae1e28b72a Author: Slava Leleka Date: Wed Nov 29 19:54:17 2023 +0200 fix changelog commit 12ca92664483b0acf55435ac3e3529f20ab00c21 Author: Slava Leleka Date: Wed Nov 29 14:26:14 2023 +0200 fix examples packages commit 8690c50b6672c40646055234cc0157dc83b5831b Merge: 4a92c61a 8d02bf83 Author: Slava Leleka Date: Wed Nov 29 14:24:56 2023 +0200 merge master into the branch, resolve conflicts commit 4a92c61aefb67f6b22d8713f62209e081eaa5536 Author: Slava Leleka Date: Tue Nov 28 12:26:34 2023 +0200 fix readme commit 42a8fb9c06ff3e7ef4971f9d23dc85378b520abc Author: Slava Leleka Date: Tue Nov 28 12:25:15 2023 +0200 update changelog commit f9a234837e4aa832b91d915847c7171359e80660 Author: Slava Leleka Date: Mon Nov 27 15:36:49 2023 +0200 fix naming commit cc3239841b9cd231ad3ffdbdfa7e2c01bb1794c0 Author: Slava Leleka Date: Fri Nov 24 13:23:11 2023 +0200 fix changelog commit 24a8bdae1b23876f9b52a941697ba9074dca17b7 Author: Slava Leleka Date: Fri Nov 24 13:16:34 2023 +0200 add settings.verboseScriptletsLogging commit 229e6b5b59672766c61396f8c76dc2097c96551c Author: Slava Leleka Date: Fri Nov 24 13:15:35 2023 +0200 update examples --- .../extension/src/background/index.ts | 1 + .../tswebextension-mv2/scripts/constants.ts | 1 + .../tswebextension-mv3/scripts/constants.ts | 1 + packages/tswebextension/CHANGELOG.md | 10 ++++-- packages/tswebextension/README.md | 31 ++++++++++++++----- .../src/lib/common/configuration.ts | 5 +++ .../src/lib/mv2/background/app.ts | 10 ++++++ .../src/lib/mv2/background/cosmetic-api.ts | 5 ++- .../lib/mv2/background/configuration.test.ts | 1 + .../mv2/background/fixtures/configuration.ts | 1 + 10 files changed, 54 insertions(+), 12 deletions(-) diff --git a/packages/examples/tswebextension-mv2/extension/src/background/index.ts b/packages/examples/tswebextension-mv2/extension/src/background/index.ts index db357d100..e569bea14 100644 --- a/packages/examples/tswebextension-mv2/extension/src/background/index.ts +++ b/packages/examples/tswebextension-mv2/extension/src/background/index.ts @@ -29,6 +29,7 @@ const defaultConfig: ConfigurationMV2 = { filteringEnabled: true, stealthModeEnabled: true, collectStats: true, + debugScriptlets: false, allowlistInverted: false, allowlistEnabled: false, documentBlockingPageUrl: browser.runtime.getURL('pages/document-blocking.html'), diff --git a/packages/examples/tswebextension-mv2/scripts/constants.ts b/packages/examples/tswebextension-mv2/scripts/constants.ts index b3deeb88b..7b8943f10 100644 --- a/packages/examples/tswebextension-mv2/scripts/constants.ts +++ b/packages/examples/tswebextension-mv2/scripts/constants.ts @@ -30,6 +30,7 @@ export const DEFAULT_EXTENSION_CONFIG: ConfigurationMV2 = { settings: { assistantUrl: `${BuildOutput.AssistantInject}.js`, collectStats: true, + debugScriptlets: false, allowlistInverted: false, allowlistEnabled: false, stealthModeEnabled: true, diff --git a/packages/examples/tswebextension-mv3/scripts/constants.ts b/packages/examples/tswebextension-mv3/scripts/constants.ts index 72cc29c03..44969cf9a 100644 --- a/packages/examples/tswebextension-mv3/scripts/constants.ts +++ b/packages/examples/tswebextension-mv3/scripts/constants.ts @@ -18,6 +18,7 @@ export const DEFAULT_EXTENSION_CONFIG: Configuration = { settings: { assistantUrl: '', collectStats: true, + debugScriptlets: false, allowlistEnabled: true, allowlistInverted: false, stealthModeEnabled: true, diff --git a/packages/tswebextension/CHANGELOG.md b/packages/tswebextension/CHANGELOG.md index 58d00312f..4634b1279 100644 --- a/packages/tswebextension/CHANGELOG.md +++ b/packages/tswebextension/CHANGELOG.md @@ -8,11 +8,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 +## [Unreleased] + +### Added +- API `settings.debugScriptlets` property and `setDebugScriptlets()` method for its setting + [#2584](https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2584). + + ## [0.4.8] - 2023-11-29 ### Changed - -- `MessageHandlerMV2` type is exported now. +- `MessageHandlerMV2` type is exported now. ## [0.4.7] - 2023-11-21 diff --git a/packages/tswebextension/README.md b/packages/tswebextension/README.md index f3ddd2b51..01ebfa485 100644 --- a/packages/tswebextension/README.md +++ b/packages/tswebextension/README.md @@ -24,11 +24,12 @@ Table of content: - [allowlist](#allowlist) - [trustedDomains](#trusteddomains) - [userrules](#userrules) - - [verbose](#verbose) + - [verbose (deprecated)](#verbose) - [settings](#settings) - [allowlistInverted](#allowlistinverted) - [allowlistEnabled](#allowlistenabled) - [collectStats](#collectstats) + - [debugScriptlets](#debugscriptlets) - [stealthModeEnabled](#stealthmodeenabled) - [filteringEnabled](#filteringenabled) - [documentBlockingPageUrl](#documentblockingpageurl) @@ -58,6 +59,7 @@ Table of content: - [getMessageHandler()](#getmessagehandler) - [setFilteringEnabled() (MV2 only)](#setfilteringenabled-mv2-only) - [setCollectHitStats() (MV2 only)](#setcollecthitstats-mv2-only) + - [setDebugScriptlets() (MV2 only)](#setdebugscriptlets-mv2-only) - [setStealthModeEnabled() (MV2 only)](#setstealthmodeenabled-mv2-only) - [setSelfDestructFirstPartyCookies() (MV2 only)](#setselfdestructfirstpartycookies-mv2-only) - [setSelfDestructThirdPartyCookies() (MV2 only)](#setselfdestructthirdpartycookies-mv2-only) @@ -155,7 +157,7 @@ Commands: ## API -The main idea of the library is to provide a common interface for different browsers and manifest versions. +The main idea of the library is to provide a common interface for different browsers and manifest versions. via [Configuration](#configuration) object. [TsWebExtension](#tswebextension-1) class provides a set of methods for filtering content from the extension's background context. @@ -226,7 +228,7 @@ Path to the filter list file. type: `string` -Path to directory with converted rule sets. +Path to directory with converted rule sets. #### filteringLogEnabled (MV3 only) @@ -252,7 +254,7 @@ type: `string[]` List of rules added by user. -#### verbose (deprecated) +#### verbose (deprecated) type: `boolean | undefined` @@ -288,6 +290,12 @@ type: `boolean` Enables css hits counter if true. +##### debugScriptlets + +Optional, type: `boolean` + +Defaults to false. Enables scriptlets logging in console if true. + ##### stealthModeEnabled type: `boolean` @@ -464,6 +472,15 @@ Updates [collectStats](#collectstats) configuration value without re-initializat Throws error if [configuration](#configuration) is not set. +##### setDebugScriptlets() (MV2 only) + +type: `(isDebugScriptlets: boolean) => void` + +Updates [debugScriptlets](#debugscriptlets) configuration value +without re-initialization of engine. + +Throws error if [configuration](#configuration) is not set. + ##### setStealthModeEnabled() (MV2 only) type: `(value: isStealthModeEnabled) => Promise` @@ -566,7 +583,7 @@ type: `TabReloadEvent` Dispatched on tab reload. -#### applyBasicRule +#### applyBasicRule type: `ApplyBasicRuleEvent` @@ -576,7 +593,7 @@ Dispatched on request block or allowlist rule matching. type: `ApplyCosmeticRuleEvent` -Dispatched on elemhide, css or html rule applying. +Dispatched on elemhide, css or html rule applying. #### applyCspRule @@ -649,7 +666,7 @@ Event channel for [filtering log events](#filtering-log-api-mv2-only). ### methods #### addEventListener() -type: +type: ``` (type: T, listener: FilteringLogListener>) => void ``` diff --git a/packages/tswebextension/src/lib/common/configuration.ts b/packages/tswebextension/src/lib/common/configuration.ts index 72a190d6a..d337b5ad3 100644 --- a/packages/tswebextension/src/lib/common/configuration.ts +++ b/packages/tswebextension/src/lib/common/configuration.ts @@ -113,6 +113,11 @@ export const settingsConfigValidator = zod.object({ */ collectStats: zod.boolean(), + /** + * Enables verbose scriptlets logging if true. + */ + debugScriptlets: zod.boolean().default(false), + /** * Enables stealth mode if true. */ diff --git a/packages/tswebextension/src/lib/mv2/background/app.ts b/packages/tswebextension/src/lib/mv2/background/app.ts index 74ed7b1d2..e5e76acbd 100644 --- a/packages/tswebextension/src/lib/mv2/background/app.ts +++ b/packages/tswebextension/src/lib/mv2/background/app.ts @@ -258,6 +258,16 @@ MessageHandlerMV2 this.configuration.settings.collectStats = isCollectStats; } + /** + * Updates `debugScriptlets` configuration value without re-initialization of engine. + * + * @throws Error if {@link configuration} not set. + * @param isDebugScriptlets `debugScriptlets` config value. + */ + public setDebugScriptlets(isDebugScriptlets: boolean): void { + this.configuration.settings.debugScriptlets = isDebugScriptlets; + } + /** * Updates `stealthModeEnabled` configuration value without re-initialization of engine. * Also updates webRTC privacy.network settings on demand. diff --git a/packages/tswebextension/src/lib/mv2/background/cosmetic-api.ts b/packages/tswebextension/src/lib/mv2/background/cosmetic-api.ts index 3eff437eb..f22a5ae0c 100644 --- a/packages/tswebextension/src/lib/mv2/background/cosmetic-api.ts +++ b/packages/tswebextension/src/lib/mv2/background/cosmetic-api.ts @@ -176,9 +176,8 @@ export class CosmeticApi extends CosmeticApiCommon { if (configuration) { const { settings } = configuration; if (settings) { - if (settings.collectStats) { - debug = true; - } + // https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2584 + debug = settings.debugScriptlets; } } diff --git a/packages/tswebextension/test/lib/mv2/background/configuration.test.ts b/packages/tswebextension/test/lib/mv2/background/configuration.test.ts index 60164c46c..4abb52954 100644 --- a/packages/tswebextension/test/lib/mv2/background/configuration.test.ts +++ b/packages/tswebextension/test/lib/mv2/background/configuration.test.ts @@ -15,6 +15,7 @@ describe('configuration validator', () => { filteringEnabled: true, stealthModeEnabled: true, collectStats: true, + debugScriptlets: false, allowlistInverted: false, allowlistEnabled: false, documentBlockingPageUrl: 'https://example.org', diff --git a/packages/tswebextension/test/lib/mv2/background/fixtures/configuration.ts b/packages/tswebextension/test/lib/mv2/background/fixtures/configuration.ts index 9d4d749ea..7bede8c87 100644 --- a/packages/tswebextension/test/lib/mv2/background/fixtures/configuration.ts +++ b/packages/tswebextension/test/lib/mv2/background/fixtures/configuration.ts @@ -13,6 +13,7 @@ export const getConfigurationMv2Fixture = (): ConfigurationMV2 => ({ filteringEnabled: true, stealthModeEnabled: true, collectStats: true, + debugScriptlets: false, allowlistInverted: false, allowlistEnabled: false, documentBlockingPageUrl: 'https://example.org',