È un metodo temporaneo ma utile. Forse vogliono migliorarlo, lascio il codice qui
Sito web di prova: 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("Utente non collegato");
return;
}
fetch(`/u/${currentUser.username}.json`)
.then(response => response.json())
.then(data => {
console.log("Dati Gamification:", data);
let score = data?.user?.gamification_score ?? 0;
// Se la casella del punteggio esiste già, non aggiungerla di nuovo
if (document.querySelector("#gamification-score")) return;
// Trova il contenitore .contents dell'header
let headerContents = document.querySelector(".d-header>.wrap .contents");
if (headerContents) {
// Crea la casella del punteggio
let scoreContainer = document.createElement("a");
scoreContainer.id = "gamification-score";
scoreContainer.classList.add("gamification-box");
scoreContainer.href = "website";
scoreContainer.target = "_blank"; // Apri in una nuova scheda
// Icona dorata
let icon = document.createElement("span");
icon.innerText = "T";
icon.classList.add("gold-icon");
// Testo del punteggio
let scoreText = document.createElement("span");
scoreText.innerText = `${score}`;
scoreText.classList.add("gold-text");
// Aggiungi icona e testo alla casella
scoreContainer.appendChild(icon);
scoreContainer.appendChild(scoreText);
// Aggiungi la casella all'header
headerContents.appendChild(scoreContainer);
}
})
.catch(error => console.error("Errore nel recupero del punteggio gamification:", error));
});
});