問題は、getGroupInfo() から api.onPageChange(() => { へ変数を渡す方法がわからなかったことです。
以下のコードは、クッキーを使ってこれを達成し、あなたが所属する任意のグループに対して、div ID main に "name-of-current-group" クラスを自動的に追加・削除します。
<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>