打板客网股票代码复制插件

给dabanke股票表格添加复制按钮,复制股票代码到剪贴板

// ==UserScript==
// @name         打板客网股票代码复制插件
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  给dabanke股票表格添加复制按钮,复制股票代码到剪贴板
// @match        https://dabanke.com/*
// @grant        GM_setClipboard
// @icon         https://dabanke.com/favicon.ico
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    // 等待表格加载(确保页面渲染完成)
    function waitForTable() {
        const table = document.querySelector("table");
        if (table) {
            addCopyButtons(table);
            addGlobalCopyButton(table);
        } else {
            setTimeout(waitForTable, 500); // 如果未加载完成,继续等待
        }
    }
    function addGlobalCopyButton(table) {
        const button = document.createElement("button");
        button.textContent = "CopyAll";
        button.style.position = "fixed";
        button.style.bottom = "20px";
        button.style.right = "20px";
        button.style.zIndex = "9999";
        button.style.padding = "8px 12px";
        button.style.fontSize = "14px";
        button.style.cursor = "pointer";

        button.addEventListener("click", () => {
            const links = table.querySelectorAll('a[href*="/gupiao-"]');
            const allCodes = Array.from(links).map(link => {
                const href = link.getAttribute("href");
                const match = href.match(/gupiao-(\d+)\.html/);
                return match ? match[1] : null;
            }).filter(code => code !== null);

            if (allCodes.length > 0) {
                const uniqueCodes = [...new Set(allCodes)];
                GM_setClipboard(uniqueCodes.join(" "));
                button.textContent = "CopiedAll";
                setTimeout(() => {
                    button.textContent = "CopyAll";
                }, 1500);
            } else {
                button.textContent = "None";
                setTimeout(() => {
                    button.textContent = "CopyAll";
                }, 1500);
            }
        });

        document.body.appendChild(button);
    }
    function addCopyButtons(table) {
        const rows = table.querySelectorAll("tr");

        rows.forEach(row => {
            const tdList = row.querySelectorAll("td");

            // 确保是我们要处理的行(至少3列)
            if (tdList.length >= 3) {
                const thirdTd = tdList[2];
                const links = thirdTd.querySelectorAll('a[href*="/gupiao-"]');

                const stockCodes = Array.from(links).map(link => {
                    const href = link.getAttribute("href");
                    const match = href.match(/gupiao-(\d+)\.html/);
                    return match ? match[1] : null;
                }).filter(code => code !== null);
                if (stockCodes.length > 0) {
                    // 创建按钮
                    const button = document.createElement("button");
                    button.textContent = "Copy";
                    button.style.marginLeft = "5px";
                    button.style.cursor = "pointer";
                    button.style.fontSize = "12px";

                    // 绑定点击事件
                    button.addEventListener("click", () => {
                        GM_setClipboard(stockCodes);
                        button.textContent = "Copied";
                        setTimeout(() => {
                            button.textContent = "Copy";
                        }, 1000);
                    });

                    // 插入到第一列(tdList[0])中
                    tdList[0].appendChild(button);
                }
            }
        });
    }

    waitForTable();
})();