Crossgeposted von: https://feddit.de/post/1185964

Please excuse my sub-par JavaScript, I am a backend dev.

All you need to do is paste this into Tampermonkey and enter your username and your instance url (on two locations).

This is not showing other users’ scores and it doesn’t make your score visible to anyone else than yourself.

So no need for karma farming. This is just for fun.

// ==UserScript==
// @name         Lemmy score
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Shows your total post/comment score at the top right.
// @author       You
// @match        ENTER INSTANCE URL HERE (leave the asterisk after the URL)*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=feddit.de
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    var USERNAME = "ENTER USERNAME HERE";
    var INSTANCE_URL = "ENTER INSTANCE URL HERE";

    var totalScore = 0;
    var currentPage = 1;

    function postResult() {
        var navbar = document.getElementsByClassName("collapse navbar-collapse")[0];
        console.log(navbar);
        var ul = document.createElement("ul");
        ul.className = "navbar-nav";
        ul.id = "karma-ul";
        var li = document.createElement("li");
        li.id = "karma-li";
        li.className = "nav-item";
        li.innerHTML = '<div id="karma-div">' + totalScore + '</div>'
        navbar.appendChild(ul);
        ul.appendChild(li);
    }
    function callPage() {
        var userRequest = new XMLHttpRequest();
        userRequest.onreadystatechange = function () {
            if (this.readyState == 4) {
                if (this.status == 200 ) {
                    var res = JSON.parse(this.responseText);
                    if (res.posts.length==0 && res.comments.length==0) {
                        postResult();
                    } else {
                        totalScore += res.posts.map(x => x.counts.score).reduce((partialSum, a) => partialSum + a, 0);
                        totalScore += res.comments.map(x => x.counts.score).reduce((partialSum, a) => partialSum + a, 0);
                        currentPage++;
                        callPage();
                    }
                }
            }
        }
        userRequest.open("GET", INSTANCE_URL + "/api/v3/user?username=" + USERNAME + "&limit=50&page=" + currentPage, true);
        userRequest.send();
    }

    setTimeout(callPage, 200);
})();

  • Square Singer@feddit.deOP
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    The person_view post_score/comment_score is buggy. I have no idea why, but it’s wildly different from the sum of all post/comment scores. Here’s a bug report for that: https://github.com/LemmyNet/lemmy/issues/3393

    The person_view seems to be independent of the pages. So my script iterates through all the pages, sums up the scores and returns that.

    But even that is pretty inaccurate, since Lemmy has quite severe syncing issues. Here’s the bug report for that: https://github.com/LemmyNet/lemmy/issues/3101

    Apparently, any updates are only sent once and not retried. If the target instance isn’t reachable when the update was sent (e.g. down for maintenance or overloaded), the update gets lost. This means, if you view the same post on multiple instances, they might have different scores, comments might be completely missing and edits/deletes might not have happened on the other instance.

    So take your score with a mountain of salt ;)

      • Square Singer@feddit.deOP
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        No problem! Yeah, sadly Lemmy is still rather buggy when it comes to anything more complicated. I guess, until 3 weeks ago, the syncing wasn’t much of an issue, but now that many instances are operating at or above capacity, everything’s become much more complicated. And in the end, there are only two full-time devs on Lemmy.