From dc2a4fa8bdda4e40f56deef63d4d39c0c925cce2 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Tue, 12 Mar 2024 12:53:57 -0700 Subject: [PATCH] add index.d.ts thanks to https://github.com/DefinitelyTyped/DefinitelyTyped/pull/65416 for the initial version --- js/index.d.ts | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 123 insertions(+) create mode 100644 js/index.d.ts diff --git a/js/index.d.ts b/js/index.d.ts new file mode 100644 index 00000000..0b698ac2 --- /dev/null +++ b/js/index.d.ts @@ -0,0 +1,122 @@ +/// + +declare interface MimeType { + mime: string; +} + +export interface Resource { + name: string; + aliases: string[]; + kind: MimeType | "template"; + content: string; + dependencies?: string[]; + permission?: number; +} + +export interface FilterListMetadata { + homepage?: string; + title?: string; + expires?: number; + redirect?: string; +} + +export interface ParseOptions { + format: typeof FilterFormat; + rule_types: typeof RuleTypes; +} + +declare interface BlockerResult { + matched: boolean, + important: boolean, + redirect?: string, + rewritten_url?: string, + exception?: string, + filter?: string, +} + +export class Engine { + constructor(rules: FilterSet, debug: boolean); + addResource(resource: Resource): boolean; + check(url: string, source_url: string, request_type: string, debug?: false): boolean; + check(url: string, source_url: string, request_type: string, debug: true): BlockerResult; + clearTags(): null; + deserialize(serialized_handle: ArrayBuffer): null; + enableTag(tag: string): null; + getResource(name: string): Resource; + serializeCompressed(): ArrayBuffer; + serializeRaw(): ArrayBuffer; + tagExists(tag: string): boolean; + useResources(resources: Resource[]): null; +} + +type CbType = + 'block' | + 'block-cookies' | + 'css-display-none' | + 'ignore-previous-rules' | + 'make-https'; + +declare interface CbAction { + type: CbType, + selector?: string, +} + +type CbResourceType = + 'document' | + 'image' | + 'style-sheet' | + 'script' | + 'font' | + 'raw' | + 'svg-document' | + 'media' | + 'popup'; + +type CbLoadType = + 'first-party' | + 'third-party'; + +declare interface CbTrigger { + 'url-filter': string, + 'url-filter-is-case-sensitive'?: boolean, + 'if-domain'?: string[], + 'unless-domain'?: string[], + 'resource-type'?: Record, + 'load-type'?: CbLoadType[], + 'if-top-url'?: string[], + 'unless-top-url'?: string[], +} + +declare interface CbRule { + action: CbAction, + trigger: CbTrigger, +} + +declare interface ContentBlockingConversionResult { + content_blocking_rules: CbRule[], + filters_used: string[], +} + +export class FilterSet { + constructor(debug: boolean); + addFilter(filter: string, opts?: ParseOptions): null; + addFilters(rules: string[], opts?: ParseOptions): FilterListMetadata; + intoContentBlocking(): ContentBlockingConversionResult | undefined; +} + +export const FilterFormat: { + HOSTS: string; + STANDARD: string; +}; + +export const RuleTypes: { + ALL: string; + COSMETIC_ONLY: string; + NETWORK_ONLY: string; +}; + +export function uBlockResources( + web_accessible_resource_dir: string, + redirect_resources_path: string, + scriptlets_path?: string, +): Resource[]; diff --git a/package.json b/package.json index a4d13a52..d17613f2 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "rust" ], "main": "js/index.js", + "types": "js/index.d.ts", "author": "Anton Lazarev ", "contributors": [ "Andrius Aucinas"