Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created April 3, 2025 17:33
Show Gist options
  • Save jcsalterego/ca9173d1effb57cb6d476e7f5235cab6 to your computer and use it in GitHub Desktop.
Save jcsalterego/ca9173d1effb57cb6d476e7f5235cab6 to your computer and use it in GitHub Desktop.
ratio.js
function run() {
function valueToLabel(value) {
console.log("value = " + value);
let label = value;
if (value === Infinity) {
label = "∞";
} else if (value >= 1000000) {
label = (value / 1000000).toFixed(1) + 'M';
} else if (value >= 1000) {
label = (value / 1000).toFixed(1) + 'K';
} else {
label = value.toFixed(1);
}
console.info("label = " + label);
return `${label}:1`;
}
function labelToValue(str) {
str = str.trim();
let multiplier = 1;
if (str.includes('K')) {
multiplier = 1000;
str = str.replace('K', '');
}
if (str.includes('M')) {
multiplier = 1000000;
str = str.replace('M', '');
}
return parseInt(parseFloat(str) * multiplier);
}
followersElem = document.querySelector("[data-testid='profileHeaderFollowersButton']");
followersValue = labelToValue(followersElem.querySelectorAll("span")[0].innerText);
followingElem = document.querySelector("[data-testid='profileHeaderFollowsButton']");
followingValue = labelToValue(followingElem.querySelectorAll("span")[0].innerText);
console.log(followersValue);
console.log(followingValue);
ratio = followersValue / followingValue;
console.log(ratio);
let clone = followersElem.cloneNode(true);
followersElem.parentNode.prepend(clone);
let spans = clone.querySelectorAll("span");
spans[0].innerText = valueToLabel(ratio);
spans[1].innerText = " followers-to-following ratio";
followersElem.parentNode.removeChild(followersElem);
followingElem.parentNode.removeChild(followingElem);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment