Unique CSS class for Group pages?

Here is an ugly way to do it. Adding the class is easy. This will add a groupname to your main id, where groupname = whatever the name of the group is, ie <section id="main" class="ember-application groupname">

<script type="text/discourse-plugin" version="0.8">
        function getGroupInfo() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                var myObj = JSON.parse(this.responseText);
                if (myObj.group.name) {
                    document.getElementById("main").classList.add(myObj.group.name);
                }
            }
        };
        var url = window.location.href;
        xmlhttp.open("GET", url + ".json", true);
        xmlhttp.send();    			
    }
    api.onPageChange(() =>{
        getGroupInfo();
    });
</script>

The problem is the class stays there once you navigate away. We need a way to remove that class when we leave the page. There is probably an easy way to do this but I don’t really know js so I’m not clever enough to figure it out. Hopefully you can figure out a better way to do it, but the ugly way to do it is to change the end of the script to

    api.onPageChange(() =>{
        document.getElementById("main").classList.remove("group1");
        document.getElementById("main").classList.remove("group2");
        document.getElementById("main").classList.remove("group3");
        getGroupInfo();
    });

This will remove any possible groupname class you’ve added. But it seems like a really ugly way to do it, and you have to change the code any time you add a new group.

1 Like