-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit-highlight-posts-based-on-scores.user.js
49 lines (43 loc) · 1.64 KB
/
reddit-highlight-posts-based-on-scores.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ==UserScript==
// @name Reddit Highlight Posts Based on Scores
// @namespace metapone
// @version 0.1.1
// @description Highligh posts above a certain score
// @author metapone
// @license GPL-2.0-only; https://opensource.org/licenses/GPL-2.0
// @homepage https://github.com/metapone/userscript-collection
// @supportURL https://github.com/metapone/userscript-collection/issues
// @noframes
// @match *://*.reddit.com/*
// @grant none
// ==/UserScript==
if (window.location.href.indexOf('comments') !== -1) return;
const threshold = 500; // Minimum score to highlight
const color = "yellow"; // Highlight CSS color
function callback(mutationsList) {
for (let mutation of mutationsList) {
for (let addedNode of mutation.addedNodes) {
highlightPosts(addedNode);
}
}
}
function parseScore(score) {
const temp = parseFloat(score, 10);
return score.indexOf('k') === -1 ? temp : temp * 1000;
}
function highlightPosts(node) {
node.querySelectorAll("[id^='upvote-button']").forEach(e => {
if (parseScore(e.nextSibling.textContent) > threshold) {
e.parentNode.parentNode.style.background = color;
e.parentNode.parentNode.nextSibling.style.background = color;
}
});
}
if (window.location.href.indexOf('old.reddit.com') !== -1) {
document.querySelectorAll(".midcol .score.unvoted").forEach(e => (parseInt(e.textContent, 10) >= threshold) && (e.parentNode.parentNode.style.background = color));
} else {
highlightPosts(document);
const targetNode = document.querySelector(".Post").parentNode.parentNode.parentNode;
const observer = new MutationObserver(callback);
observer.observe(targetNode, { childList: true });
}