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);
})();

  • Tywèle [she|her]@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    1 year ago

    Since the update to v0.18.1-rc.10 (UI: v0.18.1-rc.11) my score got almost cut in half (from ~700 to 475). It seems to me like it either isn’t counting comment or post score anymore?

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

      The data structure didn’t change, so the script should still count posts and comments as before. My instance is on UI: 0.18.1-rc.10 BE: 0.18.1-rc.9, so one version behind, and my score is still as before.

      Maybe they rolled back some part of the DB or maybe they fixed the score syncing? I don’t know.

      • Tywèle [she|her]@lemmy.dbzer0.com
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Thanks for looking into it. I thought of looking into the data that gets returned by the API directly but now I’m even more confused 😅

        Screenshot of the API result of the scores for posts and comments

        According to this I should have a score of 230? But my current score is 661. The results of the different pages (the page parameter in the API call) of my API results are only different in the comments that are returned.

        • 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.

  • PrettyFlyForAFatGuy@lemmy.ml
    link
    fedilink
    arrow-up
    0
    arrow-down
    6
    ·
    1 year ago

    the this.readyState if statement and the this.status could be one statement. e.g. this.readyState === 4 && this.status === 200

    also note the three equals instead of two. it’s is more performant as it is type specific