Roblox Auto Remove Connection Background Friends etc

Adds a red button to remove friends directly in background without opening new tabs

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Roblox Auto Remove Connection Background Friends etc
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a red button to remove friends directly in background without opening new tabs
// @author       @superdeliciousfries
// @match        https://www.roblox.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const url = window.location.href;

    if(url.includes("/friends") || url.includes("/users")) {

        function removeFriend(userId, btn) {
            const sendRequest = (token) => {
                fetch(`https://friends.roblox.com/v1/users/${userId}/unfriend`, {
                    method: 'POST',
                    credentials: 'include',
                    headers: {
                        'X-CSRF-TOKEN': token
                    }
                }).then(res => {
                    if(res.status === 403) {
                        const newToken = res.headers.get('x-csrf-token');
                        if(newToken) sendRequest(newToken);
                    } else if(res.ok) {
                        btn.textContent = "Removed!";
                        btn.disabled = true;
                        btn.style.backgroundColor = "gray";
                    }
                }).catch(err => console.error(err));
            };

            sendRequest('');
        }

        function createRedButton(userId) {
            const btn = document.createElement("button");
            btn.textContent = "Remove";
            btn.style.backgroundColor = "#e74c3c";
            btn.style.color = "white";
            btn.style.border = "none";
            btn.style.borderRadius = "4px";
            btn.style.padding = "2px 6px";
            btn.style.cursor = "pointer";
            btn.style.marginLeft = "6px";
            btn.style.fontWeight = "bold";
            btn.style.fontSize = "12px";
            btn.style.boxShadow = "0 1px 3px rgba(0,0,0,0.2)";
            btn.style.transition = "all 0.15s ease";

            btn.addEventListener("mouseenter", () => {
                btn.style.backgroundColor = "#c0392b";
                btn.style.transform = "scale(1.05)";
            });
            btn.addEventListener("mouseleave", () => {
                btn.style.backgroundColor = "#e74c3c";
                btn.style.transform = "scale(1)";
            });

            btn.addEventListener("click", e => {
                e.stopPropagation();
                removeFriend(userId, btn);
            });

            return btn;
        }

        function addButtons() {
            const avatarCards = document.querySelectorAll(".avatar-card-content");
            avatarCards.forEach(card => {
                if(card.querySelector(".red-profile-button")) return;
                const link = card.querySelector(".avatar-card-link");
                if(!link) return;
                const match = link.href.match(/\/users\/(\d+)\/profile/);
                if(!match) return;
                const userId = match[1];
                const btn = createRedButton(userId);
                btn.classList.add("red-profile-button");
                const caption = card.querySelector(".avatar-card-caption");
                if(caption) caption.appendChild(btn);
            });
        }

        addButtons();
        const observer = new MutationObserver(addButtons);
        observer.observe(document.body, { childList: true, subtree: true });
    }

})();