天使之路

下注助手,一键设置前缀,一键设置下注点数

// ==UserScript==
// @name 天使之路
// @version 1.1.5
// @description 下注助手,一键设置前缀,一键设置下注点数
// @author veip007
// @license LGPL-2.0-or-later
// @match http*://*/htm_data/*/23/*
// @grant none
// @namespace https://greasyfork.org/users/13363
// ==/UserScript==
(function() {
    'use strict';
    // 创建并添加按钮到页面左下角
    function createButtons() {
        const buttonContainer = document.createElement('div');
        buttonContainer.style.position = 'fixed';
        buttonContainer.style.bottom = '10px';
        buttonContainer.style.left = '10px';
        buttonContainer.style.zIndex = '999';
        const settingsBtn = document.createElement('button');
        settingsBtn.textContent = '设置';
        const randomBtn = document.createElement('button');
        randomBtn.textContent = '随机';
        const highBtn = document.createElement('button');
        highBtn.textContent = '高赔';
        const lowBtn = document.createElement('button');
        lowBtn.textContent = '低赔';
        const extractBtn = document.createElement('button');
        extractBtn.textContent = '显示提取内容';
        const copyBtn = document.createElement('button');
        copyBtn.textContent = '复制';
        // 隐藏提取按钮
        extractBtn.style.display = 'none';
        buttonContainer.appendChild(settingsBtn);
        buttonContainer.appendChild(randomBtn);
        buttonContainer.appendChild(highBtn);
        buttonContainer.appendChild(lowBtn);
        buttonContainer.appendChild(extractBtn);
        buttonContainer.appendChild(copyBtn);
        document.body.appendChild(buttonContainer);
        return {
            settingsBtn,
            randomBtn,
            highBtn,
            lowBtn,
            extractBtn,
            copyBtn
        };
    }

    // 提取页面中楼主发布的表格信息(通用函数,处理不同页面结构)
    function extractTableInfo() {
        const postContent = document.querySelector('.tpc_content');
        if (!postContent) {
            console.error('未找到楼主发布的内容');
            return;
        }
        const table = postContent.querySelector('table');
        if (!table) {
            console.error('未找到表格元素');
            return;
        }
        const rows = table.querySelectorAll('tr');
        const headerRow = rows[0];
        const headerCells = headerRow.querySelectorAll('td');
        const hasDrawColumn = Array.from(headerCells).some(cell => cell.textContent.includes('平局'));
        const dataRows = Array.from(rows).slice(1);
        const tableData = [];
        dataRows.forEach(row => {
            const cells = row.querySelectorAll('td');
            const rowData = [];
            if (hasDrawColumn) {
                rowData.push(cells[1].textContent.trim(), cells[2].textContent.trim(), cells[3].textContent.trim());
            } else {
                rowData.push(cells[1].textContent.trim(), cells[3].textContent.trim());
            }
            tableData.push(rowData);
        });
        console.log('提取的表格数据:', tableData);
        return tableData;
    }

    // 根据赔率比较获取对应结果(高赔、低赔),赔率相同时随机返回,考虑第三列平局情况参与计算
    function getResultByOdds(tableData, isHighOdds) {
        const result = [];
        tableData.forEach(data => {
            const homeOdds = parseFloat(data[0].match(/\[([\d.]+)\]/)[1]);
            const awayOdds = parseFloat(data[1].match(/\[([\d.]+)\]/)[1]);
            const drawOdds = 1;
            if (homeOdds * drawOdds > awayOdds * drawOdds) {
                result.push(isHighOdds ? data[0] : data[1]);
            } else if (homeOdds * drawOdds < awayOdds * drawOdds) {
                result.push(isHighOdds ? data[1] : data[0]);
            } else {
                result.push(Math.random() < 0.5 ? data[0] : data[1]);
            }
        });
        return result;
    }

    // 生成随机选择的结果(修改为随机选择多组不同队伍,处理多页面数据)
    function getRandomResult(tableData) {
        const result = [];
        const numResultsNeeded = tableData.length;
        for (let i = 0; i < numResultsNeeded; i++) {
            const data = tableData[i];
            const randomSelection = Math.random() < 0.5 ? 0 : 1;
            result.push(data[randomSelection]);
        }
        return result;
    }

    // 显示输出结果到左上角,点击新按钮时清除上一次结果
    function displayResult(result, prefix, betPoints) {
        const existingResultDiv = document.querySelector('.result-display');
        if (existingResultDiv) {
            existingResultDiv.remove();
        }
        const resultDiv = document.createElement('div');
        resultDiv.className = 'result-display';
        resultDiv.style.position = 'fixed';
        resultDiv.style.top = '10px';
        resultDiv.style.left = '10px';
        resultDiv.style.zIndex = '999';
        resultDiv.style.backgroundColor = 'white';
        resultDiv.style.padding = '20px';
        resultDiv.style.border = '1px solid gray';
        let output = '';
        if (prefix) {
            output += `${prefix}<br>`;
        }
        for (let i = 0; i < result.length; i++) {
            output += `${i + 1}.下注球队:${result[i]}<br>`;
        }
        output += `下注点数:${betPoints}`;
        resultDiv.innerHTML = output;
        document.body.appendChild(resultDiv);
        return resultDiv;
    }

    // 创建设置弹窗
function createSettingsPopup() {
    const popup = document.createElement('div');
    popup.style.position = 'fixed';
    popup.style.top = '50%';
    popup.style.left = '50%';
    popup.style.transform = 'translate(-50%, -50%)';
    popup.style.backgroundColor = 'white';
    popup.style.padding = '20px';
    popup.style.border = '1px solid gray';
    popup.style.zIndex = '9999';

    const prefixCheckbox = document.createElement('input');
    prefixCheckbox.type = 'checkbox';
    const prefixLabel = document.createElement('label');
    prefixLabel.textContent = '前缀';
    const prefixInput = document.createElement('input');
    prefixInput.value = '队名:';

    const betPointsCheckbox = document.createElement('input');
    betPointsCheckbox.type = 'checkbox';
    betPointsCheckbox.checked = true; // 设置默认勾选状态
    const betPointsLabel = document.createElement('label');
    betPointsLabel.textContent = '下注点数';
    const betPointsInput = document.createElement('input');

    const saveBtn = document.createElement('button');
    saveBtn.textContent = '保存';
    const closeBtn = document.createElement('button');
    closeBtn.textContent = '关闭';

    popup.appendChild(prefixCheckbox);
    popup.appendChild(prefixLabel);
    popup.appendChild(prefixInput);
    popup.appendChild(document.createElement('br'));
    popup.appendChild(betPointsCheckbox);
    popup.appendChild(betPointsLabel);
    popup.appendChild(betPointsInput);
    popup.appendChild(document.createElement('br'));
    popup.appendChild(saveBtn);
    popup.appendChild(closeBtn);

    closeBtn.addEventListener('click', () => {
        popup.remove();
        settingsPopup = null;
    });

    return {
        popup,
        prefixCheckbox,
        prefixInput,
        betPointsCheckbox,
        betPointsInput,
        saveBtn,
        closeBtn
    };
}
    // 从本地存储读取设置参数
    function loadSettings() {
        const savedSettings = localStorage.getItem('scriptSettings');
        if (savedSettings) {
            return JSON.parse(savedSettings);
        }
        // prefixEnabled: true/false 为前缀默认勾选开关, betPointsEnabled: true/false 为前默认点数勾选开关, true 开,false 关
        return { prefix: '队名:无码大爷威武霸气', betPoints: '50', prefixEnabled: false, betPointsEnabled: true };
    }

    // 保存设置参数到本地存储
    function saveSettings(prefix, betPoints, prefixEnabled, betPointsEnabled) {
        const settings = { prefix, betPoints, prefixEnabled, betPointsEnabled };
        localStorage.setItem('scriptSettings', JSON.stringify(settings));
    }

    let settings = loadSettings();
    let settingsPopup;
    const {
        settingsBtn,
        highBtn,
        lowBtn,
        extractBtn,
        copyBtn,
        randomBtn // 获取随机按钮元素
    } = createButtons();

    // 随机按钮点击事件处理函数
    randomBtn.addEventListener('click', () => {
        const tableData = extractTableInfo();
        if (tableData) {
            const result = getRandomResult(tableData);
            const resultDiv = displayResult(result, settings.prefixEnabled ? settings.prefix : '', settings.betPointsEnabled ? settings.betPoints : '');
            copyBtn.addEventListener('click', () => {
                const textToCopy = resultDiv.innerText;
                navigator.clipboard.writeText(textToCopy).then(() => {
                    console.log('复制成功');
                }).catch((err) => {
                    console.error('复制失败:', err);
                });
            });
        }
    });

    // 点击设置按钮显示设置弹窗
    settingsBtn.addEventListener('click', () => {
        if (!settingsPopup) {
            const { popup, prefixCheckbox, prefixInput, betPointsCheckbox, betPointsInput, saveBtn, closeBtn } = createSettingsPopup();
            settingsPopup = { popup, prefixCheckbox, prefixInput, betPointsCheckbox, betPointsInput, saveBtn, closeBtn };
            prefixCheckbox.checked = settings.prefixEnabled;
            prefixInput.value = settings.prefix;
            betPointsCheckbox.checked = settings.betPointsEnabled;
            betPointsInput.value = settings.betPoints;
            document.body.appendChild(popup);
            saveBtn.addEventListener('click', () => {
                settings.prefixEnabled = prefixCheckbox.checked;
                settings.prefix = prefixInput.value;
                settings.betPointsEnabled = betPointsCheckbox.checked;
                settings.betPoints = betPointsInput.value;
                saveSettings(settings.prefix, settings.betPoints, settings.prefixEnabled, settings.betPointsEnabled);
                popup.remove();
                settingsPopup = null;
            });
        }
    });

    // 高赔按钮点击事件处理函数
    highBtn.addEventListener('click', () => {
        const tableData = extractTableInfo();
        if (tableData) {
            const result = getResultByOdds(tableData, true);
            const resultDiv = displayResult(result, settings.prefixEnabled ? settings.prefix : '', settings.betPointsEnabled ? settings.betPoints : '');
            copyBtn.addEventListener('click', () => {
                const textToCopy = resultDiv.innerText;
                navigator.clipboard.writeText(textToCopy).then(() => {
                    console.log('复制成功');
                }).catch((err) => {
                    console.error('复制失败:', err);
                });
            });
        }
    });

    // 低赔按钮点击事件处理函数
    lowBtn.addEventListener('click', () => {
        const tableData = extractTableInfo();
        if (tableData) {
            const result = getResultByOdds(tableData, false);
            const resultDiv = displayResult(result, settings.prefixEnabled ? settings.prefix : '', settings.betPointsEnabled ? settings.betPoints : '');
            copyBtn.addEventListener('click', () => {
                const textToCopy = resultDiv.innerText;
                navigator.clipboard.writeText(textToCopy).then(() => {
                    console.log('复制成功');
                }).catch((err) => {
                    console.error('复制失败:', err);
                });
            });
        }
    });

    // 显示提取内容按钮点击事件处理函数
    extractBtn.addEventListener('click', () => {
        const tableData = extractTableInfo();
        if (tableData) {
            const result = [];
            tableData.forEach(row => {
                result.push(...row);
            });
            const resultDiv = displayResult(result, '', '');
            copyBtn.addEventListener('click', () => {
                const textToCopy = resultDiv.innerText;
                navigator.clipboard.writeText(textToCopy).then(() => {
                    console.log('复制成功');
                }).catch((err) => {
                    console.error('复制失败:', err);
                });
            });
        }
    });

    // 监听键盘事件,当按下 F9 键时触发随机功能
    document.addEventListener('keydown', function(event) {
        if (event.key === 'F9') {
            const tableData = extractTableInfo();
            if (tableData) {
                const result = getRandomResult(tableData);
                const resultDiv = displayResult(result, settings.prefixEnabled ? settings.prefix : '', settings.betPointsEnabled ? settings.betPoints : '');
                copyBtn.addEventListener('click', () => {
                    const textToCopy = resultDiv.innerText;
                    navigator.clipboard.writeText(textToCopy).then(() => {
                        console.log('复制成功');
                    }).catch((err) => {
                        console.error('复制失败:', err);
                    });
                });
            }
        }
    });
})();