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); })();
Find the perfect side table design for your bedroom at Chahyay. Explore a variety of stylish and functional bedside tables to enhance your space.
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?
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.
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 😅
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.
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 ;)
I see, I figured as much already. Thanks for the link to the issue.
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.
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
Wasn’t really looking for a code review. This was just a quick and dirty hack. I didn’t even intend to post it here, but someone where I posted it first insisted.
There are a lot of potential optimisations, but the time budget that I wanted to sink into this script was a maximum of 15 minutes.
thanks for sharing :)