Skip to content

Instantly share code, notes, and snippets.

@colindean
Last active October 18, 2017 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colindean/d8021bf42167730a40661086ad26ab2b to your computer and use it in GitHub Desktop.
Save colindean/d8021bf42167730a40661086ad26ab2b to your computer and use it in GitHub Desktop.
Lobsters user list fun
// drop this into your console while on the users page
// https://lobste.rs/u
var pattern = /\((\d*)\)/;
var users_by_score_desc = Array.from(users).map(function(e){
var score_match = pattern.exec(e.children[0].nextSibling.textContent.trim());
var score = score_match === null ? NaN : parseInt(score[1]);
var name = e.children[0].getAttribute("name");
return {name: name, score: score }
}).sort(function(ths,that){
var me = ths.score;
var them = that.score;
if(me == them) return 0; else if (me > them) return -1; else if (me < them) return 1
})
// ==UserScript==
// @name Display Lobsters point position
// @namespace http://cad.cx/
// @version 1.0
// @description Shows point position on users listing
// @author Colin Dean
// @match https://lobste.rs/u
// @grant none
// ==/UserScript==
(function() {
'use strict';
var username_area = document.getElementsByClassName("headerlinks")[1].children[1];
var original_username_content = username_area.textContent;
username_area.textContent = username_area.textContent + " [loading place…]";
var current_username = username_area.textContent.split(" ")[0];
var pattern = /\((\d*)\)/;
var all_users = document.getElementsByTagName("li", document.getElementsByClassName("tree"));
var users_by_score_desc = Array.from(all_users).map(function(e){
var name = e.children[0].getAttribute("name");
var score_text = e.children[0].nextSibling.textContent.trim();
console.log("Cleaning up " + name + " with [" + score_text + "]");
var score_match = pattern.exec(score_text);
var score = score_match === null ? NaN : parseInt(score_match[1]);
return {name: name, score: score };
}).sort(function(ths,that){
var me = ths.score;
var them = that.score;
if(me == them) return 0; else if (me > them) return -1; else if (me < them) return 1;
});
var position = users_by_score_desc.findIndex(function(user){ return user.name === current_username; });
var num_users = users_by_score_desc.length;
console.log("Setting position to " + position + "/" + num_users);
username_area.textContent = original_username_content + " #" + position + " out of " + num_users;
var list_parent = document.getElementsByClassName("tree")[0].parentNode;
var ordered_list = document.createElement("ol");
users_by_score_desc.forEach(function(user){
var text = document.createTextNode(user.name + " (" + user.score + ")");
var li = document.createElement("li");
if(current_username === user.name) {
li.setAttribute("style","font-weight:bold");
}
li.appendChild(text);
ordered_list.appendChild(li);
});
list_parent.appendChild(ordered_list);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment