Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unified Post Point Counter for Twitter. (likes + rt*2 + replies*5) Not a good post counter, rather a popularity counter.
// ==UserScript==
// @name TwitterUniPoints
// @namespace https://skiilaa.me/
// @version 1.0
// @description Unified Tweet Points
// @author skiilaa
// @match *://twitter.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var lastCount = 0;
(function($) {
var origAppend = $.fn.append;
$.fn.append = function () {
return origAppend.apply(this, arguments).trigger("append");
};
$.fn.exists = function(){ return this.length > 0; }
})(window.jQuery);
function tweetCounterParse(x) {
if (x.endsWith("K")) {
return parseFloat(x.substring(0, x.length - 1)) * 1000;
} else if (x.endsWith("M")) {
return parseFloat(x.substring(0, x.length - 1)) * 1000000;
} else if (x == "") {
return 0;
} else {
return parseInt(x);
}
}
function updatePoints() {
var streamOl = document.querySelector("#stream-items-id");
if (!streamOl) {
console.log("[TwitterUniPoints] Couldn't get tweet list.");
return;
}
if (lastCount == streamOl.children.length) {
return;
}
lastCount = streamOl.children.length
var tweets = Array.from(streamOl.children).map(x => x.dataset.itemId);
tweets = tweets.filter(x => x != null);
tweets = tweets.map(x => streamOl.querySelector(`li[data-item-id="${x}"]`));
var tweetData = tweets.map(x => {
var data = { id: x.dataset.itemId };
var RTs = x.querySelector(".js-actionRetweet .ProfileTweet-actionCountForPresentation").innerText;
var likes = x.querySelector(".js-actionFavorite .ProfileTweet-actionCountForPresentation").innerText;
var replies = x.querySelector(".js-actionReply .ProfileTweet-actionCountForPresentation").innerText;
data.rt = tweetCounterParse(RTs);
data.fav = tweetCounterParse(likes);
data.rpl = tweetCounterParse(replies);
return data;
});
tweetData.forEach(x => {
if (streamOl.querySelector(`li[data-item-id="${x.id}"] .ProfileTweet-actionList .ProfileTweet-action-twitterUniPoints`)) return;
var p = x.fav + (x.rt * 2) + (x.rpl * 5);
var pointDisplay = document.createElement("div");
pointDisplay.classList.add("ProfileTweet-action");
pointDisplay.classList.add("ProfileTweet-action-twitterUniPoints");
var pointDisplaySpan = document.createElement("span");
pointDisplaySpan.classList.add("ProfileTweet-actionCount");
pointDisplaySpan.innerText = p;
pointDisplay.appendChild(pointDisplaySpan)
streamOl.querySelector(`li[data-item-id="${x.id}"] .ProfileTweet-actionList`).appendChild(pointDisplay);
})
}
function updateOverlayPoints() {
var poBody = document.getElementsByClassName("permalink-tweet")[0];
if (!poBody) return;
var data = {};
var RTs = poBody.querySelector(".js-actionRetweet .ProfileTweet-actionCountForPresentation").innerText;
var likes = poBody.querySelector(".js-actionFavorite .ProfileTweet-actionCountForPresentation").innerText;
var replies = poBody.querySelector(".js-actionReply .ProfileTweet-actionCountForPresentation").innerText;
data.rt = tweetCounterParse(RTs);
data.fav = tweetCounterParse(likes);
data.rpl = tweetCounterParse(replies);
if (poBody.querySelector(`.ProfileTweet-actionList .ProfileTweet-action-twitterUniPoints`)) return;
var p = data.fav + (data.rt * 2) + (data.rpl * 5);
var pointDisplay = document.createElement("div");
pointDisplay.classList.add("ProfileTweet-action");
pointDisplay.classList.add("ProfileTweet-action-twitterUniPoints");
var pointDisplaySpan = document.createElement("span");
pointDisplaySpan.classList.add("ProfileTweet-actionCount");
pointDisplaySpan.innerText = p;
pointDisplay.appendChild(pointDisplaySpan)
poBody.querySelector(`.ProfileTweet-actionList`).appendChild(pointDisplay);
}
var lastStreamHTML = "";
setInterval(() => {
var x = document.getElementById("stream-items-id").innerHTML;
if ( x != lastStreamHTML ) {
lastStreamHTML = x;
updatePoints();
}
}, 500);
var lastOverlayHTML = "";
setInterval(() => {
var x = document.getElementsByClassName("permalink-tweet")[0];
if ( x != lastOverlayHTML ) {
lastOverlayHTML = x;
updateOverlayPoints();
}
}, 500);
window.$("#stream-items-id").bind("append", updatePoints);
window.$("#permalink-overlay-body").bind("DOMSubtreeModified", updateOverlayPoints);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.