Files
Sam Maddock 3c4e6463fc feat: preload script URL filters (#54903)
* feat: preload script URL filters

* feat: reject an empty preload script matches array

An explicit `matches: []` meant "every URL", the same ambiguity webRequest
filters dropped in favor of `<all_urls>`. Registering one now throws and
points at `<all_urls>`; omitting `matches` still matches every URL, so
existing registrations are unaffected.

Also adds coverage that `<all_urls>` matches both an http page and
about:blank.

* docs: link preload URL filters to MDN match patterns

Matches the webRequest filter docs, which already link to MDN.

* test: extension protocol

* feat: add matchOriginAsFallback to preload script URL filters

A document whose URL has no origin of its own (about:, blob:, data:,
filesystem:) was only matched by its literal URL, so an about:blank popup
escaped its opener's excludeMatches and excludeGlobs.

With matchOriginAsFallback, frame preloads match such documents by the
origin they inherited from their creator, like match_origin_as_fallback
for content scripts.

* fix: reject an empty preload script includeGlobs array

An explicitly empty includeGlobs array converted to the same state as an
omitted one, so the preload ran for every URL instead of none. Reject it
at registration, as an empty matches array already is.

* test: use vitest assertions in preload URL filter specs

main dropped chai in #54439, so the chai-style assertions these specs
added no longer match the rest of the suite, and the dirty-chai call
forms (to.be.empty(), to.be.false()) are no longer available.

* fix: address review feedback on preload script URL filters

- Match a data: URL loaded with baseURLForDataURL by its base URL, which
  is the URL the document commits at.
- Never match when URL patterns were not parsed, instead of matching
  every URL.
- Explain a scheme separator mismatch in the invalid pattern error.
- Document custom scheme requirements, error page matching and opaque
  origins with matchOriginAsFallback.
- Add specs for baseURLForDataURL, subframes and redirects.

* docs: list preload script URL filter conditions

Break the URL filter description for ses.registerPreloadScript into
bullet lists, and note that in-page navigations do not rerun the
preload script.
2026-10-21 16:48:27 -04:00

1.7 KiB

PreloadScript Object

  • type string - Context type where the preload script will be executed. Possible values include frame or service-worker.
  • id string - Unique ID of preload script.
  • filePath string - Path of the script file. Must be an absolute path.
  • matches string[] (optional) - Match patterns for URLs the script runs in. A custom protocol can be used if its scheme is registered as standard with protocol.registerSchemesAsPrivileged. To filter documents served from a non-standard scheme, use includeGlobs or excludeGlobs, which match against the whole URL. If omitted, the script runs for all URLs not excluded by other filters.
  • excludeMatches string[] (optional) - Match patterns for URLs the script will not run in.
  • includeGlobs string[] (optional) - Glob patterns applied after matches. A URL must also match one of these globs. * matches any sequence of characters and ? matches any single character.
  • excludeGlobs string[] (optional) - Glob patterns for URLs the script will not run in.
  • matchOriginAsFallback boolean (optional) - Whether a document whose URL has no origin of its own (about:, blob:, data: or filesystem:) is matched by the origin of the frame that created it instead of by its URL. This includes documents with an opaque origin, such as a data: frame or a sandboxed <iframe srcdoc>. For example, an about:blank popup opened by https://example.com/page is matched as https://example.com/. When set, every pattern in matches must have a path of /*. Only applies to frame contexts. Default is false.