@adguard/tswebextension
TypeScript icon, indicating that this package has built-in type declarations

1.0.24 • Public • Published

TSWebExtension

TypeScript library that wraps webextension api for tsurlfilter library.

Table of content:

Browser support

manifest v2 manifest v3
Chrome 🚧
Firefox 🚧

Install

pnpm add @adguard/tswebextension

Usage

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 intergrate 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

CLI

The console interface provides useful tools for building extensions.

Usage: tswebextension-utils [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

API

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.

configuration

type: Configuration

Configuration object.

TSWEBEXTENSION_VERSION

type: string

Version of the library.

EXTENDED_CSS_VERSION

type: string

Version of the extended css module, used in current library version.

filters (MV2 only)

type: FilterMV2[]

List of filters.

filterId

type: number

Filter identifier.

content

type: string

Filter list text string.

trusted

type: boolean

Determines if filter list js rules should be executed.

staticFiltersIds (MV3 only)

type: number[]

List of static filters ids.

customFilters (MV3 only)

type: CustomFilterMV3[]

List of custom filters that can be added/edited/deleted by the user.

filterId

type: number

Filter identifier.

content

type: string

Filter list text string.

filtersPath (MV3 only)

type: string

Path to the filter list file.

ruleSetsPath (MV3 only)

type: string

Path to directory with converted rule sets.

filteringLogEnabled (MV3 only)

type: boolean

Enables filtering log if true.

allowlist

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.

trustedDomains

type: string[]

List of domain names of sites, which should be temporary excluded from document blocking.

userrules

type: string[]

List of rules added by user.

verbose (deprecated)

type: boolean | undefined

Optional flag responsible for logging. Defaults to true. Will be removed in the next minor version.

logLevel

type: string | undefined

Optional flag that sets logging level, defaults to 'error'. Available levels: 'error', 'warn', 'info', 'debug'.

settings

type: SettingsConfig

Settings configuration object.

allowlistInverted

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.

allowlistEnabled

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.

collectStats

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

Enables stealth mode if true.

filteringEnabled

type: boolean

Enables filtering if true.

documentBlockingPageUrl

type: string

Redirect url for $document rules.

assistantUrl

type: string

Path to the assembled @adguard/assistant module. Necessary for lazy on-demand loading of the assistant.

stealthConfig

type: StealthConfig

Stealth mode configuration object.

selfDestructFirstPartyCookies

type: boolean

Should the application set a fixed lifetime from selfDestructFirstPartyCookiesTime for first-party cookies.

selfDestructFirstPartyCookiesTime

type: number

Time in minutes to delete first-party cookies.

selfDestructThirdPartyCookies

type: boolean

Should the application set a fixed lifetime from selfDestructThirdPartyCookiesTime for third-party cookies.

selfDestructThirdPartyCookiesTime

type: number

Time in minutes to delete third-party cookies.

hideReferrer

type: boolean

Should the application hide the origin referrer in third-party requests by replacing the referrer url with the request url.

hideSearchQueries

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.

blockChromeClientData

type: boolean

For Google Chrome, it removes the 'X-Client-Data' header from the requests, which contains information about the browser version and modifications.

sendDoNotTrack

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.

blockWebRTC

type: boolean

Blocks the possibility of leaking your IP address through WebRTC, even if you use a proxy server or VPN.

TsWebExtension

Properties

configuration

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.

onFilteringLogEvent

type: EventChannel<FilteringLogEvent>

Event channel for filtering log events.

isStarted

type: boolean

Is app started.

Methods

initStorage()

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.

start()

type: (configuration: TConfiguration) => Promise<TConfigurationResult>

Starts the app.

Also updates webRTC privacy.network settings on demand and flushes browser in-memory request cache.

configure()

type: (configuration: TConfiguration) => Promise<TConfigurationResult>

Updates the configuration.

Also updates webRTC privacy.network settings on demand and flushes browser in-memory request cache.

stop()

type: () => Promise<void>

Stops the app.

openAssistant()

type: (tabId: number) => void

Opens the assistant in the specified tab.

closeAssistant()

type: (tabId: number) => void

getRulesCount()

type: () => number

Returns number of active rules.

getMessageHandler()

type: () => MessageHandlerMV2 | MessageHandlerMV3

Returns a message handler that will listen to internal messages, for example: message for get computed css for content-script.

setFilteringEnabled() (MV2 only)

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.

setCollectHitStats() (MV2 only)

type: (isCollectStats: boolean) => void

Updates collectStats configuration value without re-initialization of engine.

Throws error if configuration is not set.

setDebugScriptlets() (MV2 only)

type: (isDebugScriptlets: boolean) => void

Updates debugScriptlets configuration value without re-initialization of engine.

Throws error if configuration is not set.

setStealthModeEnabled() (MV2 only)

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.

setSelfDestructFirstPartyCookies() (MV2 only)

type: (isSelfDestructFirstPartyCookies: boolean) => void

Updates selfDestructFirstPartyCookies stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setSelfDestructThirdPartyCookies() (MV2 only)

type: (isSelfDestructThirdPartyCookies: boolean) => void

Updates selfDestructThirdPartyCookies stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setSelfDestructFirstPartyCookiesTime() (MV2 only)

type: (selfDestructFirstPartyCookiesTime: number) => void

Updates selfDestructFirstPartyCookiesTime stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setSelfDestructThirdPartyCookiesTime() (MV2 only)

type: (selfDestructThirdPartyCookiesTime: number) => void

Updates selfDestructThirdPartyCookiesTime stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setHideReferrer() (MV2 only)

type: (isHideReferrer: boolean) => void

Updates hideReferrer stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setHideSearchQueries() (MV2 only)

type: (isHideSearchQueries: boolean) => void

Updates hideSearchQueries stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setBlockChromeClientData() (MV2 only)

type: (isBlockChromeClientData: boolean) => void

Updates blockChromeClientData stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setSendDoNotTrack() (MV2 only)

type: (isSendDoNotTrack: boolean) => void

Updates sendDoNotTrack stealth config value without re-initialization of engine.

Throws error if configuration is not set.

setBlockWebRTC() (MV2 only)

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.

Filtering Log API (MV2 only)

Provides a set of methods for filtering log events management.

events

sendRequest

type: SendRequestEvent

Dispatched on request sending.

tabReload

type: TabReloadEvent

Dispatched on tab reload.

applyBasicRule

type: ApplyBasicRuleEvent

Dispatched on request block or allowlist rule matching.

applyCosmeticRule

type: ApplyCosmeticRuleEvent

Dispatched on elemhide, css or html rule applying.

applyCspRule

type: ApplyCspRuleEvent

Dispatched on csp rule applying.

receiveResponse

type: ReceiveResponseEvent

Dispatched on response receiving.

cookie

type: CookieEvent

Dispatched on cookie removing or modifying.

removeHeader

type: RemoveHeaderEvent

Dispatched on request or response header removing.

removeParam

type: RemoveParamEvent

Dispatched on request or response param removing.

replaceRuleApply

type: ReplaceRuleApplyEvent

Dispatched on replace rule applying.

contentFilteringStart

type: ContentFilteringStartEvent

Dispatched on content filtering start.

contentFilteringFinish

type: ContentFilteringFinishEvent

Dispatched on content filtering end.

stealthAction

type: StealthActionEvent

Dispatched on stealth action.

JsInject

type: JsInjectEvent

Dispatched on js inject into page context.

properties

onLogEvent

type: EventChannel<FilteringLogEvent>

Event channel for filtering log events.

methods

addEventListener()

type:

<T extends FilteringEventType>(type: T, listener: FilteringLogListener<ExtractedFilteringLogEvent<T>>) => void

Registers a listener for the specified filtering event type.

publishEvent()

type:

<T extends FilteringLogEvent>(event: T) => void

Dispatch the specified filtering event.

Development

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

Lint source code

npx lerna run --scope=@adguard/tswebextension lint

Readme

Keywords

none

Package Sidebar

Install

npm i @adguard/tswebextension

Weekly Downloads

93

Version

1.0.24

License

GPL-3.0-only

Unpacked Size

3.55 MB

Total Files

241

Last publish

Collaborators

  • ameshkov
  • maximtop
  • blakhard