鱼糕V2 钓饵统计器 (优化版)

使用最新浏览器 API 优化,更高效地统计鱼糕V2游戏所需的钓饵种类

// ==UserScript==
// @name    鱼糕V2 钓饵统计器 (优化版)
// @version  2.2
// @description 使用最新浏览器 API 优化,更高效地统计鱼糕V2游戏所需的钓饵种类
// @author   Atail@神意之地
// @match    https://fish.ffmomola.com/ng/
// @icon     https://www.google.com/s2/favicons?sz=64&domain=ffmomola.com
// @grant    GM_notification
// @namespace https://gf.qytechs.cn/users/437453
// ==/UserScript==

(() => {
    'use strict';

    const fishingRecordsPageUrl = 'https://fish.ffmomola.com/ng/#/fishing';
    const targetContainerSelector = 'div.MuiPaper-root';
    const loadMoreButtonSelector = 'div.MuiTypography-root';
    const loadMoreButtonText = '加载更多';
    const fishingRecordRowSelector = 'tr.MuiTableRow-root:not(.MuiTableRow-head)';
    const baitInfoCellIndex = 4;
    const baitLabelContainerSelector = 'div.MuiAvatar-root[aria-label]';

    const createNotification = (bait) => GM_notification({
        text: `[鱼糕V2] 钓饵 "${bait}" 已复制到剪贴板`,
        title: '提示',
        image: 'https://www.google.com/s2/favicons?sz=64&domain=ffmomola.com',
        timeout: 2000
    });

    const displayBaitSummary = () => {
        console.log('[鱼糕V2] 开始显示钓饵统计');
        const uniqueBaitNames = new Set();
        const fishingRecordRows = document.querySelectorAll(fishingRecordRowSelector);

        fishingRecordRows.forEach(recordRow => {
            const baitCell = recordRow.children[baitInfoCellIndex];
            if (baitCell) {
                const baitLabelContainer = baitCell.querySelector(baitLabelContainerSelector);
                if (baitLabelContainer) {
                    const baitName = baitLabelContainer.getAttribute('aria-label');
                    if (baitName && baitName !== '捕鱼人之识' && baitName !== '钓组') {
                        uniqueBaitNames.add(baitName);
                    }
                }
            }
        });
        console.log(`[鱼糕V2] 找到 ${uniqueBaitNames.size} 种不同的钓饵`);

        const targetContainerElement = document.querySelector(targetContainerSelector);
        if (!targetContainerElement) {
            console.error("[鱼糕V2] 未找到目标容器元素,无法插入钓饵统计。");
            return;
        }

        const baitTagsContainer = document.createElement('div');
        baitTagsContainer.style.cssText = 'display: flex; flex-wrap: wrap; gap: 4px;'; // 使用 gap 简化 margin
        const fragment = document.createDocumentFragment();

        uniqueBaitNames.forEach(bait => {
            const baitTagElement = document.createElement('div');
            baitTagElement.textContent = bait;
            baitTagElement.style.cssText = `
                border-radius: 16px;
                font-size: 14px;
                height: 32px;
                line-height: 32px;
                color: #fff;
                padding: 0 12px;
                border: 1px solid #ffffff1f;
                user-select: none;
                background-color: #333333;
                cursor: pointer;
            `;
            baitTagElement.addEventListener('click', () => {
                navigator.clipboard.writeText(bait).then(() => {
                    createNotification(bait)
                    // 可以添加一些视觉反馈,例如短暂地改变标签的背景色
                    const originalBackgroundColor = baitTagElement.style.backgroundColor;
                    baitTagElement.style.backgroundColor = '#555555';
                    setTimeout(() => {
                        baitTagElement.style.backgroundColor = originalBackgroundColor;
                    }, 500);
                }).catch(err => {
                    console.error('[鱼糕V2] 复制到剪贴板失败: ', err);
                });
            });
            fragment.appendChild(baitTagElement);
        });
        baitTagsContainer.appendChild(fragment);

        const previousBaitTagsContainer = targetContainerElement.previousElementSibling;
        if (previousBaitTagsContainer && previousBaitTagsContainer.style.display === 'flex' && previousBaitTagsContainer.dataset.scriptVersion === '鱼糕V2钓饵统计') {
            previousBaitTagsContainer.remove();
        }

        baitTagsContainer.dataset.scriptVersion = '鱼糕V2钓饵统计'; // 添加一个自定义属性,方便后续识别
        targetContainerElement.parentNode.insertBefore(baitTagsContainer, targetContainerElement);
        console.log('[鱼糕V2] 钓饵统计显示完毕');
    };

    const autoLoadAllRecords = () => {
        console.log('[鱼糕V2] 开始自动加载所有记录');
        const loadMoreObserver = new MutationObserver((mutationsList, observer) => {
            for (const mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    const loadMoreButton = Array.from(document.querySelectorAll(loadMoreButtonSelector)).find(div => div.textContent.trim() === loadMoreButtonText);
                    if (loadMoreButton) {
                        console.log('[鱼糕V2] 找到 "加载更多" 按钮,正在点击...');
                        loadMoreButton.click();
                        // 继续监听,等待下次出现
                    } else {
                        // "加载更多" 按钮消失,认为所有记录已加载
                        console.log('[鱼糕V2] "加载更多" 按钮已消失,停止监听。');
                        observer.disconnect();
                        displayBaitSummary();
                    }
                    break; // 假设每次 mutation 只关心子节点变化
                }
            }
        });

        const recordsContainer = document.querySelector(targetContainerSelector)?.parentElement; // 监听父元素可能更稳定
        if (recordsContainer) {
            loadMoreObserver.observe(recordsContainer, { childList: true, subtree: true });
            console.log('[鱼糕V2] 开始监听 "加载更多" 按钮...');
        } else {
            console.error('[鱼糕V2] 未找到记录容器,无法监听 "加载更多" 按钮。');
            displayBaitSummary(); // 如果找不到容器,尝试直接分析当前加载的数据
        }
    };

    const startScript = () => {
        console.log('[鱼糕V2] 脚本开始执行');
        if (window.location.href === fishingRecordsPageUrl) {
            console.log('[鱼糕V2] 当前 URL 与钓鱼记录页面 URL 匹配');
            const targetContainerObserver = new MutationObserver((mutationsList, observer) => {
                for (const mutation of mutationsList) {
                    if (mutation.type === 'childList') {
                        const targetContainerElement = document.querySelector(targetContainerSelector);
                        if (targetContainerElement) {
                            console.log('[鱼糕V2] 找到目标容器元素,开始自动加载记录');
                            observer.disconnect(); // 找到目标元素后停止监听
                            autoLoadAllRecords();
                            return;
                        }
                    }
                }
            });

            // 开始监听 body 元素的子节点变化,寻找目标容器
            const bodyElement = document.body;
            if (bodyElement) {
                targetContainerObserver.observe(bodyElement, { childList: true, subtree: true });
                console.log('[鱼糕V2] 开始监听目标容器元素...');
            } else {
                console.error('[鱼糕V2] 未找到 body 元素,无法启动监听。');
            }
        } else {
            console.log(`[鱼糕V2] 当前 URL (${window.location.href}) 不是钓鱼记录页面 URL (${fishingRecordsPageUrl}),脚本将不会执行。`);
        }
    };

    startScript();
})();

QingJ © 2025

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