Il problema era che non riuscivo a capire come passare una variabile da getGroupInfo() a api.onPageChange(() => {
Il codice qui sotto risolve il problema utilizzando un cookie e dovrebbe aggiungere e rimuovere automaticamente la classe "nome-del-gruppo-corrente" dall’ID div principale per qualsiasi gruppo di cui fai parte.
<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);
var groupName = myObj.group.name;
if (groupName) {
document.getElementById("main").classList.add(groupName);
$.cookie('groupCookie', groupName, { expires: 7, path: '/' });
}
}
};
var url = window.location.href;
xmlhttp.open("GET", url + ".json", true);
xmlhttp.send();
}
api.onPageChange(() =>{
var group = $.cookie('groupCookie');
document.getElementById("main").classList.remove(group);
$.cookie('groupCookie', 'false', { expires: -1, path: '/' })
getGroupInfo();
});
</script>