sb6657.cn斗鱼玩机器烂梗收集

在斗鱼直播间 6657 添加一个按钮,提供在线搜索,复制和一键发送

// ==UserScript==
// @name         sb6657.cn斗鱼玩机器烂梗收集
// @namespace    http://tampermonkey.net/
// @version      2024.10.20.01
// @description  在斗鱼直播间 6657 添加一个按钮,提供在线搜索,复制和一键发送
// @author       Hzm
// @match        https://www.douyu.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @icon         https://www.google.com/s2/favicons?sz=64&domain=douyu.com
// @grant        none
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    // 添加 CSS 样式         *://*/*          https://www.douyu.com/*
    function addStyles(css) {
        let styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.innerHTML = css;
        document.head.appendChild(styleElement);
    }

    const css = `
    #messageBox {
        font-size: 16px;
        position: fixed;
        top: 50px;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #64ce83;
        color: white;
        padding: 10px;
        border-radius: 5px;
        z-index: 2000;
        transition: all 0.3s ease;
    }
    .image-button {
        width: 50px;
        height: 50px;
        background-size: cover;
        margin: 5px;
    }
    .custom-button {
        font-size: 16px;
        padding: 10px;
        background-color: #4CAF50;
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        margin-right: 10px; /* 添加边距 */
    }
    .table-container {
        font-size: 16px;
        border-radius: 10px; /* 添加圆角 */
        display: none;
        position: fixed;
        width: 400px;
        top: 150px;
        right: 20px;
        z-index: 1001;
        background-color: #f0f0f0;
        border: 1px solid #ccc;
        max-height: 400px;
        overflow-y: auto;
    }
    .search-container {
        font-size: 16px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: sticky;
        top: 0;
        background-color: #f0f0f0; /* 与表格背景一致 */
        padding: 10px;
        border-bottom: 1px solid #ccc;
        z-index: 10; /* 保证搜索框置顶 */
    }
`;

    addStyles(css);

    // 创建并设置元素样式
    function createElement(tag, styles, textContent) {
        let element = document.createElement(tag);
        Object.assign(element.style, styles);
        if (textContent) {
            element.innerText = textContent;
        }
        return element;
    }

    // 创建按钮
    let button = createElement("button", {
        fontSize: "16px",
        width:"60px",
        height:"30px",
        position: "absolute",
        zIndex:1000,
        backgroundColor: "#4CAF50",
        color: "white",
        border: "none",
        right:"10px",
        bottom:"60px",
        borderRadius: "5px",
        marginRight: "53px",
        cursor: "pointer"
    }, "玩烂梗");

    // 等待页面加载完成,插入按钮到 .ChatToolBar__right 左侧
    function insertButton() {
        let toolbar = document.querySelector('.layout-Player');
        if (toolbar) {
            toolbar.insertBefore(button, toolbar.firstChild); // 将按钮插入到第一个子元素前
        } else {
            setTimeout(insertButton, 500); // 如果未找到,延时再尝试
        }
    }

    insertButton();
    // 创建表格容器
    let tableContainer = createElement("div", {
        fontSize: "16px",
        borderRadius: "10px", // 添加圆角
        display: "none",
        position: "fixed",
        width: "400px",
        top: "250px",
        right: "20px",
        zIndex: 1001,
        backgroundColor: "#f0f0f0",
        border: "1px solid #ccc",
        maxHeight: "400px",
        overflowY: "auto"
    });
    document.body.appendChild(tableContainer);


    // 搜索框容器
    let searchContainer = createElement("div", {
        fontSize: "16px",
        display: "flex",
        justifyContent: "space-between",
        alignItems: "center",
        height:"80px",
    });
    tableContainer.appendChild(searchContainer);

    // 搜索框
    let searchInput = createElement("input", {
        fontSize: "16px",
        width:"330px",
        flex: "1",
        zIndex:"5",
        padding: "0px",
        position: "fixed",
        boxSizing: "border-box",
        marginRight: "10px",
        marginTop: "30px",
        marginLeft: "5px"
    }, null);
    searchInput.type = "text";
    searchInput.placeholder = "  搜索弹幕...  在此复制次数不加";
    searchContainer.appendChild(searchInput);

    // 搜索按钮
    let searchButton = createElement("button", {
        fontSize: "13px",
        zIndex:"5",
        padding: "5px 10px",
        height:"30px",
        position: "fixed",
        backgroundColor: "#4CAF50",
        color: "white",
        border: "none",
        borderRadius: "5px",
        marginLeft:"345px",
        marginTop:"30px",
        cursor: "pointer"
    }, "搜索");
    searchContainer.appendChild(searchButton);


    { // home按钮
        let svgButton = createElement("button", {
            width: "32px",
            height: "32px",
            fontSize: "13px",
            zIndex:"5",
            position: "fixed",
            padding: "5px",
            backgroundColor: "transparent",
            color: "white",
            border: "none",
            marginBottom: "45px",
            marginLeft: "10px",
            borderRadius: "5px",
            cursor: "pointer"
        }, "");
        searchContainer.appendChild(svgButton);

        // 创建 SVG 图标
        let svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
        svgIcon.setAttribute("t", "1728366472699");
        svgIcon.setAttribute("class", "icon");
        svgIcon.setAttribute("viewBox", "0 0 1080 1024");
        svgIcon.setAttribute("version", "1.1");
        svgIcon.setAttribute("xmlns", "http://www.w3.org/2000/svg");
        svgIcon.setAttribute("p-id", "7286");
        svgIcon.setAttribute("width", "27");
        svgIcon.setAttribute("height", "27");

        // 创建路径
        let path1 = document.createElementNS("http://www.w3.org/2000/svg", "path");
        path1.setAttribute("d", "M1077.361778 507.050667L922.794667 369.208889V163.441778h-120.035556v98.702222l-88.689778-79.132444L542.606222 38.001778 371.143111 183.011556 183.580444 350.321778 7.793778 507.050667l73.500444 90.453333L183.580444 506.311111l182.158223-162.588444L542.606222 186.026667l176.753778 157.752889L901.688889 506.311111l102.229333 91.192889z");
        path1.setAttribute("fill", "#389f25");
        path1.setAttribute("p-id", "7287");

        let path2 = document.createElementNS("http://www.w3.org/2000/svg", "path");
        path2.setAttribute("d", "M544.824889 244.906667L368.071111 402.659556 185.856 565.248v386.275556c0 10.410667 4.949333 18.830222 11.150222 18.830222h273.237334v-257.706667h149.219555v257.706667h273.237333c6.144 0 11.150222-8.476444 11.150223-18.887111v-386.275556l-182.158223-162.531555-176.867555-157.752889z");
        path2.setAttribute("fill", "#389f25");
        path2.setAttribute("p-id", "7288");

        // 将路径添加到 SVG 图标中
        svgIcon.appendChild(path1);
        svgIcon.appendChild(path2);

        // 将 SVG 图标添加到按钮中
        svgButton.appendChild(svgIcon);

        // 绑定点击事件
        svgButton.addEventListener("click", function() {
            window.open("https://sb6657.cn", '_blank'); // 在新窗口中打开链接
        });
    }
    { // power按钮
        let powerButton = createElement("button", {
            width: "32px",
            zIndex:"5",
            height: "32px",
            fontSize: "13px",
            position: "fixed",
            padding: "5px",
            backgroundColor: "transparent",
            color: "white",
            border: "none",
            marginBottom: "45px",
            marginLeft: "50px",
            borderRadius: "5px",
            cursor: "pointer"
        }, "");
        searchContainer.appendChild(powerButton);

        // 创建 SVG 图标
        let svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
        svgIcon.setAttribute("t", "1728366887035");
        svgIcon.setAttribute("class", "icon");
        svgIcon.setAttribute("viewBox", "0 0 1024 1024");
        svgIcon.setAttribute("version", "1.1");
        svgIcon.setAttribute("xmlns", "http://www.w3.org/2000/svg");
        svgIcon.setAttribute("p-id", "9769");
        svgIcon.setAttribute("width", "27");
        svgIcon.setAttribute("height", "27");

        // 创建路径
        let path1 = document.createElementNS("http://www.w3.org/2000/svg", "path");
        path1.setAttribute("d", "M395.765333 586.570667h-171.733333c-22.421333 0-37.888-22.442667-29.909333-43.381334L364.768 95.274667A32 32 0 0 1 394.666667 74.666667h287.957333c22.72 0 38.208 23.018667 29.632 44.064l-99.36 243.882666h187.050667c27.509333 0 42.186667 32.426667 24.042666 53.098667l-458.602666 522.56c-22.293333 25.408-63.626667 3.392-54.976-29.28l85.354666-322.421333z");
        path1.setAttribute("fill", "#1296db");
        path1.setAttribute("p-id", "9770");


        svgIcon.appendChild(path1);


        // 将 SVG 图标添加到按钮中
        powerButton.appendChild(svgIcon);

        // 绑定点击事件
        powerButton.addEventListener("click", function() {
            window.open("http://cdn.dgq63136.icu/zfb.jpg", '_blank'); // 在新窗口中打开链接
        });
    }

    { // 问题按钮
        let powerButton = createElement("button", {
            width: "32px",
            height: "32px",
            zIndex:"5",
            fontSize: "13px",
            position: "fixed",
            padding: "5px",
            backgroundColor: "transparent",
            color: "white",
            border: "none",
            marginBottom: "45px",
            marginLeft: "90px",
            borderRadius: "5px",
            cursor: "pointer"
        }, "");
        searchContainer.appendChild(powerButton);

        // 创建 SVG 图标
        let svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
        svgIcon.setAttribute("t", "1728367594482");
        svgIcon.setAttribute("class", "icon");
        svgIcon.setAttribute("viewBox", "0 0 1024 1024");
        svgIcon.setAttribute("version", "1.1");
        svgIcon.setAttribute("xmlns", "http://www.w3.org/2000/svg");
        svgIcon.setAttribute("p-id", "13015");
        svgIcon.setAttribute("width", "27");
        svgIcon.setAttribute("height", "27");

        // 创建路径
        let path1 = document.createElementNS("http://www.w3.org/2000/svg", "path");
        path1.setAttribute("d", "M517.119 954.878c244.589 0 442.879-198.289 442.879-442.879 0-244.594-198.289-442.879-442.879-442.879C272.524 69.12 74.24 267.404 74.24 511.999 74.24 756.588 272.524 954.878 517.119 954.878L517.119 954.878zM517.119 891.608c-209.654 0-379.612-169.955-379.612-379.609s169.957-379.612 379.612-379.612 379.609 169.957 379.609 379.612S726.773 891.608 517.119 891.608L517.119 891.608zM523.949 243.992c-54.662 0-97.177 16.705-127.547 50.112-30.367 31.885-44.795 74.402-44.795 127.547l59.98 0c0-37.202 8.35-66.052 25.055-87.31 18.222-25.055 46.312-37.202 84.272-37.202 31.885 0 56.945 8.352 74.405 26.572 16.705 16.702 25.81 40.24 25.81 70.607 0 21.26-7.59 41-22.775 59.98-4.55 6.072-13.665 15.185-25.81 27.33-41 36.445-66.055 65.295-76.685 88.075-9.11 18.98-13.665 40.995-13.665 66.05l0 17.465 60.735 0 0-17.465c0-20.5 4.56-38.725 14.43-55.425 7.59-13.665 18.98-27.335 35.68-41.755 33.405-29.612 53.91-49.352 61.5-58.462 18.98-25.055 28.85-54.665 28.85-88.83 0-45.552-14.425-81.235-42.515-107.05C611.258 256.902 572.544 243.992 523.949 243.992L523.949 243.992zM512.564 706.363c-12.907 0-23.535 3.795-32.647 12.905-9.11 8.345-12.905 18.98-12.905 31.885s3.795 23.54 12.905 32.65c9.112 8.345 19.74 12.905 32.647 12.905 12.91 0 23.535-4.56 32.645-12.905 9.11-8.355 13.665-18.985 13.665-32.65 0-12.905-4.555-23.54-12.905-31.885C536.859 710.158 525.474 706.363 512.564 706.363L512.564 706.363z");
        path1.setAttribute("fill", "#000001");
        path1.setAttribute("p-id", "13016");


        svgIcon.appendChild(path1);


        // 将 SVG 图标添加到按钮中
        powerButton.appendChild(svgIcon);

        // 绑定点击事件
        powerButton.addEventListener("click", function() {
            window.open("https://www.wjx.cn/vm/rQUgnS0.aspx#", '_blank'); // 在新窗口中打开链接
        });
    }

    {//蒙蔽罩
        let zhanweiButton = createElement("button", {
            width: "400px",
            height: "80px",
            fontSize: "29px",
            position: "fixed",
            zIndex:"4",
            padding: "5px",
            pointerevents: "none",
            backgroundColor: "#f0f0f0",
            color: "white",
            border: "none",
            borderRadius: "7px 7px 0px 0px",
            cursor: "Default"
        }, "");
        searchContainer.appendChild(zhanweiButton);
    }




    { // 更新按钮
        let updateButton = createElement("button", {
            width: "32px",
            zIndex:"5",
            height: "32px",
            fontSize: "13px",
            position: "fixed",
            padding: "5px",
            backgroundColor: "transparent",
            color: "white",
            border: "none",
            marginBottom: "45px",
            marginLeft: "130px",
            borderRadius: "5px",
            cursor: "pointer"
        }, "");
        searchContainer.appendChild(updateButton);

        // 创建 SVG 图标
        let svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
        svgIcon.setAttribute("t", "1728372163273");
        svgIcon.setAttribute("class", "icon");
        svgIcon.setAttribute("viewBox", "0 0 1024 1024");
        svgIcon.setAttribute("version", "1.1");
        svgIcon.setAttribute("xmlns", "http://www.w3.org/2000/svg");
        svgIcon.setAttribute("p-id", "14109");
        svgIcon.setAttribute("width", "27");
        svgIcon.setAttribute("height", "27");

        // 创建路径
        let path1 = document.createElementNS("http://www.w3.org/2000/svg", "path");
        path1.setAttribute("d", "M705.536 328.544c-314.56-271.744-641.056 51.2-641.056 51.2 352-603.84 772.192-160 772.192-160l121.92-115.52v407.104H554.272zM318.912 695.008c314.496 271.648 641.056-51.2 641.056-51.2-352 603.808-772.192 160-772.192 160L64 919.392V548.704h406.208z");
        path1.setAttribute("fill", "#0590DF");
        path1.setAttribute("p-id", "14110");


        svgIcon.appendChild(path1);


        // 将 SVG 图标添加到按钮中
        updateButton.appendChild(svgIcon);

        // 绑定点击事件
        updateButton.addEventListener("click", function() {
            window.open("https://greasyfork.org/zh-CN/scripts/511842-sb6657-cn%E6%96%97%E9%B1%BC%E7%8E%A9%E6%9C%BA%E5%99%A8%E7%83%82%E6%A2%97%E6%94%B6%E9%9B%86", '_blank'); // 在新窗口中打开链接
        });
    }

    { // 关闭按钮
        let XButton = createElement("button", {
            width: "32px",
            height: "32px",
            zIndex:"5",
            fontSize: "13px",
            position: "fixed",
            padding: "5px",
            backgroundColor: "transparent",
            color: "black",
            border: "none",
            marginLeft: "370px",
            marginBottom: "45px",
            borderRadius: "5px",
            cursor: "pointer"
        }, "❌");
        searchContainer.appendChild(XButton);
        // 按钮点击事件,显示/隐藏搜索框
        XButton.addEventListener("click", function() {
            if (tableContainer.style.display === "none" || tableContainer.style.display === "") {
                tableContainer.style.display = "block";
                table.style.display = "none";  // 默认隐藏表格
            } else {
                tableContainer.style.display = "none";
            }
        });
    }




    // 创建表格
    let table = createElement("table", {
        fontSize: "16px",
        width: "100%",
        zIndex:"3",
        borderCollapse: "collapse"
    });
    tableContainer.appendChild(table);


    // 存储数据的哈希表
    let dataHash = {};

    // 请求后端数据(使用 fetch)
    function fetchDataFromServer(searchQuery) {
        fetch("https://dgq63136.icu:9090/machine/Query", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                D: "油猴",
                QueryBarrage: searchQuery  // 传递搜索的查询条件
            })
        })
            .then(response => response.json())
            .then(res => {
            if (res.code === "200") {
                buildDataHash(res.data); // 构建哈希表并存储数据
            } else {
                console.error("请求失败,返回错误码:", res.msg);
            }
        })
            .catch(error => {
            console.error("请求失败:", error);
        });
    }

    // 构建哈希表
    function buildDataHash(data) {
        dataHash = {}; // 清空哈希表
        data.forEach(item => {
            let lowerCaseBarrage = item.barrage.toLowerCase();
            if (!dataHash[lowerCaseBarrage]) {
                dataHash[lowerCaseBarrage] = [];
            }
            dataHash[lowerCaseBarrage].push(item);
        });
        renderTable(Object.values(dataHash).flat()); // 渲染表格数据
    }

    // 渲染表格数据
    function renderTable(data) {
        table.innerHTML = ""; // 清空表格
        if (data.length === 0) {
            table.style.display = "none";  // 如果没有数据,隐藏表格
        } else {
            table.style.display = "table";  // 如果有数据,显示表格
            data.forEach((item, index) => {
                let row = createElement("tr", {
                    backgroundColor: index % 2 === 0 ? "#f2f2f2" : "#ffffff"
                }, null);

                let barrageCell = createElement("td", {
                    cursor: "pointer",
                    padding: "5px",
                    width: "60%"
                }, item.barrage);
                barrageCell.addEventListener("click", function() {
                    copyToClipboard(item.barrage);
                });
                row.appendChild(barrageCell);

                let copyButtonCell = createElement("td", {
                    width: "8%"
                }, null);
                let copyButton = createElement("button", {
                    backgroundColor: "#4CAF50",
                    color: "white",
                    border: "none",
                    borderRadius: "5px",
                    cursor: "pointer"
                }, "复制");
                copyButton.addEventListener("click", function() {
                    copyToClipboard(item.barrage);
                });
                copyButtonCell.appendChild(copyButton);
                row.appendChild(copyButtonCell);

                // 一键发送按钮
                let sendButtonCell = createElement("td", {
                    width: "10%"
                }, null);
                let sendButton = createElement("button", {
                    backgroundColor: "#FF5722",
                    color: "white",
                    border: "none",
                    borderRadius: "5px",
                    cursor: "pointer"
                }, "一键发送");
                sendButton.addEventListener("click", function() {
                    sendBarrage(item.barrage);  // 调用发送功能
                });
                sendButtonCell.appendChild(sendButton);
                row.appendChild(sendButtonCell);

                table.appendChild(row);
            });
        }
    }

    // 搜索功能
    function performSearch() {
        let searchQuery = searchInput.value.toLowerCase();
        if (!searchQuery) {
            table.style.display = "none";  // 如果搜索框为空,不显示表格
            return;
        }
        fetchDataFromServer(searchQuery);  // 调用 fetchDataFromServer 获取数据
    }

    // 监听搜索按钮点击事件
    searchButton.addEventListener("click", performSearch);

    // 监听回车键事件
    searchInput.addEventListener("keydown", function(event) {
        if (event.key === "Enter") {
            performSearch();
        }
    });

    // 显示消息
    function showMessage(text, duration = 2000) {
        let messageBox = document.createElement("div");
        messageBox.id = "messageBox";
        messageBox.innerText = text;

        document.body.appendChild(messageBox);

        setTimeout(() => {
            messageBox.style.opacity = "0";
            setTimeout(() => {
                document.body.removeChild(messageBox);
            }, 300); // 等待过渡动画完成后再移除元素
        }, duration);
    }

    // 按钮点击事件,显示/隐藏搜索框
    button.addEventListener("click", function() {
        if (tableContainer.style.display === "none" || tableContainer.style.display === "") {
            tableContainer.style.display = "block";
            table.style.display = "none";  // 默认隐藏表格
        } else {
            tableContainer.style.display = "none";
        }
    });


    let isDragging = false;
    let offsetX, offsetY;

    tableContainer.addEventListener('mousedown', function(e) {
        isDragging = true;
        offsetX = e.clientX - tableContainer.getBoundingClientRect().left;
        offsetY = e.clientY - tableContainer.getBoundingClientRect().top;
        tableContainer.style.cursor = "grabbing";
    });

    document.addEventListener('mousemove', function(e) {
        if (isDragging) {
            let left = e.clientX - offsetX;
            let top = e.clientY - offsetY;
            tableContainer.style.left = left + 'px';
            tableContainer.style.top = top + 'px';
        }
    });

    document.addEventListener('mouseup', function() {
        isDragging = false;
        tableContainer.style.cursor = "move";
    });


    // 复制到剪贴板
    async function copyToClipboard(text) {
        try {
            await navigator.clipboard.writeText(text);
            showMessage("✔️✔️复制成功: ✔️✔️");
        } catch (error) {
            showMessage("❌❌复制失败,请尝试手动复制。❌❌");
        }
    }


    // 用于控制发送按钮冷却时间的变量
    let sendButtonCooldown = false;

    // 发送弹幕到 <textarea> 并点击发送按钮
    function sendBarrage(barrageText) {
        // 如果按钮处于冷却状态,则直接返回
        if (sendButtonCooldown) {
            showMessage("❌❌不许刷屏!😡❌❌");
            return;
        }

        sendButtonCooldown = true;  // 设置按钮为冷却状态

        // 找到 textarea 并填入内容
        let textArea = document.querySelector('textarea.ChatSend-txt');
        if (textArea) {
            textArea.value = barrageText;  // 填入弹幕内容

            // 找到发送按钮并模拟点击
            let sendButton = document.querySelector('div.ChatSend-button');
            if (sendButton) {
                sendButton.click();  // 模拟点击发送按钮
                showMessage("✔️✔️弹幕发送成功✔️✔️");
            } else {
                showMessage("❌❌发送失败,请尝试手动复制。❌❌");
            }
        } else {
            showMessage("❌❌发送失败,请尝试手动复制。❌❌");
        }

        // 启动定时器,在5秒后解除冷却状态
        setTimeout(() => {
            sendButtonCooldown = false;
        }, 10000);
    }
})();