54 points · by polm23 · 5y ago · 35 comments
> What happens when you combine the fact that part after the : for CSS Custom Properties doesn’t need to be valid CSS with window.getComputedStyle()? You get a way of passing data – including Arrays, Objects, and even JSON – from CSS to JavaScript.
The first article proposes the possibility of sending updated variables via a CDN, and the linked one shows a CSS variable being parsed as JSON and then executed by the Javascript engine.
There's probably some fun and high jinks to be had there…
[1] https://www.bram.us/2020/03/30/pass-data-from-css-to-javascr...
I haven't tried this, but this should have the same effect:
.sneaky-class:before {
content: '{"tracking_code": "abc"}';
display: none;
}
const trackingPayload = JSON.parse(
window.getComputedStyle(
document.querySelector(".sneaky-class"),
":before")
.getPropertyValue("content")
);The only issue I can think of is mixing code/data with styling information. Otherwise there is no "issue" per se.
> What does this circumvent that cannot be done with pure JS?
Nothing, it is just JS after all, and the idea that you can store data in other things than JS variables is not new (jQuery days we stored data in various attributes in the DOM to keep track of state). You could also store the same data in <title/> tag and get the same results. Not sure what the use case is, except it's a fun little hack.
Maybe this will spawn some new JS framework, nothing would surprise me anymore.
Except for enjoying a web without styles, there is no reason to disable CSS (together with disabling JS). You're not gonna prevent any more JS fudgery by having JS and CSS disabled compared to just JS disabled.
classic cross domain ad tracking trick. BUT seems like chrome is trying to limit by keying caches to the domain. BUT BUT maybe not in cross domain web workers?
IDK though might be some interesting ideas. *edit maybe just cname cloak like are doing to get around itp
poc: https://github.com/dillondoyle/css_cache https://dillondoyle.github.io/css_cache/index.html
- basic js generates a css file with a random uuid and an example just my name. puts into a css var as a JSON object
- sets long term cache headers AND a matching etag (same as uuid)
- etag is a classic tracking trick but this is really cool it makes it accessible as state in JS client side
- index.html prints/logs the css var- if css file is cached the uuid you see is static. no cookies needed
- i was also going to add just an array of recent request domains, last and first session. would help test the chrome cache keying. could even put a jwt in there
it's just a cool hack I never thought of. it's really cool how one thing can trigger an 'omg that's so obvious' brain fart.
Like even before CSS vars you could have done this just set really specific css width 12.999933/122.3333 or something then measure the div you set or maybe simpler just a random "'uuid_"+the_uuid_var+"'" classname add to body and then read if it exists with js. But I never thought of it until I saw this article!!! Same with just generating a JS file with static var which I've played with but never deployed. though I might guess a css file is cached longer? idk i don't have proof of that just a guess could be wrong
could maybe extend this to get around chrome cache keying with cname cloaking *edit actually maybe more likely that doesn't make sense idk i'd have to think about it
or just reading their docs last night about their new policy (only a couple months old) it looks like webworkers aren't enforced yet and you can explicitly control & cache a file with JS in a webworker
In fairness the author talks about using them very dynamically (rip cache), which is a bit different, but still its just an extension of the basic idea of setting a variable.
So, the more things you can move over into CSS, the more things exist in this beautiful pure context that's highly debuggable thanks to modern browser tooling (and that's not even getting into the performance advantages vs doing things in JS).
Put simply: it's really exciting every time CSS gets a new "real programming language" feature, even if these things are basic and familiar in other programming contexts. In this case we're not really getting a "new" feature, but people are used to thinking of CSS variables as like, a theming mechanism. I for one had never thought to use them as an API data format. The author is probing the boundaries of what's possible to do with them, and that's exciting and interesting.
As an aside: there's also a huge community that loves to just push and abuse HTML/CSS as far as they can for the sport of it. CSS-Tricks and its sibling-site CodePen are at the center of that community. To people like that, the idea of an API endpoint that uses a stylesheet to relay raw data for arbitrary usage is pretty cool.
And by "state" I mean it in its proper sense: something where CSS itself can mutate its own state. The OP calls CSS custom properties "state", but they're really just "constants" in the context of CSS itself (they can only be mutated by JS).
You'd have a separate theme.css that defined the new variables. Because of CSS variables you can keep the main style.css and all the markup the same.