Greasy Fork

Greasy Fork is available in English.

雷速进球

复制进球方,隔4秒后复制对方

当前为 2020-08-23 提交的版本,查看 最新版本

// ==UserScript==
// @name         雷速进球
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  复制进球方,隔4秒后复制对方
// @author       han2ee
// @match        https://www.leisu.com/
// @grant   GM.getTab
// @grant   GM.saveTab
// ==/UserScript==

(function() {
    'use strict';
    const WAIT_TIME = 4; // 等4秒复制对方
    let lastWinScoreTeam = null;
    let lastLoseScoreTeam = null;

    var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
    function isPromise(obj) {
        return !!obj && ((typeof obj === 'object' && typeof obj.then === 'function') || (typeof obj === 'function' && typeof obj().then === 'function'));
    }
        /**
     * Wait Resource.
     *
     * @param {Function} resourceFn pred function to check resource
     * @param {Object} options
     * @returns Promise
     */
    function waitResource(resourceFn, options) {
        var optionsRes = Object.assign(
            {
                interval: 1000,
                max: 6
            },
            options
        );
        var current = 0;
        return new Promise((resolve, reject) => {
            var timer = setInterval(() => {
                if (isPromise(resourceFn)) {
                    resourceFn().then(res => {
                        if(res) {
                            clearInterval(timer);
                            resolve();
                        }
                    });
                } else if (resourceFn()) {
                    clearInterval(timer);
                    resolve();
                }
                current++;
                if (current >= optionsRes.max) {
                    clearInterval(timer);
                    reject('Time out');
                }
            }, optionsRes.interval);
        });
    }

    async function scoreChange(winScoreTeam, loseScoreTeam) {
        lastWinScoreTeam = winScoreTeam;
        lastLoseScoreTeam = loseScoreTeam;
        document.querySelector('#winInput').value = lastWinScoreTeam;
        document.querySelector('#loseInput').value = lastLoseScoreTeam;
        document.querySelector('#winBtn').click();
        await wait(WAIT_TIME * 1000);
        document.querySelector('#loseBtn').click();
    }

    function addUI() {
        let div=document.createElement("div");
        //div.setAttribute("style", "float:right");
        let winInput = document.createElement("INPUT");
        winInput.setAttribute("style", "width:160px");
        winInput.setAttribute("id", "winInput");
        div.appendChild(winInput);

        let winBtn = document.createElement("BUTTON");
        winBtn.innerText = "复制进球方";
        winBtn.setAttribute("id", "winBtn");
        winBtn.onclick = () => {
            const input = document.querySelector('#winInput');
            input.select();
            if (document.execCommand('copy')) {
                console.log('复制成功');
            }
        };
        div.appendChild(winBtn);

        let loseInput = document.createElement("INPUT");
        loseInput.setAttribute("style", "width:160px");
        loseInput.setAttribute("id", "loseInput");
        div.appendChild(loseInput);

        let loseBtn = document.createElement("BUTTON");
        loseBtn.innerText = "复制对方";
        loseBtn.setAttribute("id", "loseBtn");
        loseBtn.onclick = () => {
            const input = document.querySelector('#loseInput');
            input.select();
            if (document.execCommand('copy')) {
                console.log('复制成功');
            }
        };
        div.appendChild(loseBtn);
        let node = document.querySelector('.match-type');
        let parentNode = node.parentNode;
        parentNode.insertBefore(div, node);
    }

    async function main() {
        await waitResource(async () => {
            return !!document.querySelector('.match-type');
        });
        addUI();
        while (true) {
            await wait(500);
            let oldGamesObj = await GM.getTab();
            // console.log(`DEBUG: lastWinScoreTeam:${lastWinScoreTeam} lastLoseScoreTeam:${lastLoseScoreTeam}`);
            let liveGameDiv = document.querySelector('.index-model.live');
            if (!liveGameDiv) {
                continue;
            }
            let gameDivs = liveGameDiv.querySelectorAll('div.match-label.box_h');
            if (!gameDivs) {
                continue;
            }
            let gamesObj = {};
            for (let i = 0; i < gameDivs.length; i++) {
                let game = gameDivs[i];
                let homeName = game.querySelector('.home .name').innerText;
                let homeScore = game.querySelector('.homescore').innerText;
                let awayName = game.querySelector('.away .name').innerText;
                let awayScore = game.querySelector('.awayscore').innerText;
                let id = `${homeName} VS ${awayName}`;
                gamesObj[id] = {
                    homeName,
                    homeScore,
                    awayName,
                    awayScore,
                };
                if (oldGamesObj[id] && oldGamesObj[id].homeScore !== homeScore) { // home win score
                    await scoreChange(homeName, awayName);
                }
                if (oldGamesObj[id] && oldGamesObj[id].awayScore !== awayScore) { // away win score
                    await scoreChange(awayName, homeName);
                }
            }
            // update games
            await GM.saveTab(gamesObj);
        }
    }
    main()
        .then(() => console.log("Main Done"))
        .catch((err) => {
        console.error(err, "Restart in 6 secs ...");
        setTimeout(() => location.reload(), 6 * 1000);
    });
})();