Simple Script to Show / Hide Content Based on Group

AI-generated summary

The discussion revolves around a simple script to show or hide content based on group membership. 8BIT wants to hide an ad script from users in the “patrons” group and provided an initial script that doesn’t produce the desired result. Falco suggested an alternative script using the api.getCurrentUser() method, which works but requires an extra request on every page load. nixie inquired about the possibility of inserting CSS code and confirmed that the script can be pasted in the <head> section.

Maybe a little spot-check here…

I was given this for advertising:

<script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
    <script>
(function(){
    if(typeof _bsa !== 'undefined' && _bsa) {
        _bsa.init('fancybar', 'CKYDV2QM', 'placement:thebitcoinpub');
    }
})();
</script>

And I’d like to show this code to everyone except those who are in the Group “patrons” (using the Patreon Plugin).

Does this look right?

<script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
    <script>
const user = Discourse.User.current(); 
(function(){
    if  ((typeof _bsa !== 'undefined' && _bsa && user === null) || typeof _bsa !== 'undefined' && _bsa && user !== null && user.primary_group_name !== "patrons") {
        _bsa.init('fancybar', 'CKYDV2QM', 'placement:thebitcoinpub'); }

    }
  }
})();
</script>

This doesn’t seem to give the result that I’m looking for… is there a better way of doing this?

Thanks in advance!

<script type="text/discourse-plugin" version="0.8">
    var u = api.getCurrentUser();
    if (u !== null) {
        u.findDetails().then(function(u) {
            if (!u.groups.some((g) => g.name === 'patrons')) {

                // user isn't a patron, insert code here

            }
        });
    }
</script>

This will work, but will be an extra request on every page load.

@Falco

  1. Can we insert even CSS code there?
  2. This can be code-block can be pasted in the section - correct?