Skip to content

feat: make collect_css and track_css_responses public to allow more scraping control - #8

Merged
bartveneman merged 2 commits into
mainfrom
feat/freedom-api
Nov 2, 2026
Merged

bartveneman merged 2 commits into
mainfrom
feat/freedom-api

Conversation

@bartveneman

@bartveneman bartveneman commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

After @nucliweb's suggestion. Basically took his idea 1:1 and made it the default in the readme because it's actually a better API that way.

Also decided to expose most of the types because that may be helpful in building up your own system of scraped css.

closes #5

…re scraping control

Co-authored-by: Joan León <1307927+nucliweb@users.noreply.github.com>
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

⚠️ Package Size Increase

📦 Package 📏 Base Size 📏 Source Size 📈 Size Change
@projectwallace/css-scraper 5.7 kB 6 kB +303 B

@nucliweb

nucliweb commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Tried this branch in the page flow of the webperf-snippets CLI: tracking before goto, then throttling (CPU 4x, slow 4G), extra headers and a wait. It works well. collect_css reads a cross-origin stylesheet without CORS, which is exactly what we can't get from the console, and a 6 MB stylesheet in 75 ms without throttling. Nothing here blocks this PR.

Two things I noticed, both already in main, so separate from this:

  • media is declared on CSSLinkSource and CSSImportSource but never filled in, so <link media="print"> and @import url(x.css) print come back without it.
  • A redirected stylesheet, a href with a #fragment and a preload as=style that is never applied come back as import.

Happy to send a PR for media once this lands. Would that be useful?

@bartveneman

Copy link
Copy Markdown
Member Author

Yeah, please do! This library is really early stages and I haven't used it anywhere myself yet, so I'm probably missing heaps of test cases.

@bartveneman
bartveneman merged commit f39b47a into main Nov 2, 2026
10 checks passed
@bartveneman
bartveneman deleted the feat/freedom-api branch November 2, 2026 22:05
bartveneman pushed a commit that referenced this pull request Nov 3, 2026
`CSSLinkSource` and `CSSImportSource` declare `media`, but it was never
set, so a print stylesheet and one that applies came back the same.
Follow-up to #8.

- A `link` source carries the `media` attribute of its `<link>`, as
written.
- An `import` source carries the condition of its `@import` rule, read
from the CSSOM, so as the browser serializes it. The walk follows nested
imports and open shadow roots.
- A rule inside a cross-origin stylesheet stays without `media`: the
page cannot read those rules.
- If one URL is imported twice with different conditions, the last one
wins, since the sources are collapsed by URL.

The fixtures with a media condition now expect it, and a new one covers
`@import` in a `<style>` element. Nested imports and shadow roots go
through the same walk, without a test of their own.

Tried in the page flow of the webperf-snippets CLI, with throttling, on
local pages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let a caller that already navigated collect the CSS of the page

2 participants