CSS classes for group membership? (For simplified UI mode)

There might be something that exists in Discourse already - but I personally don’t know of it…

Here is some code I wrote a long time ago and it still appears to work…
… however there is probably a far better way.

It may / may not help you.

For a specific username

<script>
if (window.jQuery) {
    window.jQuery(function ($) {
        var u = Discourse.User.current();
        if (u && (u.username === 'DeanMarkTaylor' || u.username === 'BobTheBuilder')) {
	    // Here is where you could add a CSS class to the HTML / body tag
        }
   });
});
</script>

For a user in a specific group

This has the downside of making an additional HTTP request for the initial page load to get the user’s details.

<script>
if (window.jQuery) {
    window.jQuery(function ($) {
        var u = Discourse.User.current();
        u.findDetails().then(function(u) {
            // Output the groups for the user
            console.log(u.groups);
	     // Here is where you could add a CSS class to the HTML / body tag
    });
});
</script>
10 Likes