Simple Script to Show / Hide Content Based on Group

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.

4 Likes

@Falco

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