It also seems to work if you replace api.onPageChange(() => {
with $( document ).ready(function() {
and then the script won’t load on every page, but I never know for sure where I need api.onPageChange(() => { .
<script type="text/discourse-plugin" version="0.8">
api.onPageChange(() => {
// Retrieve current user details
const currentUser = api.getCurrentUser();
if (currentUser) {
// List of group names you want to exclude from script execution
const excludedGroups = ['group1', 'group2'];
// Get the user's groups
const userGroups = currentUser.groups.map(group => group.name);
// Check if the user belongs to any excluded group
const isExcluded = excludedGroups.some(group => userGroups.includes(group));
if (!isExcluded) {
// Your custom script logic here
console.log('Script is executed for this user.');
// Example: trigger any custom logic for non-excluded users
// customScriptFunction();
} else {
console.log('User is in an excluded group, script not executed.');
}
}
});
</script>