X Block List Counter

Counts the number of accounts in your X block list, handling pagination

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         X Block List Counter
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Counts the number of accounts in your X block list, handling pagination
// @author       adamlproductions
// @match        https://x.com/settings/blocked/all
// @grant        none
// @license      MIT
// ==/UserScript==
// jshint        esversion: 8

(function() {
    'use strict';
    let textColor = '#FFFFFF';

    async function countBlockedAccounts() {
        let blockedCount = 0;
        let seenUserIds = new Set();
        let lastHeight = document.body.scrollHeight;
        let noNewContentCount = 0;

        const counterDiv = document.createElement('div');
        counterDiv.style.position = 'fixed';
        counterDiv.style.top = '10px';
        counterDiv.style.right = '10px';
        counterDiv.style.color = textColor;
        counterDiv.style.padding = '10px';
        counterDiv.style.border = `1px solid ${textColor}`;
        counterDiv.style.zIndex = '1000';
        counterDiv.innerText = 'Counting blocked accounts...';
        document.body.appendChild(counterDiv);

        while (noNewContentCount < 3) {
            if(window.location.href != 'https://x.com/settings/blocked/all'){
                console.log('X block list counting aborted.');
                break;
            }
            const blockedAccounts = document.querySelectorAll('div[data-testid="cellInnerDiv"]');

            for (const account of blockedAccounts) {
                const userLink = account.querySelector('a[href*="/"]');
                const userId = userLink ? userLink.getAttribute('href') : null;
                if (userId && !seenUserIds.has(userId)) {
                    seenUserIds.add(userId);
                    blockedCount++;
                }
            }

            counterDiv.innerText = `Blocked accounts: ${blockedCount}`;

            window.scrollTo(0, document.body.scrollHeight);
            await new Promise(resolve => setTimeout(resolve, 1500));

            const newHeight = document.body.scrollHeight;
            if (newHeight === lastHeight) {
                noNewContentCount++;
            } else {
                noNewContentCount = 0;
            }
            lastHeight = newHeight;
        }

        counterDiv.innerText = `Total blocked accounts: ${blockedCount}`;
        console.log(`Total blocked accounts: ${blockedCount}`);
    }

    window.addEventListener('load', () => {
        let sum = rgbSum(window.getComputedStyle(document.body).backgroundColor);
        if(sum != 0){
            textColor = '#000000';
        }
        countBlockedAccounts().catch(error => {
            console.error('Error counting blocked accounts:', error);
            const errorDiv = document.createElement('div');
            errorDiv.style.position = 'fixed';
            errorDiv.style.top = '50px';
            errorDiv.style.right = '10px';
            errorDiv.style.color = textColor;
            errorDiv.style.padding = '10px';
            errorDiv.style.border = '1px solid #f00';
            errorDiv.style.zIndex = '1000';
            errorDiv.innerText = 'Error counting blocked accounts. Check console for details.';
            document.body.appendChild(errorDiv);
        });
    });

    function rgbSum(rgb) {
        const rgbValues = rgb.match(/\d+/g);
        const r = parseInt(rgbValues[0]).toString(16).padStart(2, '0');
        const g = parseInt(rgbValues[1]).toString(16).padStart(2, '0');
        const b = parseInt(rgbValues[2]).toString(16).padStart(2, '0');

        return r + g + b;
    }
})();