源码安装
1090197965 / 【云】收集名称工具

// ==UserScript==
// @name         【云】收集名称工具
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  在界面左下角添加收集名称按钮,收集posts-wrapper下的thumbnail链接文字,以及lin08.app的uos_card_username内容
// @author       You
// @license MIT
// @match        https://www.piaoxue77.com/u/*
// @match        https://lin08.app/original_user_list.php*
// @grant        GM_download
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    // 收集池数据
    let collectionPool = [];
    
    // 从存储中加载数据
    function loadCollectionData() {
        const saved = GM_getValue('collectionPool', []);
        collectionPool = saved;
        updateCollectionDisplay();
    }
    
    // 保存数据到存储
    function saveCollectionData() {
        GM_setValue('collectionPool', collectionPool);
    }
    
    // 创建UI元素
    function createUI() {
        // 创建容器
        const container = document.createElement('div');
        container.id = 'collection-tool-container';
        container.style.cssText = `
            position: fixed;
            left: 20px;
            bottom: 20px;
            z-index: 10000;
            background: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 15px;
            border-radius: 8px;
            font-family: Arial, sans-serif;
            font-size: 14px;
            min-width: 300px;
            max-width: 400px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        `;
        
        // 创建按钮
        const collectButton = document.createElement('button');
        collectButton.textContent = '收集名称';
        collectButton.style.cssText = `
            background: #4CAF50;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin-bottom: 10px;
            font-size: 14px;
        `;
        collectButton.addEventListener('click', collectNames);
        
        // 创建导出按钮
        const exportButton = document.createElement('button');
        exportButton.textContent = '导出JSON';
        exportButton.style.cssText = `
            background: #2196F3;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin-bottom: 10px;
            margin-left: 10px;
            font-size: 14px;
        `;
        exportButton.addEventListener('click', exportToJSON);
        
        // 创建清空按钮
        const clearButton = document.createElement('button');
        clearButton.textContent = '清空列表';
        clearButton.style.cssText = `
            background: #f44336;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin-bottom: 10px;
            margin-left: 10px;
            font-size: 14px;
        `;
        clearButton.addEventListener('click', clearCollection);
        
        // 创建导入按钮
        const importButton = document.createElement('button');
        importButton.textContent = '导入JSON';
        importButton.style.cssText = `
            background: #FF9800;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            margin-bottom: 10px;
            margin-left: 10px;
            font-size: 14px;
        `;
        importButton.addEventListener('click', importFromJSON);
        
        // 创建收集列表容器
        const listContainer = document.createElement('div');
        listContainer.id = 'collection-list';
        listContainer.style.cssText = `
            max-height: 200px;
            overflow-y: auto;
            border: 1px solid #555;
            border-radius: 4px;
            padding: 8px;
            background: rgba(255, 255, 255, 0.1);
        `;
        
        // 创建计数器
        const counter = document.createElement('div');
        counter.id = 'collection-counter';
        counter.style.cssText = `
            margin-top: 8px;
            font-size: 12px;
            color: #ccc;
        `;
        
        // 组装UI
        container.appendChild(collectButton);
        container.appendChild(exportButton);
        container.appendChild(importButton);
        container.appendChild(clearButton);
        container.appendChild(listContainer);
        container.appendChild(counter);
        
        document.body.appendChild(container);
    }
    
    // 收集名称
    function collectNames() {
        // 检查当前页面类型
        const currentUrl = window.location.href;
        
        if (currentUrl.includes('lin08.app/original_user_list.php')) {
            collectLin08Names();
        } else if (currentUrl.includes('piaoxue77.com')) {
            collectPiaoxue77Names();
        } else {
            alert('当前页面不支持收集功能');
        }
    }
    
    // 收集lin08.app的名称
    function collectLin08Names() {
        const usernameElements = document.querySelectorAll('.uos_card_username');
        let newCount = 0;
        
        if (usernameElements.length === 0) {
            alert('未找到 uos_card_username 元素');
            return;
        }
        
        usernameElements.forEach(element => {
            const text = element.textContent.trim();
            if (text && !collectionPool.includes(text)) {
                collectionPool.push(text);
                newCount++;
            }
        });
        
        // 排序
        collectionPool.sort();
        
        // 更新显示
        updateCollectionDisplay();
        
        // 保存数据
        saveCollectionData();
        
        // 显示收集结果
        if (newCount > 0) {
            alert(`成功收集 ${newCount} 个新名称,当前总计 ${collectionPool.length} 个`);
        } else {
            alert('没有发现新的名称');
        }
    }
    
    // 收集piaoxue77.com的名称
    function collectPiaoxue77Names() {
        const postsWrapper = document.querySelector('.posts-wrapper');
        if (!postsWrapper) {
            alert('未找到 posts-wrapper 元素');
            return;
        }
        
        // 查找 h3 标签内的 a 标签,这些包含实际的标题文字
        const titleLinks = postsWrapper.querySelectorAll('h3 a');
        let newCount = 0;
        
        titleLinks.forEach(link => {
            const text = link.textContent.trim();
            if (text) {
                // 添加原始记录
                if (!collectionPool.includes(text)) {
                    collectionPool.push(text);
                    newCount++;
                }
                
                // 生成去掉第一个空格前内容的记录
                const spaceIndex = text.indexOf(' ');
                if (spaceIndex > 0) {
                    const shortText = text.substring(spaceIndex + 1).trim();
                    if (shortText && !collectionPool.includes(shortText)) {
                        collectionPool.push(shortText);
                        newCount++;
                    }
                }
            }
        });
        
        // 排序
        collectionPool.sort();
        
        // 更新显示
        updateCollectionDisplay();
        
        // 保存数据
        saveCollectionData();
        
        // 显示收集结果
        if (newCount > 0) {
            alert(`成功收集 ${newCount} 个新名称,当前总计 ${collectionPool.length} 个`);
        } else {
            alert('没有发现新的名称');
        }
    }
    
    // 更新收集列表显示
    function updateCollectionDisplay() {
        const listContainer = document.getElementById('collection-list');
        const counter = document.getElementById('collection-counter');
        
        if (!listContainer || !counter) return;
        
        // 清空列表
        listContainer.innerHTML = '';
        
        if (collectionPool.length === 0) {
            listContainer.innerHTML = '<div style="color: #999; text-align: center;">暂无收集内容</div>';
        } else {
            collectionPool.forEach((name, index) => {
                const item = document.createElement('div');
                item.style.cssText = `
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    padding: 4px 0;
                    border-bottom: 1px solid #444;
                `;
                
                const nameSpan = document.createElement('span');
                nameSpan.textContent = name;
                nameSpan.style.cssText = `
                    flex: 1;
                    word-break: break-all;
                `;
                
                const deleteBtn = document.createElement('button');
                deleteBtn.textContent = '×';
                deleteBtn.style.cssText = `
                    background: #f44336;
                    color: white;
                    border: none;
                    border-radius: 50%;
                    width: 20px;
                    height: 20px;
                    cursor: pointer;
                    margin-left: 8px;
                    font-size: 12px;
                    line-height: 1;
                `;
                deleteBtn.addEventListener('click', () => deleteItem(index));
                
                item.appendChild(nameSpan);
                item.appendChild(deleteBtn);
                listContainer.appendChild(item);
            });
        }
        
        // 更新计数器
        counter.textContent = `共收集 ${collectionPool.length} 个名称`;
    }
    
    // 删除单个项目
    function deleteItem(index) {
        collectionPool.splice(index, 1);
        updateCollectionDisplay();
        saveCollectionData();
    }
    
    // 清空收集列表
    function clearCollection() {
        if (confirm('确定要清空所有收集的内容吗?')) {
            collectionPool = [];
            updateCollectionDisplay();
            saveCollectionData();
        }
    }
    
    // 导出JSON文件
    function exportToJSON() {
        const exportData = {
            blacklist: collectionPool,
            exportTime: Date.now(),
            version: "1.0"
        };
        
        const jsonString = JSON.stringify(exportData, null, 2);
        const blob = new Blob([jsonString], { type: 'application/json' });
        const url = URL.createObjectURL(blob);
        
        const a = document.createElement('a');
        a.href = url;
        a.download = `collection_${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.json`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
        
        alert('JSON文件导出成功!');
    }
    
    // 导入JSON文件
    function importFromJSON() {
        // 创建文件输入元素
        const fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = '.json';
        fileInput.style.display = 'none';
        
        fileInput.addEventListener('change', function(event) {
            const file = event.target.files[0];
            if (!file) return;
            
            const reader = new FileReader();
            reader.onload = function(e) {
                try {
                    const importData = JSON.parse(e.target.result);
                    let importedNames = [];
                    
                    // 检查不同的数据格式
                    if (importData.blacklist && Array.isArray(importData.blacklist)) {
                        importedNames = importData.blacklist;
                    } else if (Array.isArray(importData)) {
                        importedNames = importData;
                    } else if (typeof importData === 'object') {
                        // 如果是对象,尝试提取所有字符串值
                        importedNames = Object.values(importData).filter(item => typeof item === 'string');
                    }
                    
                    if (importedNames.length === 0) {
                        alert('未找到有效的名称数据');
                        return;
                    }
                    
                    // 合并数据并去重
                    let newCount = 0;
                    importedNames.forEach(name => {
                        const trimmedName = name.trim();
                        if (trimmedName && !collectionPool.includes(trimmedName)) {
                            collectionPool.push(trimmedName);
                            newCount++;
                        }
                    });
                    
                    // 排序
                    collectionPool.sort();
                    
                    // 更新显示
                    updateCollectionDisplay();
                    
                    // 保存数据
                    saveCollectionData();
                    
                    // 显示导入结果
                    alert(`成功导入 ${newCount} 个新名称,当前总计 ${collectionPool.length} 个`);
                    
                } catch (error) {
                    alert('JSON文件格式错误,请检查文件内容');
                    console.error('导入错误:', error);
                }
            };
            
            reader.readAsText(file);
        });
        
        // 触发文件选择
        document.body.appendChild(fileInput);
        fileInput.click();
        document.body.removeChild(fileInput);
    }
    
    // 检查页面是否有可收集的数据
    function checkPageData() {
        const currentUrl = window.location.href;
        
        if (currentUrl.includes('lin08.app/original_user_list.php')) {
            const usernameElements = document.querySelectorAll('.uos_card_username');
            return usernameElements.length > 0;
        } else if (currentUrl.includes('piaoxue77.com')) {
            const postsWrapper = document.querySelector('.posts-wrapper');
            if (postsWrapper) {
                const titleLinks = postsWrapper.querySelectorAll('h3 a');
                return titleLinks.length > 0;
            }
        }
        return false;
    }
    
    // 自动收集功能
    function autoCollect() {
        if (checkPageData()) {
            console.log('检测到页面数据,自动执行收集...');
            collectNames();
        }
    }
    
    // 初始化
    function init() {
        // 等待页面加载完成
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', () => {
                createUI();
                loadCollectionData();
                // 延迟一点时间确保页面完全渲染
                setTimeout(autoCollect, 1000);
            });
        } else {
            createUI();
            loadCollectionData();
            // 延迟一点时间确保页面完全渲染
            setTimeout(autoCollect, 1000);
        }
    }
    
    // 启动脚本
    init();
    
})();