njofra / Studosi ReactCnt

// ==UserScript==
// @name         Studosi ReactCnt
// @namespace    https://forum.studosi.net/
// @version      0.1
// @description  Show users reacting to posts
// @author       njofra
// @match        https://forum.studosi.net/*
// @grant        none
// @license      MIT
// ==/UserScript==

function get_username(user_id) {
    let user = Object.values(flarum.core.app.store.data.users).find(u => u.data.id == user_id)
    if (!user) return "ID: " + user_id
    return user.data.attributes.username
}

function get_reactions() {
    const reactions = []
    Object.values(flarum.core.app.store.data.reactions).map(r => r.data.attributes).forEach(r => {
        reactions[r.post_id] = reactions[r.post_id] || {}
        reactions[r.post_id][r.identifier] = reactions[r.post_id][r.identifier] || []
        reactions[r.post_id][r.identifier].push(get_username(r.user_id))
    })
    return reactions
}

function add_reactions() {
    let reactions = get_reactions()
    const posts = document.getElementsByClassName("PostStream-item")

    for (let post of posts) {
        if (!(post.getAttribute("data-id") in reactions)) continue
        const buttons = [
            post.getElementsByClassName("Button Button--link Reactions--ShowReactions"),
            post.getElementsByClassName("Button-label Button-emoji-parent"),
            post.getElementsByClassName("emoji button-emoji")
        ]
        for (let button of buttons) {
            if (button.length < 1) continue
            button[0].title = JSON.stringify(reactions[post.getAttribute("data-id")], null, 4)
        }
    }
}

add_reactions()
window.setInterval(add_reactions, 5000);