class HidingBuilder { } type RequestType = 'image' | 'script' | 'font' | 'frame' | 'media' | 'font' | 'css' ; class NetworkBuilder { private options : Set < RequestType > = new Set ( ) ; private prefix : string | undefined = undefined ; private infix : string | undefined = undefined ; private suffix : string | undefined = undefined ; private redirect : string | undefined = undefined ; private blockRequestsWithType ( t ) { if ( this . options . has ( t ) ) { throw new Error ( `Already blocking type ${ t }` ) ; } this . options . add ( t ) ; return this ; } public images ( ) { return this . blockRequestsWithType ( 'image' ) ; } public scripts ( ) { return this . blockRequestsWithType ( 'script' ) ; } public frames ( ) { return this . blockRequestsWithType ( 'frame' ) ; } public fonts ( ) { return this . blockRequestsWithType ( 'font' ) ; } public medias ( ) { return this . blockRequestsWithType ( 'media' ) ; } public styles ( ) { return this . blockRequestsWithType ( 'css' ) ; } public redirectTo ( redirect ) { if ( this . redirect !== undefined ) { throw new Error ( `Already redirecting: ${ this . redirect }` ) ; } this . redirect = `redirect=${ redirect }` ; return this ; } public urlContains ( infix ) { if ( this . infix !== undefined ) { throw new Error ( `Already matching pattern: ${ this . infix }` ) ; } this . infix = infix ; return this ; } public urlStartsWith ( prefix ) { if ( this . prefix !== undefined ) { throw new Error ( `Already matching prefix: ${ this . prefix }` ) ; } this . prefix = `|${ prefix }` ; return this ; } public urlEndsWith ( suffix ) { if ( this . suffix !== undefined ) { throw new Error ( `Already matching suffix: ${ this . suffix }` ) ; } this . suffix = `${ suffix }|` ; return this ; } public withHostname ( hostname ) { if ( this . prefix !== undefined ) { throw new Error ( `Cannot match hostname if filter already has prefix: ${ this . prefix }` ) ; } this . prefix = `||${ hostname }^` ; return this ; } public toString ( ) { const parts = [ ] ; if ( this . prefix !== undefined ) { parts . push ( this . prefix ) ; } if ( this . infix !== undefined ) { parts . push ( this . infix ) ; } if ( this . suffix !== undefined ) { parts . push ( this . suffix ) ; } const options = [ 'important' ] ; if ( this . options . size !== 0 ) { for ( const option of this . options ) { options . push ( option ) ; } } if ( this . redirect !== undefined ) { options . push ( this . redirect ) ; } return `${ parts . length === 0 ? '*' : parts . join ( '*' ) }$${ options . join ( ',' ) }` ; } } export function block ( ) { return new NetworkBuilder ( ) ; } export function hide ( ) { return new HidingBuilder ( ) ; }