Плагин геймификации Discourse в заголовке

Это временный, но полезный метод. Возможно, они захотят его улучшить, я оставляю код здесь.

Тестовый сайт: https://trgala.com

Js

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer("0.8", (api) => {
    api.onPageChange(() => {
        const currentUser = api.getCurrentUser();
        if (!currentUser) {
            console.warn("Пользователь не авторизован");
            return;
        }

        fetch(`/u/${currentUser.username}.json`)
            .then(response => response.json())
            .then(data => {
                console.log("Данные геймификации:", data);
                let score = data?.user?.gamification_score ?? 0;

                // Если блок с очками уже существует, не добавлять его повторно
                if (document.querySelector("#gamification-score")) return;

                // Найти контейнер .contents заголовка
                let headerContents = document.querySelector(".d-header>wrap .contents");
                if (headerContents) {
                    // Создать блок с очками
                    let scoreContainer = document.createElement("a");
                    scoreContainer.id = "gamification-score";
                    scoreContainer.classList.add("gamification-box");
                    scoreContainer.href = "website";
                    scoreContainer.target = "_blank"; // Открыть в новой вкладке

                    // Золотая иконка
                    let icon = document.createElement("span");
                    icon.innerText = "T";
                    icon.classList.add("gold-icon");

                    // Текст с очками
                    let scoreText = document.createElement("span");
                    scoreText.innerText = `${score}`;
                    scoreText.classList.add("gold-text");

                    // Добавить иконку и текст в блок
                    scoreContainer.appendChild(icon);
                    scoreContainer.appendChild(scoreText);

                    // Добавить блок в заголовок
                    headerContents.appendChild(scoreContainer);
                }
            })
            .catch(error => console.error("Ошибка при получении очков геймификации:", error));
    });
});