Greasy Fork

Greasy Fork is available in English.

图寻我的插件

我的插件

当前为 2023-10-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         图寻我的插件
// @namespace    http://tampermonkey.net/
// @version      2.63
// @description  我的插件
// @author       yukejun
// @match        https://tuxun.fun/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 图寻插件功能
    let currentRound = 1; // 默认为1

    function getModifiedURL(originalURL) {
        let match = originalURL.match(/(infinityId|challengeId|streakId)=([\w-]+)/);
        if (match && match[2]) {
            return `https://tuxun.fun/replay_pano?gameId=${match[2]}&round=${currentRound}`;
        }
        return originalURL;
    }

    function bindClickEvent() {
        let element = document.querySelector('img[src="https://i.chao-fan.com/biz/1662830707508_d7e5c8ce884a4fb692096396a5405f5b_0.png"]');
        if(element && !element.__bound) {
            element.__bound = true;
            element.addEventListener("click", function() {
                let modifiedURL = getModifiedURL(window.location.href);
                window.open(modifiedURL, '_blank');
            });
        }
    }

    function monitorRoundChange() {
        let roundDiv = document.querySelector("div[data-v-26f5391c]");
        if(roundDiv) {
            let roundObserver = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if(mutation.type === "childList") {
                        let match = roundDiv.textContent.match(/第 (\d+) 轮/);
                        if(match && match[1]) {
                            currentRound = parseInt(match[1], 10);
                        }
                    }
                });
            });
            roundObserver.observe(roundDiv, { childList: true, subtree: true });
        }
    }

    document.addEventListener('keydown', function(e) {
        console.log('Key pressed:', e.keyCode);
        if (e.keyCode === 32) {
            const buttons = document.querySelectorAll('button');
            buttons.forEach(function(button) {
                if (button.textContent.includes('开始(经典5轮)') || button.textContent.includes('再来一局') || button.textContent.includes('保留')) {
                    button.click();
                }
            });
        }
    });

    const mainObserver = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            bindClickEvent();
            monitorRoundChange();
            if (mutation.addedNodes) {
                mutation.addedNodes.forEach(function(node) {
                    if (node.nodeType === Node.ELEMENT_NODE && node.matches('.van-toast.van-toast--middle.van-toast--text')) {
                        const innerDiv = node.querySelector('.van-toast__text');
                        if (innerDiv && innerDiv.textContent.trim() === "比赛已经开始或者这一轮游戏还未结束") {
                            node.style.display = 'none';
                        }
                    }
                });
            }
        });
    });

    mainObserver.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 拖拽功能
    function setInitialPositionFromStorage(element, selector) {
        const storedPos = localStorage.getItem(selector);
        if (storedPos) {
            const { left, top } = JSON.parse(storedPos);
            element.style.left = left;
            element.style.top = top;
        }
    }

    function makeDraggable(element, selector) {
        let isDragging = false;
        let startX, startY, initialX, initialY;
        if (!element) return;
        if (window.getComputedStyle(element).position === 'static') {
            element.style.position = 'relative';
        }
        setInitialPositionFromStorage(element, selector);
        element.addEventListener('mousedown', function(event) {
            isDragging = true;
            startX = event.clientX;
            startY = event.clientY;
            initialX = parseInt(element.style.left || 0);
            initialY = parseInt(element.style.top || 0);
            const map = window.map || document.querySelector('#map').__gm;
            if (map && map.setOptions) {
                map.setOptions({draggable: false});
            }
            event.stopPropagation();
            event.preventDefault();
        });

        document.addEventListener('mousemove', function(event) {
            if (!isDragging) return;
            let dx = event.clientX - startX;
            let dy = event.clientY - startY;
            element.style.left = (initialX + dx) + 'px';
            element.style.top = (initialY + dy) + 'px';
            event.stopPropagation();
            event.preventDefault();
        });

        document.addEventListener('mouseup', function(event) {
            if (isDragging) {
                const map = window.map || document.querySelector('#map').__gm;
                if (map && map.setOptions) {
                    map.setOptions({draggable: true});
                }
                localStorage.setItem(selector, JSON.stringify({
                    left: element.style.left,
                    top: element.style.top
                }));
            }
            isDragging = false;
            event.stopPropagation();
        });
    }

    document.addEventListener('dblclick', function(event) {
        if (event.target.closest('#tuxun')) {
            event.preventDefault();
            event.stopPropagation();
        }
    }, true);

    const selectors = [
        '#viewer > div > div:nth-child(14) > div.gmnoprint.gm-bundled-control.gm-bundled-control-on-bottom > div'
    ];
    const dragObserver = new MutationObserver(mutations => {
        for (const mutation of mutations) {
            if (mutation.addedNodes.length) {
                selectors.forEach(selector => {
                    const element = document.querySelector(selector);
                    if (element) {
                        makeDraggable(element, selector);
                    }
                });
            }
        }
    });

    dragObserver.observe(document.body, {
        childList: true,
        subtree: true
    });

})();