Estimate Post Score

Recreates old RES feature which shows individual up/downvotes when possible. Votes are calculated on post score and percentage.

当前为 2025-02-22 提交的版本,查看 最新版本

// ==UserScript==
// @name        Estimate Post Score
// @namespace   greenwithenvy
// @description Recreates old RES feature which shows individual up/downvotes when possible. Votes are calculated on post score and percentage.
// @include     *.reddit.com/*
// @include     *./reddit.com/user/*/submitted
// @version     3
// @license     MIT
// @grant       none
// ==/UserScript==

window.addEventListener('load', addUpvoteDownvoteInfo);

async function addUpvoteDownvoteInfo() {
    const linkListing = document.querySelector(".linklisting") || document.querySelector(".Post")?.parentElement;
    if (!linkListing) return;

    const linkDivs = linkListing.getElementsByClassName("link");

    const promises = Array.from(linkDivs).map(async (linkDiv) => {
        const commentsLink = linkDiv.querySelector(".comments");
        if (!commentsLink) return;

        const commentsPage = await httpGet(`${commentsLink.href}?limit=1&depth=1`);

        const scoreSection = /<div class=(\"|\')score(\"|\')[\s\S]*?<\/div>/.exec(commentsPage);
        if (!scoreSection) return;

        const scoreMatch = /<span class=(\"|\')number(\"|\')>([\d\,\.]*)<\/span>/.exec(scoreSection[0]);
        if (!scoreMatch) return;

        const score = parseInt(scoreMatch[3].replace(',', '').replace('.', ''), 10);
        const upvotesPercentageMatch = /\((\d+)\s*\%[^\)]*\)/.exec(scoreSection[0]);
        if (!upvotesPercentageMatch) return;

        const upvotesPercentage = parseInt(upvotesPercentageMatch[1], 10);
        const upvotes = calcUpvotes(score, upvotesPercentage);
        const downvotes = upvotes !== "--" ? score - upvotes : "--";

        updateTagline(linkDiv, upvotes, downvotes);
    });

    await Promise.all(promises);
}

function calcUpvotes(score, upvotesPercentage) {
    if (score === 0) return "--";
    return Math.round(((upvotesPercentage / 100) * score) / (2 * (upvotesPercentage / 100) - 1));
}

function updateTagline(linkDiv, upvotes, downvotes) {
    const taglineParagraph = linkDiv.querySelector(".tagline") || linkDiv.querySelector(".Post div[data-test-id='post-content']")?.querySelector(".tagline");
    if (!taglineParagraph) return;

    let upvoteSpan = taglineParagraph.querySelector(".res_post_ups");
    let downvoteSpan = taglineParagraph.querySelector(".res_post_downs");

    if (!upvoteSpan || !downvoteSpan) {
        const updownInfoSpan = document.createElement("span");

        upvoteSpan = createVoteSpan("res_post_ups", upvotes, "#FF8B24");
        downvoteSpan = createVoteSpan("res_post_downs", downvotes, "#9494FF");

        updownInfoSpan.append("(", upvoteSpan, "|", downvoteSpan, ")");
        taglineParagraph.insertBefore(updownInfoSpan, taglineParagraph.firstChild);
    } else {
        upvoteSpan.textContent = upvotes;
        downvoteSpan.textContent = downvotes;
    }
}

function createVoteSpan(className, textContent, color) {
    const span = document.createElement("span");
    span.classList.add(className);
    span.style.color = color;
    span.textContent = textContent;
    return span;
}

async function httpGet(url) {
    const response = await fetch(url);
    return response.text();
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址