완벽합니다 ![]()
다른 환경에서 개발 업무를 해본 경험이 있지만, 테마 컴포넌트를 통해 이것이 가능한지 탐구해 보고 있습니다.
AI의 작은 도움 덕분에
작동하는 것 같지만, 이것이 Discourse에서 올바르게 구현하는 방식인지 100% 확신은 없습니다:
<script type="text/discourse-plugin" version="0.1">
api.onPageChange(() => {
try {
const isTopicPage = /^\/t\//.test(window.location.pathname);
// Only run on topic pages
if (!isTopicPage) return;
const allowedCategories = ['ask-a-question']; // Use category slugs directly
// Access the topic details to get the category slug
const topic = Discourse.__container__.lookup("controller:topic");
const categorySlug = topic && topic.get("model.category.slug");
const isAllowedCategory = categorySlug && allowedCategories.includes(categorySlug);
// Function to hide or show the specific emoji
const toggleReactionEmoji = () => {
const emoji = document.querySelector("[data-reaction='frog']");
if (emoji) {
emoji.style.display = isAllowedCategory ? '' : 'none';
console.log(`Emoji with data-reaction='frog' ${isAllowedCategory ? 'shown' : 'hidden'}.`);
}
};
// Initial check in case the element is already in the DOM
toggleReactionEmoji();
// Set up a mutation observer to hide/show emoji based on category changes
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeType === 1) { // Check if it's an element node
const emoji = node.querySelector("[data-reaction='frog']");
if (emoji) {
emoji.style.display = isAllowedCategory ? '' : 'none';
console.log(`Emoji with data-reaction='frog' found in mutation and ${isAllowedCategory ? 'shown' : 'hidden'}.`);
}
}
});
});
});
// Observe the body for child list changes (e.g., reactions being added)
observer.observe(document.body, { childList: true, subtree: true });
// Clean up the observer when navigating away
api.cleanupStream(() => observer.disconnect());
} catch (error) {
console.error("An error occurred in the emoji toggle script:", error);
}
});
</script>