Roblox Friend Requests Counter

Count total friend requests on Roblox

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Roblox Friend Requests Counter
// @namespace    https://mopsfl.de
// @version      1.2
// @description  Count total friend requests on Roblox
// @author       mopsfl
// @match        *://*.roblox.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @license MIT
// ==/UserScript==

(async function() {
    'use strict';

    function waitForElm(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                return resolve(document.querySelector(selector));
            }

            const observer = new MutationObserver(mutations => {
                if (document.querySelector(selector)) {
                    observer.disconnect();
                    resolve(document.querySelector(selector));
                }
            });

            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        });
    }

    async function fetchFriendRequests(cursor = "") {
        const url = `https://friends.roblox.com/v1/my/friends/requests?sortOrder=Desc&limit=100${cursor ? `&cursor=${cursor}` : ""}`;

        const response = await fetch(url, {
            method: "GET",
            credentials: "include",
        });

        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }

        return response.json();
    }

    async function main() {
        const cachedData = GM_getValue("friendRequestCount", null);
        const now = new Date().getTime();

        if (cachedData && now - cachedData.t < 5 * 60 * 1000) {
            console.log(`Using cached value: ${cachedData.v}`);
            waitForElm("#nav-friends > div.dynamic-width-item.align-right > span").then(el => {
                el.innerText = cachedData.v;
            });

            waitForElm("#friends-web-app > div > div.rbx-tabs-horizontal.rbx-scrollable-tabs-horizontal > div > div > div.friends-content.section > div > div > h2").then(el => {
                el.innerText = `Requests (${cachedData.v})`;
            });
            return;
        } else {
            waitForElm("#nav-friends > div.dynamic-width-item.align-right > span").then(el => {
                el.innerText = cachedData.v;
            });

            waitForElm("#friends-web-app > div > div.rbx-tabs-horizontal.rbx-scrollable-tabs-horizontal > div > div > div.friends-content.section > div > div > h2").then(el => {
                el.innerText = `Requests (${cachedData.v})`;
            });
        }

        console.log("Fetching all friend requests! This may take a few seconds...")

        let length = 0;
        let cursor = "";

        try {
            do {
                const response = await fetchFriendRequests(cursor);
                length += response.data.length;
                cursor = response.nextPageCursor;
            } while (cursor);

            GM_setValue("friendRequestCount", { v: length, t: now });

            waitForElm("#nav-friends > div.dynamic-width-item.align-right > span").then(el => {
                el.innerText = length;
            });

            waitForElm("#friends-web-app > div > div.rbx-tabs-horizontal.rbx-scrollable-tabs-horizontal > div > div > div.friends-content.section > div > div > h2").then(el => {
                el.innerText = `Requests (${length})`;
            });

            console.log(`Total friend requests: ${length}`);
        } catch (error) {
            console.error("Error fetching friend requests:", error);
        }
    }

    main();
})();