# 在头部显示特定组成员的自定义HTML

**URL:** <https://meta.discourse.org/t/show-custom-html-in-header-to-member-of-a-specific-group/142974>\
**Category:** Development\
**Created:** [2020 年2 月 29 日 17:39 UTC](https://meta.discourse.org/t/show-custom-html-in-header-to-member-of-a-specific-group/142974 "2020-02-29T17:39:46Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Paracelsus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/paracelsus/32/164912_2.png) [@Paracelsus](https://meta.discourse.org/u/Paracelsus)\
**Post date:** [2024 年10 月 24 日 17:28 UTC](https://meta.discourse.org/t/show-custom-html-in-header-to-member-of-a-specific-group/142974/7 "2024-10-24T17:28:08Z")

</div>

你好，

我一直在寻找类似的东西，但要排除一些群组，所以我尝试了 ChatGPT，答案是：

> 要在 Discourse 中创建一个 HTML 主题，该主题会触发一个排除特定群组的脚本，您需要使用 Discourse 的主题自定义选项以及 JavaScript 来处理群组排除。Discourse 提供 `api.getCurrentUser()` 来检索用户详细信息，包括他们的群组，这对于有条件地触发您的脚本很有用。
> 
> 以下是您如何在主题或组件的 ` ` 部分实现此目的的示例：

```plaintext
<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>

```

> 说明：
> 
> 1. api.onPageChange()：每当页面更改时运行脚本。
> 2. api.getCurrentUser()：获取当前登录用户的详细信息。
> 3. excludedGroups：列出您要排除的群组。
> 4. userGroups.includes(group)：检查用户是否属于任何排除的群组。
> 5. 条件脚本执行：脚本仅在用户不属于任何排除的群组时运行。
> 
> 您可以将 customScriptFunction() 替换为您需要运行的任何脚本。如果您需要任何进一步的调整，请告诉我！

我的意思是，这让我大吃一惊 🤯。我还没有尝试过，但是 ChatGPT 的代码是正确的吗？

---

_[View the full topic](https://meta.discourse.org/t/show-custom-html-in-header-to-member-of-a-specific-group/142974)._
