Skip to content

Commit 69142f7

Browse files
authored
🤖 Merge PR DefinitelyTyped#74000 [chrome] update declarativeWebRequest namespace (MV2 only) by @erwanjugand
1 parent d3264da commit 69142f7

2 files changed

Lines changed: 181 additions & 6 deletions

File tree

‎types/chrome/index.d.ts‎

Lines changed: 155 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,109 +2435,258 @@ declare namespace chrome {
24352435
* @deprecated Check out the {@link declarativeNetRequest} API instead
24362436
*/
24372437
export namespace declarativeWebRequest {
2438+
/** Filters request headers for various criteria. Multiple criteria are evaluated as a conjunction. */
24382439
export interface HeaderFilter {
2440+
/** Matches if the header name is equal to the specified string. */
24392441
nameEquals?: string | undefined;
2442+
/** Matches if the header value contains all of the specified strings. */
24402443
valueContains?: string | string[] | undefined;
2444+
/** Matches if the header name ends with the specified string. */
24412445
nameSuffix?: string | undefined;
2446+
/** Matches if the header value ends with the specified string. */
24422447
valueSuffix?: string | undefined;
2448+
/** Matches if the header value starts with the specified string. */
24432449
valuePrefix?: string | undefined;
2450+
/** Matches if the header name contains all of the specified strings. */
24442451
nameContains?: string | string[] | undefined;
2452+
/** Matches if the header value is equal to the specified string. */
24452453
valueEquals?: string | undefined;
2454+
/** Matches if the header name starts with the specified string. */
24462455
namePrefix?: string | undefined;
24472456
}
24482457

2458+
/** Adds the response header to the response of this web request. As multiple response headers may share the same name, you need to first remove and then add a new response header in order to replace one. */
24492459
export interface AddResponseHeader {
2460+
/** HTTP response header name. */
24502461
name: string;
2462+
/** HTTP response header value. */
24512463
value: string;
24522464
}
24532465

2466+
/** Removes one or more cookies of response. Note that it is preferred to use the Cookies API because this is computationally less expensive. */
24542467
export interface RemoveResponseCookie {
2455-
filter: ResponseCookie;
2468+
/** Filter for cookies that will be removed. All empty entries are ignored. */
2469+
filter: FilterResponseCookie;
24562470
}
24572471

2472+
/** Removes all response headers of the specified names and values. */
24582473
export interface RemoveResponseHeader {
2474+
/** HTTP request header name (case-insensitive). */
24592475
name: string;
2476+
/** HTTP request header value (case-insensitive). */
24602477
value?: string | undefined;
24612478
}
24622479

2480+
/** Matches network events by various criteria. */
24632481
export interface RequestMatcher {
2482+
/** Matches if the MIME media type of a response (from the HTTP Content-Type header) is contained in the list. */
24642483
contentType?: string[] | undefined;
2465-
url?: chrome.events.UrlFilter | undefined;
2484+
/** Matches if the conditions of the UrlFilter are fulfilled for the URL of the request. */
2485+
url?: events.UrlFilter | undefined;
2486+
/** Matches if the MIME media type of a response (from the HTTP Content-Type header) is not contained in the list. */
24662487
excludeContentType?: string[] | undefined;
2488+
/** Matches if none of the request headers is matched by any of the HeaderFilters. */
2489+
excludeResponseHeaders?: HeaderFilter[] | undefined;
2490+
/** Matches if none of the response headers is matched by any of the HeaderFilters. */
24672491
excludeResponseHeader?: HeaderFilter[] | undefined;
2468-
resourceType?: string | undefined;
2492+
/**
2493+
* Matches if the conditions of the UrlFilter are fulfilled for the 'first party' URL of the request. The 'first party' URL of a request, when present, can be different from the request's target URL, and describes what is considered 'first party' for the sake of third-party checks for cookies.
2494+
* @deprecated since Chrome 82
2495+
*/
2496+
firstPartyForCookiesUrl?: events.UrlFilter | undefined;
2497+
/** Matches if some of the request headers is matched by one of the HeaderFilters. */
2498+
requestHeaders?: HeaderFilter[] | undefined;
2499+
/** Matches if the request type of a request is contained in the list. Requests that cannot match any of the types will be filtered out. */
2500+
resourceType?: `${webRequest.ResourceType}`[] | undefined;
2501+
/** Matches if some of the response headers is matched by one of the HeaderFilters. */
24692502
responseHeaders?: HeaderFilter[] | undefined;
2503+
/** Contains a list of strings describing stages. Allowed values are 'onBeforeRequest', 'onBeforeSendHeaders', 'onHeadersReceived', 'onAuthRequired'. If this attribute is present, then it limits the applicable stages to those listed. Note that the whole condition is only applicable in stages compatible with all attributes. */
2504+
stages?: `${Stage}`[] | undefined;
2505+
/**
2506+
* If set to true, matches requests that are subject to third-party cookie policies. If set to false, matches all other requests.
2507+
* @deprecated since Chrome 87
2508+
*/
2509+
thirdPartyForCookies?: boolean | undefined;
24702510
}
24712511

2512+
/** Masks all rules that match the specified criteria. */
24722513
export interface IgnoreRules {
2473-
lowerPriorityThan: number;
2514+
/** If set, rules with the specified tag are ignored. This ignoring is not persisted, it affects only rules and their actions of the same network request stage. Note that rules are executed in descending order of their priorities. This action affects rules of lower priority than the current rule. Rules with the same priority may or may not be ignored. */
2515+
hasTag?: string | undefined;
2516+
/** If set, rules with a lower priority than the specified value are ignored. This boundary is not persisted, it affects only rules and their actions of the same network request stage. */
2517+
lowerPriorityThan?: number | undefined;
24742518
}
24752519

2520+
/** Declarative event action that redirects a network request to an empty document. */
24762521
export interface RedirectToEmptyDocument {}
24772522

2523+
/** Declarative event action that redirects a network request. */
24782524
export interface RedirectRequest {
2525+
/** Destination to where the request is redirected. */
24792526
redirectUrl: string;
24802527
}
24812528

2529+
/** A specification of a cookie in HTTP Responses. */
24822530
export interface ResponseCookie {
2531+
/** Value of the Domain cookie attribute. */
24832532
domain?: string | undefined;
2533+
/** Name of a cookie. */
24842534
name?: string | undefined;
2535+
/** Value of the Expires cookie attribute. */
24852536
expires?: string | undefined;
2537+
/** Value of the Max-Age cookie attribute */
24862538
maxAge?: number | undefined;
2539+
/** Value of a cookie, may be padded in double-quotes. */
24872540
value?: string | undefined;
2541+
/** Value of the Path cookie attribute. */
24882542
path?: string | undefined;
2543+
/** Existence of the HttpOnly cookie attribute. */
24892544
httpOnly?: string | undefined;
2545+
/** Existence of the Secure cookie attribute. */
24902546
secure?: string | undefined;
24912547
}
24922548

2549+
/** Adds a cookie to the response or overrides a cookie, in case another cookie of the same name exists already. Note that it is preferred to use the Cookies API because this is computationally less expensive. */
24932550
export interface AddResponseCookie {
24942551
cookie: ResponseCookie;
24952552
}
24962553

2554+
/** Edits one or more cookies of response. Note that it is preferred to use the Cookies API because this is computationally less expensive. */
24972555
export interface EditResponseCookie {
2556+
/** Filter for cookies that will be modified. All empty entries are ignored. */
24982557
filter: ResponseCookie;
2558+
/** Attributes that shall be overridden in cookies that machted the filter. Attributes that are set to an empty string are removed. */
24992559
modification: ResponseCookie;
25002560
}
25012561

2562+
/** Declarative event action that cancels a network request. */
25022563
export interface CancelRequest {}
25032564

2565+
/** Removes the request header of the specified name. Do not use SetRequestHeader and RemoveRequestHeader with the same header name on the same request. Each request header name occurs only once in each request. */
25042566
export interface RemoveRequestHeader {
2567+
/** HTTP request header name (case-insensitive). */
25052568
name: string;
25062569
}
25072570

2571+
/** Edits one or more cookies of request. Note that it is preferred to use the Cookies API because this is computationally less expensive. */
25082572
export interface EditRequestCookie {
2573+
/** Filter for cookies that will be modified. All empty entries are ignored. */
25092574
filter: RequestCookie;
2575+
/** Attributes that shall be overridden in cookies that machted the filter. Attributes that are set to an empty string are removed. */
25102576
modification: RequestCookie;
25112577
}
25122578

2579+
/** A filter of a cookie in HTTP Responses. */
2580+
export interface FilterResponseCookie {
2581+
/** Inclusive lower bound on the cookie lifetime (specified in seconds after current time). Only cookies whose expiration date-time is set to 'now + ageLowerBound' or later fulfill this criterion. Session cookies do not meet the criterion of this filter. The cookie lifetime is calculated from either 'max-age' or 'expires' cookie attributes. If both are specified, 'max-age' is used to calculate the cookie lifetime. */
2582+
ageLowerBound?: number | undefined;
2583+
/** Inclusive upper bound on the cookie lifetime (specified in seconds after current time). Only cookies whose expiration date-time is in the interval [now, now + ageUpperBound] fulfill this criterion. Session cookies and cookies whose expiration date-time is in the past do not meet the criterion of this filter. The cookie lifetime is calculated from either 'max-age' or 'expires' cookie attributes. If both are specified, 'max-age' is used to calculate the cookie lifetime. */
2584+
ageUpperBound?: number | undefined;
2585+
/** Value of the Domain cookie attribute. */
2586+
domain?: string | undefined;
2587+
/** Value of the Expires cookie attribute. */
2588+
expires?: string | undefined;
2589+
/** Existence of the HttpOnly cookie attribute. */
2590+
httpOnly?: string | undefined;
2591+
/** Value of the Max-Age cookie attribute */
2592+
maxAge?: number | undefined;
2593+
/** Name of a cookie. */
2594+
name?: string | undefined;
2595+
/** Value of the Path cookie attribute. */
2596+
path?: string | undefined;
2597+
/** Existence of the Secure cookie attribute. */
2598+
secure?: string | undefined;
2599+
/** Filters session cookies. Session cookies have no lifetime specified in any of 'max-age' or 'expires' attributes. */
2600+
session?: boolean | undefined;
2601+
/** Value of a cookie, may be padded in double-quotes. */
2602+
value?: string | undefined;
2603+
}
2604+
2605+
/** Sets the request header of the specified name to the specified value. If a header with the specified name did not exist before, a new one is created. Header name comparison is always case-insensitive. Each request header name occurs only once in each request. */
25132606
export interface SetRequestHeader {
2607+
/** HTTP request header name. */
25142608
name: string;
2609+
/** HTTP request header value. */
25152610
value: string;
25162611
}
25172612

2613+
/** A filter or specification of a cookie in HTTP Requests. */
25182614
export interface RequestCookie {
2615+
/** Name of a cookie. */
25192616
name?: string | undefined;
2617+
/** Value of a cookie, may be padded in double-quotes. */
25202618
value?: string | undefined;
25212619
}
25222620

2621+
/** Redirects a request by applying a regular expression on the URL. The regular expressions use the RE2 syntax. */
25232622
export interface RedirectByRegEx {
2623+
/** Destination pattern. */
25242624
to: string;
2625+
/** A match pattern that may contain capture groups. Capture groups are referenced in the Perl syntax ($1, $2, ...) instead of the RE2 syntax (\1, \2, ...) in order to be closer to JavaScript Regular Expressions. */
25252626
from: string;
25262627
}
25272628

2629+
/** Declarative event action that redirects a network request to a transparent image. */
25282630
export interface RedirectToTransparentImage {}
25292631

2632+
/** Adds a cookie to the request or overrides a cookie, in case another cookie of the same name exists already. Note that it is preferred to use the Cookies API because this is computationally less expensive. */
25302633
export interface AddRequestCookie {
25312634
cookie: RequestCookie;
25322635
}
25332636

2637+
/** Removes one or more cookies of request. Note that it is preferred to use the Cookies API because this is computationally less expensive. */
25342638
export interface RemoveRequestCookie {
2639+
/** Filter for cookies that will be removed. All empty entries are ignored. */
25352640
filter: RequestCookie;
25362641
}
25372642

2538-
export interface RequestedEvent extends chrome.events.Event<() => void> {}
2643+
export enum Stage {
2644+
ON_AUTH_REQUIRED = "onAuthRequired",
2645+
ON_BEFORE_REQUEST = "onBeforeRequest",
2646+
ON_BEFORE_SEND_HEADERS = "onBeforeSendHeaders",
2647+
ON_HEADERS_RECEIVED = "onHeadersReceived",
2648+
}
2649+
2650+
export interface MessageDetails {
2651+
/** A UUID of the document that made the request. */
2652+
documentId?: string;
2653+
/** The lifecycle the document is in. */
2654+
documentLifecycle: extensionTypes.DocumentLifecycle;
2655+
/** The value 0 indicates that the request happens in the main frame; a positive value indicates the ID of a subframe in which the request happens. If the document of a (sub-)frame is loaded (`type` is `main_frame` or `sub_frame`), `frameId` indicates the ID of this frame, not the ID of the outer frame. Frame IDs are unique within a tab. */
2656+
frameId: number;
2657+
/** The type of frame the navigation occurred in. */
2658+
frameType: extensionTypes.FrameType;
2659+
/** The message sent by the calling script. */
2660+
message: string;
2661+
/** Standard HTTP method. */
2662+
method: string;
2663+
/** A UUID of the parent document owning this frame. This is not set if there is no parent. */
2664+
parentDocumentId?: string;
2665+
/** ID of frame that wraps the frame which sent the request. Set to -1 if no parent frame exists. */
2666+
parentFrameId: number;
2667+
/** The ID of the request. Request IDs are unique within a browser session. As a result, they could be used to relate different events of the same request. */
2668+
requestId: string;
2669+
/** The stage of the network request during which the event was triggered. */
2670+
stage: `${Stage}`;
2671+
/** The ID of the tab in which the request takes place. Set to -1 if the request isn't related to a tab. */
2672+
tabId: number;
2673+
/** The time when this signal is triggered, in milliseconds since the epoch. */
2674+
timeStamp: number;
2675+
/** How the requested resource will be used. */
2676+
type: `${webRequest.ResourceType}`;
2677+
url: string;
2678+
}
2679+
2680+
export interface SendMessageToExtension {
2681+
/** The value that will be passed in the message attribute of the dictionary that is passed to the event handler. */
2682+
message: string;
2683+
}
2684+
2685+
/** Fired when a message is sent via {@link declarativeWebRequest.SendMessageToExtension} from an action of the declarative web request API. */
2686+
export const onMessage: events.Event<(details: MessageDetails) => void>;
25392687

2540-
export var onRequest: RequestedEvent;
2688+
/** Provides the Declarative Event API consisting of `addRules`, `removeRules`, and `getRules`. */
2689+
export const onRequest: events.Event<() => void>;
25412690
}
25422691

25432692
////////////////////

‎types/chrome/test/index.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,6 +4149,32 @@ async function testDeclarativeNetRequest() {
41494149
});
41504150
}
41514151

