document.fragmentDirective API
- Category
- DOM
- Type
- New or changed feature
- Status
- Proposed (Chrome Proposed)
- Intent stage
- Start incubating
Summary
Enables interacting with features using the URL fragment directive, such as text directives (a.k.a. scroll-to-text links, e.g. example.com#:~:text=foo). This new API allows authors to work with text directives (and newly proposed directive types in the future), allowing them to better integrate their pages with "scroll-to-text" highlights and easily generate their own "scroll-to-text" links.
Motivation
We’ve seen a few use cases crop up for working with text fragments: Being able to attach commentary/UI to a targeted text range Tools that wish to create text directive links of their own find it difficult since the rules around matching text are rather complicated. This API allows such tools/pages to give the browser a Range and receive a text directive string in return. Pages that use a cross-origin iframe to host content and want to enable text highlighting can coordinate between the embedder and embeddee by passing the text directive string to the embeddee and having it set the text directive on itself. The text directive is currently exposed (accidentally) on the performance API which pages can to read to achieve this behavior. An explicit API would allow us to remove the performance API hack which may enable more privacy-sensitive features to use the fragment directive mechanism.
Standards & signals
- Firefox: No signal
- Safari: No signal
- Web developers: No signals
- Tracking bug: https://crbug.com/1214791
Ecosystem
- Web Platform Tests: https://wpt.fyi/results/?q=document-fragmentdirective-api
- Chrome Platform Showcase: https://chrome-platform-showcase.paulkinlan-ea.deno.net/features
- GenDN reference: https://gendn.paulkinlan-ea.deno.net/features
- chrome.dev demos search: https://www.google.com/search?q=site%3Achrome.dev+document.fragmentDirective%20API
- MDN Web Docs: https://developer.mozilla.org/en-US/search?q=document.fragmentDirective%20API
- Baseline status: https://webstatus.dev/?q=document.fragmentDirective%20API
Explainers: https://github.com/WICG/scroll-to-text-fragment/blob/main/fragment-directive-api.md