TypeScript library that wraps webextension api for tsurlfilter library.
Table of content:
-
TSWebExtension
- Browser compatibility
- Install
- Usage
- CLI
- Side effects
-
API
-
configuration
- TSWEBEXTENSION_VERSION
- EXTENDED_CSS_VERSION
- filters (MV2 only)
- staticFiltersIds (MV3 only)
- customFilters (MV3 only)
- filtersPath (MV3 only)
- ruleSetsPath (MV3 only)
- declarativeLogEnabled (MV3 only)
- quickFixesRules (MV3 only)
- allowlist
- trustedDomains
- userrules (MV2)
- userrules (MV3)
- verbose (deprecated)
- logLevel
- settings
-
TsWebExtension
- Properties
-
Methods
- initStorage()
- start()
- configure()
- stop()
- openAssistant()
- closeAssistant()
- getRulesCount()
- retrieveRuleNode
- getMessageHandler()
- setFilteringEnabled() (MV2 only)
- setCollectHitStats() (MV2 only)
- setDebugScriptlets()
- setStealthModeEnabled() (MV2 only)
- setSelfDestructFirstPartyCookies() (MV2 only)
- setSelfDestructThirdPartyCookies() (MV2 only)
- setSelfDestructFirstPartyCookiesTime() (MV2 only)
- setSelfDestructThirdPartyCookiesTime() (MV2 only)
- setHideReferrer() (MV2 only)
- setHideSearchQueries() (MV2 only)
- setBlockChromeClientData() (MV2 only)
- setSendDoNotTrack() (MV2 only)
- setBlockWebRTC() (MV2 only)
- getRawFilterList() (MV3 only)
- getPreprocessedFilterList() (MV3 only)
-
configuration
- Filtering Log API (MV2 only)
- Development
Browser | Version |
---|---|
Chromium-based browsers MV2 | 106 |
Chromium-based browsers MV3 | 121 |
Firefox | 78 |
Firefox Mobile | 113 |
pnpm add @adguard/tswebextension
You can find examples in packages/examples/tswebextension-*
Note:
Before running compiled app, load the web accessible resources for redirect rules
via built-in cli:
tswebextension war [path]
or integrate loading in your build pipeline:
import { copyWar, DEFAULT_WAR_PATH } from '@adguard/tswebextension/cli';
const build = async () => {
...
await copyWar(DEFAULT_WAR_PATH);
...
};
If path is not defined, the resources will be loaded to build/war
relative to your current working directory by default
The console interface provides useful tools for building extensions.
Usage: tswebextension [options] [command]
CLI to some development utils
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
war [path] Downloads web accessible resources for redirect rules
help [command] display help for command
In this project, the sideEffects
field is defined as follows in the package.json
file:
"sideEffects": [
"dist/assistant-inject.js",
"dist/content-script.js",
"dist/content-script.mv3.js",
"dist/gpc.mv3.js",
"dist/hide-document-referrer.mv3.js"
]
This configuration indicates that the specified files have side effects and should not be tree-shaken by the bundler. These files will modifying the global scope, which are essential for the correct functioning of the application.
By explicitly listing these files, we ensure that they are included in the final bundle, even if they do not have any direct imports or exports that are used elsewhere in the codebase.
In the content script, we need access to @adguard/assistant
only when the user clicks 'block ad manually'. Therefore, we marked files with @adguard/assistant
as side effects. We also added a required field to the configuration object to ensure the assistant is bundled inside the final extension, allowing tswebextension
to load it on-demand.
Same approach with dynamic injecting we use for stealth options GPC and Hide
Document Referrer. Handlers for these options will dynamically register content
scripts via scripting.registerContentScript
.
The main idea of the library is to provide a common interface for different browsers and manifest versions. via Configuration object.
TsWebExtension class provides a set of methods for filtering content from the extension's background context.
MV2 submodule also provides a set of methods for filtering log management.
type: Configuration
Configuration object.
type: string
Version of the library.
type: string
Version of the extended css module, used in current library version.
type: FilterMV2[]
List of filters.
type: number
Filter identifier.
type: Uint8Array[]
AGTree byte buffer chunks.
type: boolean
Determines if filter list js rules should be executed.
type: number[]
List of static filters ids.
type: CustomFilterMV3[]
List of custom filters that can be added/edited/deleted by the user.
type: number
Filter identifier.
type: string
Raw filter list.
type: Record<string, number>
Source map, where key is the rule start index in the byte buffer content
and value is the line start index in the raw filter list.
type: Record<string, string>
Conversion map, where key is the line start index in the raw filter list and value is the rule text.
type: Uint8Array[]
AGTree byte buffer chunks.
type: string
Path to the filter list file.
type: string
Path to directory with converted rule sets.
type: boolean
Enables matching declarative rules for filtering log.
type: preprocessedFilterList
Contains rules from AdGuard Quick Fixes rules which will applied in the dynamic rules between all other dynamic rules: allowlist, userrules and custom filters.
type: string
Raw filter list.
type: Record<string, number>
Source map, where key is the rule start index in the byte buffer content
and value is the line start index in the raw filter list.
type: Record<string, string>
Conversion map, where key is the line start index in the raw filter list and value is the rule text.
type: Uint8Array[]
AGTree byte buffer chunks.
type: string[]
List of hostnames or domains of sites, which should be excluded from blocking or which should be included in blocking depending on the value of allowlistInverted setting value.
type: string[]
List of domain names of sites, which should be temporary excluded from document blocking.
type: Uint8Array[]
AGTree byte buffer chunks.
type: Record<string, number> | undefined
Source map, where key is the rule start index in the byte buffer content
and value is the line start index in the raw filter list.
Optional field, can be omitted.
type: string
Raw filter list.
type: Record<string, number>
Source map, where key is the rule start index in the byte buffer content
and value is the line start index in the raw filter list.
type: Record<string, string>
Conversion map, where key is the line start index in the raw filter list and value is the rule text.
type: Uint8Array[]
AGTree byte buffer chunks.
type: boolean | undefined
Optional flag responsible for logging. Defaults to true. Will be removed in the next minor version.
type: string | undefined
Optional flag that sets logging level, defaults to 'error'. Available levels: 'error', 'warn', 'info', 'debug', 'trace'.
type: SettingsConfig
Settings configuration object.
type: boolean
If this flag is true, the application will work ONLY with domains from the allowlist, otherwise it will work everywhere EXCLUDING domains from the list.
type: boolean
Flag specifying allowlist enable state. We don't use allowlist array length condition for calculate enable state, because it's not cover case with empty list in inverted mode.
type: boolean
Enables css hits counter if true.
Optional, type: boolean
Defaults to false. Enables scriptlets logging in console if true.
type: boolean
Enables stealth mode if true.
type: boolean
Enables filtering if true.
type: string
Redirect url for $document rules.
type: string
Path to the assembled @adguard/assistant
module. Necessary for lazy on-demand loading of the assistant.
type: string
Path to the content script that set GPC Signal. Necessary for Do Not Track
stealth option.
type: string
Path to the content script that hides the document referrer. Necessary for Hide Search Queries
stealth option.
type: StealthConfig
Stealth mode configuration object.
type: boolean
Should the application set a fixed lifetime from selfDestructFirstPartyCookiesTime for first-party cookies.
type: number
Time in minutes to delete first-party cookies.
type: boolean
Should the application set a fixed lifetime from selfDestructThirdPartyCookiesTime for third-party cookies.
type: number
Time in minutes to delete third-party cookies.
type: boolean
Should the application hide the origin referrer in third-party requests by replacing the referrer url with the request url.
type: boolean
Should the application hide the original referrer from the search page containing the search query in third-party queries, replacing the referrer url with the request url.
type: boolean
For Google Chrome, it removes the 'X-Client-Data' header from the requests, which contains information about the browser version and modifications.
type: boolean
Includes HTTP headers 'DNT' and 'Sec-GPC' in all requests.
Read more about DNT: https://en.wikipedia.org/wiki/Do_Not_Track.
Read more about GPC: https://globalprivacycontrol.org.
type: boolean
Blocks the possibility of leaking your IP address through WebRTC, even if you use a proxy server or VPN.
type: ConfigurationMV2Context | ConfigurationMV3Context
Configuration context with an omitted array of rule and domain strings loaded in the filter engine.
It is used to reduce memory consumption when storing configuration data in memory.
type: EventChannel<FilteringLogEvent>
Event channel for filtering log events.
type: boolean
Is app started.
type: () => Promise<void>
Initialize app persistent data. This method called as soon as possible and allows access to the actual context before the app is started.
type: (configuration: TConfiguration) => Promise<TConfigurationResult>
Starts the app.
Also updates webRTC privacy.network settings on demand and flushes browser in-memory request cache.
type: (configuration: TConfiguration) => Promise<TConfigurationResult>
Updates the configuration.
Also updates webRTC privacy.network settings on demand and flushes browser in-memory request cache.
type: () => Promise<void>
Stops the app.
type: (tabId: number) => void
Opens the assistant in the specified tab.
type: (tabId: number) => void
type: () => number
Returns number of active rules.
type: (filterId: number, ruleIndex: number): AnyRule | null
Retrieves rule node from engine by filter id and rule index.
type: () => MessageHandlerMV2 | MessageHandlerMV3
Returns a message handler that will listen to internal messages, for example: message for get computed css for content-script.
type: (isFilteringEnabled: boolean) => Promise<void>
Updates filteringEnabled configuration value without re-initialization of engine.
Also updates webRTC privacy.network settings on demand and flushes browser in-memory request cache.
Throws error if configuration is not set.
type: (isCollectStats: boolean) => void
Updates collectStats configuration value without re-initialization of engine.
Throws error if configuration is not set.
type: (isDebugScriptlets: boolean) => void
Updates debugScriptlets configuration value without re-initialization of engine.
Throws error if configuration is not set.
type: (value: isStealthModeEnabled) => Promise<void>
Updates stealthModeEnabled configuration value without re-initialization of engine.
Also updates webRTC privacy.network settings on demand.
Throws error if configuration is not set.
type: (isSelfDestructFirstPartyCookies: boolean) => void
Updates selfDestructFirstPartyCookies stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (isSelfDestructThirdPartyCookies: boolean) => void
Updates selfDestructThirdPartyCookies stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (selfDestructFirstPartyCookiesTime: number) => void
Updates selfDestructFirstPartyCookiesTime stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (selfDestructThirdPartyCookiesTime: number) => void
Updates selfDestructThirdPartyCookiesTime stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (isHideReferrer: boolean) => void
Updates hideReferrer stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (isHideSearchQueries: boolean) => void
Updates hideSearchQueries stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (isBlockChromeClientData: boolean) => void
Updates blockChromeClientData stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (isSendDoNotTrack: boolean) => void
Updates sendDoNotTrack stealth config value without re-initialization of engine.
Throws error if configuration is not set.
type: (isBlockWebRTC: boolean) => Promise<void>
Updates blockWebRTC stealth config value without re-initialization of engine.
Also updates webRTC privacy.network settings on demand.
Throws error if configuration is not set.
type: async (filterId: number, ruleSetsPath: string): Promise<string>
Returns raw filter list for the specified filter id via the rule sets loader.
Throws error if rule sets path is not set.
type: async (filterId: number, ruleSetsPath: string): Promise<PreprocessedFilterList>
Returns preprocessed filter list for the specified filter id via the rule sets loader.
Throws error if rule sets path is not set.
[!NOTE] You can learn more about the preprocessed filter list in the tsurlfilter documentation.
Provides a set of methods for filtering log events management.
type: SendRequestEvent
Dispatched on request sending.
type: TabReloadEvent
Dispatched on tab reload.
type: ApplyBasicRuleEvent
Dispatched on request block or allowlist rule matching.
type: ApplyCosmeticRuleEvent
Dispatched on elemhide, css or html rule applying.
type: ApplyCspRuleEvent
Dispatched on csp rule applying.
type: ReceiveResponseEvent
Dispatched on response receiving.
type: CookieEvent
Dispatched on cookie removing or modifying.
type: RemoveHeaderEvent
Dispatched on request or response header removing.
type: RemoveParamEvent
Dispatched on request or response param removing.
type: ReplaceRuleApplyEvent
Dispatched on replace rule applying.
type: ContentFilteringStartEvent
Dispatched on content filtering start.
type: ContentFilteringFinishEvent
Dispatched on content filtering end.
type: StealthActionEvent
Dispatched on stealth action.
type: StealthAllowlistActionEvent
Dispatched on preventing stealth action with $stealth
rule.
type: JsInjectEvent
Dispatched on js inject into page context.
type: EventChannel<FilteringLogEvent>
Event channel for filtering log events.
type:
<T extends FilteringEventType>(type: T, listener: FilteringLogListener<ExtractedFilteringLogEvent<T>>) => void
Registers a listener for the specified filtering event type.
type:
<T extends FilteringLogEvent>(event: T) => void
Dispatch the specified filtering event.
This project is part of the @adguard/extensions
monorepo.
It is highly recommended to use lerna
for commands, as it will execute scripts in the correct order and can cache dependencies.
Run module tests
npx lerna run --scope=@adguard/tswebextension test
Run build
npx lerna run --scope=@adguard/tswebextension build
Note that during the build simplified companiesdb data —
trackers-min.ts
— is to be updated.
Lint source code
npx lerna run --scope=@adguard/tswebextension lint