4152+
// https://developer.chrome.com/docs/extensions/mv2/reference/declarativeWebRequest
4153+
function testDeclarativeWebRequest() {
4154+
chrome.declarativeWebRequest.onRequest.addRules([]); // $ExpectType void
4155+
chrome.declarativeWebRequest.onRequest.removeRules([]); // $ExpectType void
4156+
chrome.declarativeWebRequest.onRequest.getRules((rules) => { // $ExpectType void
4157+
rules; // $ExpectType Rule[]
4158+
});
4159+
4160+
checkChromeEvent(chrome.declarativeWebRequest.onMessage, (details) => {
4161+
details.documentId; // $ExpectType string | undefined
4162+
details.documentLifecycle; // $ExpectType DocumentLifecycle
4163+
details.frameId; // $ExpectType number
4164+
details.frameType; // $ExpectType FrameType
4165+
details.message; // $ExpectType string
4166+
details.method; // $ExpectType string
4167+
details.parentDocumentId; // $ExpectType string | undefined
4168+
details.parentFrameId; // $ExpectType number
4169+
details.requestId; // $ExpectType string
4170+
details.stage; // $ExpectType "onBeforeRequest" | "onBeforeSendHeaders" | "onHeadersReceived" | "onAuthRequired"
4171+
details.tabId; // $ExpectType number
4172+
details.timeStamp; // $ExpectType number
4173+
details.type; // $ExpectType "object" | "other" | "main_frame" | "sub_frame" | "stylesheet" | "script" | "image" | "font" | "xmlhttprequest" | "ping" | "csp_report" | "media" | "websocket" | "webbundle"
4174+
details.url; // $ExpectType string
4175+
});
4176+
}
4177+
41524178
// https://developer.chrome.com/docs/extensions/reference/storage
41534179
function testStorageForPromise() {
41544180
chrome.storage.sync.getBytesInUse().then(() => {});

0 commit comments

Comments
 (0)