CC98查询粉丝的变动

自动获取所有粉丝名字

目前為 2025-03-19 提交的版本,檢視 最新版本

// ==UserScript==
// @name         CC98查询粉丝的变动
// @namespace    http://tampermonkey.net/
// @version      6.2.4
// @description  自动获取所有粉丝名字
// @author       Lay
// @match        https://www.cc98.org/usercenter/myfans*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @license      AGPL -3.0 
// @grant        GM_xmlhttpRequest
// ==/UserScript==
(function () {
    'use strict';
 
    // 获取当前用户名
    function getUsername() {
        try {
            const userInfo = JSON.parse(localStorage.getItem('userInfo'));
            return userInfo?.name || '';
        } catch (error) {
            console.error('获取用户名失败:', error);
            return '';
        }
    }
 
    // 创建并插入按钮
    function createButtons() {
        const detectButton = document.createElement('button');
        detectButton.textContent = '开始检测粉丝变动';
        detectButton.style.position = 'fixed';
        detectButton.style.top = '10px';
        detectButton.style.left = '10px';
        detectButton.style.zIndex = 1000;
        detectButton.style.padding = '10px';
        detectButton.style.fontSize = '16px';
        detectButton.style.cursor = 'pointer';
        detectButton.addEventListener('click', () => {
            console.log("按钮点击,开始检测粉丝变动...");
            scrapeAllFans();
        });
        document.body.appendChild(detectButton);
 
        // 创建清空历史记录按钮
        const clearHistoryButton = document.createElement('button');
        clearHistoryButton.textContent = '清空历史记录';
        clearHistoryButton.style.position = 'fixed';
        clearHistoryButton.style.top = '10px';
        clearHistoryButton.style.left = '180px';
        clearHistoryButton.style.zIndex = 1000;
        clearHistoryButton.style.padding = '10px';
        clearHistoryButton.style.fontSize = '16px';
        clearHistoryButton.style.cursor = 'pointer';
        clearHistoryButton.addEventListener('click', () => {
            console.log("清空历史记录...");
            clearHistory();
        });
        document.body.appendChild(clearHistoryButton);
 
        // 创建历史记录显示区域
        const historyContainer = document.createElement('div');
        historyContainer.id = 'historyContainer';
        historyContainer.style.position = 'fixed';
        historyContainer.style.top = '50px';
        historyContainer.style.left = '10px';
        historyContainer.style.zIndex = 1000;
        historyContainer.style.width = '300px';
        historyContainer.style.height = '500px';
        historyContainer.style.overflowY = 'auto';
        historyContainer.style.border = '1px solid #ccc';
        historyContainer.style.backgroundColor = 'white';
        historyContainer.style.padding = '10px';
        historyContainer.style.fontSize = '14px';
        historyContainer.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.1)';
        document.body.appendChild(historyContainer);
 
        // 从localStorage中读取历史记录并显示
        loadHistory();
    }
 
    // 提取总页数
    function getTotalPages() {
        const pageCountElement = document.querySelector('#userCenterPageCount ul');
        let totalPages = 0;
 
        if (pageCountElement) {
            const pageItems = pageCountElement.querySelectorAll('li');
            totalPages = pageItems.length;
            console.log(`总页数: ${totalPages}`);
        } else {
            console.error('无法找到总页数元素');
        }
 
        return totalPages;
    }

    function getStoredFansData() {
        const username = getUsername();  // 获取当前用户名
        return JSON.parse(localStorage.getItem(`${username}_fansData`) || "[]");
    }
 
    // 获取当前页面的粉丝信息
    function getFansFromCurrentPage() {
        let fanElements = document.querySelectorAll('.user-center-myfollowings-user');
        let fans = Array.from(fanElements).map(el => {
            let name = el.querySelector('.user-center-myfollowings-user-id a').textContent.trim();
            let posts = el.querySelector('.user-center-myfollowings-user-posts').textContent.trim();
            let followers = el.querySelector('.user-center-myfollowings-user-fans').textContent.trim();
            return { name, posts, followers };
        });
 
        console.log("当前页粉丝信息:", fans);
        return fans;
    }
 
    // 抓取所有粉丝信息
    async function scrapeAllFans() {
        let allFans = [];
        let totalPages = getTotalPages();
 
        // 如果获取不到总页数,停止执行
        if (totalPages === 0) {
            console.error("无法获取总页数,无法继续执行");
            return;
        }
 
        for (let i = 1; i <= totalPages; i++) {
            console.log(`正在抓取第 ${i} 页...`);
 
            // 模拟点击翻页
            await changePage(i);
 
            // 获取当前页面的粉丝信息
            let fans = getFansFromCurrentPage();
            allFans = allFans.concat(fans);
        }
 
        console.log("所有粉丝信息:", allFans);
        alert(`共抓取到 ${allFans.length} 位粉丝的信息!`);
        loadHistory();
 
        // 获取并存储上次存储的粉丝信息
        let storedFans = getStoredFansData();
 
        // 比较当前粉丝与之前存储的粉丝数据
        compareFansData(allFans, storedFans);
 
        // 存储当前抓取到的粉丝信息
        storeFansData(allFans);
    }
 
    // 根据用户名从localStorage获取粉丝数据


 
    // 将粉丝数据存储到localStorage
    function storeFansData(fansData) {
        const username = getUsername();
        localStorage.setItem(`${username}_fansData`, JSON.stringify(fansData));
    }
 
    // 根据用户名从localStorage获取历史记录
    function getStoredHistoryData() {
        const username = getUsername();
        return JSON.parse(localStorage.getItem(`${username}_historyData`) || "[]");
    }
 
    // 存储历史记录到localStorage
    function storeHistoryData(record) {
        const username = getUsername();
        let history = getStoredHistoryData();
        history.unshift(record);  // 将新的记录添加到数组的最前面
        localStorage.setItem(`${username}_historyData`, JSON.stringify(history));
    }
 
    // 比较当前粉丝数据与存储的粉丝数据
    function compareFansData(currentFans, storedFans) {
        let addedFans = currentFans.filter(currentFan => 
            !storedFans.some(storedFan => storedFan.name === currentFan.name)
        );
        let removedFans = storedFans.filter(storedFan => 
            !currentFans.some(currentFan => currentFan.name === storedFan.name)
        );
 
        let now = new Date();
        let timestamp = now.toLocaleDateString();  // 只记录日期
 
        // 只有在有粉丝变动时才添加记录
        if (addedFans.length > 0 || removedFans.length > 0) {
            // 更新历史记录
            let historyContainer = document.getElementById('historyContainer');
 
            if (addedFans.length > 0) {
                let addedNames = addedFans.map(fan => fan.name).join(', ');
                let record = `${timestamp} 增加粉丝: ${addedNames}`;
                addHistoryRecord(record);
            }
 
            if (removedFans.length > 0) {
                let removedNames = removedFans.map(fan => fan.name).join(', ');
                let record = `${timestamp} 移除粉丝: ${removedNames}`;
                addHistoryRecord(record);
            }
        }
    }
 
    // 将历史记录添加到历史容器
    function addHistoryRecord(record) {
        let historyContainer = document.getElementById('historyContainer');
        historyContainer.innerHTML = `<div>${record}</div>` + historyContainer.innerHTML;
 
        // 保存历史记录到localStorage
        storeHistoryData(record);
    }
 
    // 从localStorage中加载历史记录
    // 从localStorage中加载历史记录
function loadHistory() {
    let historyContainer = document.getElementById('historyContainer');
    let history = getStoredHistoryData();  // 从localStorage获取历史记录

    // 如果没有历史记录,显示暂无历史记录
    if (history.length === 0) {
        historyContainer.innerHTML = '<div>暂无历史记录</div>';
        return;  // 如果没有历史记录,直接返回
    }

    // 如果有历史记录,循环并显示
    historyContainer.innerHTML = '';  // 先清空容器内容
    history.forEach(record => {
        historyContainer.innerHTML += `<div>${record}</div>`;
    });

    console.log('历史记录已加载');
}

 
    // 清空历史记录
    function clearHistory() {
        const username = getUsername();
        localStorage.removeItem(`${username}_historyData`);
 
        // 清空页面上显示的历史记录
        let historyContainer = document.getElementById('historyContainer');
        historyContainer.innerHTML = '';
    }
 
    // 模拟点击分页链接并等待加载
    async function changePage(pageNumber) {
        let paginationLink = document.querySelector(`#userCenterPageCount ul li:nth-child(${pageNumber}) a`);
        if (paginationLink) {
            console.log(`点击分页链接: 第 ${pageNumber} 页`);
            paginationLink.click();
 
            // 等待页面加载完成,可以调整等待时间以确保页面加载完毕
            await new Promise(resolve => setTimeout(resolve, 200));
        } else {
            console.error(`找不到第 ${pageNumber} 页的分页链接`);
        }
    }
 
    // 页面加载后创建按钮
    window.addEventListener('load', () => {
        createButtons();
        loadHistory();  // 在页面加载时显示历史记录
    });
 
})();

QingJ © 2025

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