Greasy Fork

Greasy Fork is available in English.

XPath 文本复制工具

在页面右下角输入XPath并复制匹配的文本内容

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         XPath 文本复制工具
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  在页面右下角输入XPath并复制匹配的文本内容
// @author       qztan
// @match        *://*/*
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加样式
    GM_addStyle(`
        #xpathCopyTool {
            position: fixed;
            bottom: 20px;
            right: 20px;
            width: 300px;
            padding: 15px;
            background: rgba(0, 0, 0, 0.8);
            color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.5);
            z-index: 10000;
            font-family: Arial, sans-serif;
        }
        #xpathCopyTool input {
            width: calc(100% - 20px);
            padding: 8px;
            margin-bottom: 10px;
            border: none;
            border-radius: 4px;
        }
        #xpathCopyTool button {
            width: 100%;
            padding: 10px;
            background: #28a745;
            border: none;
            border-radius: 4px;
            color: #fff;
            font-size: 14px;
            cursor: pointer;
        }
        #xpathCopyTool button:hover {
            background: #218838;
        }
        #xpathCopyTool .close-btn {
            position: absolute;
            top: 5px;
            right: 5px;
            width: 20px;
            height: 20px;
            background: none;
            border: none;
            color: #fff;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            line-height: 20px;
            text-align: center;
            padding: 0;
        }
        #xpathCopyTool .close-btn:hover {
            color: #ff4d4f;
        }
        #xpathCopyTool .message {
            margin-top: 10px;
            font-size: 12px;
            color: #28a745;
        }
    `);

    // 创建提示框容器
    const container = document.createElement('div');
    container.id = 'xpathCopyTool';

    // 关闭按钮
    const closeBtn = document.createElement('button');
    closeBtn.innerHTML = '×';
    closeBtn.className = 'close-btn';
    closeBtn.title = '关闭';
    closeBtn.onclick = () => container.style.display = 'none';
    container.appendChild(closeBtn);

    // 输入框
    const input = document.createElement('input');
    input.type = 'text';
    input.placeholder = '请输入XPath路径';
    container.appendChild(input);

    // 复制按钮
    const button = document.createElement('button');
    button.textContent = '复制';
    container.appendChild(button);

    // 消息显示
    const message = document.createElement('div');
    message.className = 'message';
    container.appendChild(message);

    // 添加到页面
    document.body.appendChild(container);

    // 复制功能
    button.addEventListener('click', () => {
        const xpath = input.value.trim();
        if (!xpath) {
            message.textContent = '请输入有效的XPath路径。';
            return;
        }
        try {
            const result = [];
            const nodesSnapshot = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            for (let i = 0; i < nodesSnapshot.snapshotLength; i++) {
                const node = nodesSnapshot.snapshotItem(i);
                if (node && node.textContent) {
                    result.push(node.textContent.trim());
                }
            }
            if (result.length === 0) {
                message.textContent = '没有找到匹配的内容。';
                return;
            }
            const textToCopy = result.join('\n');
            GM_setClipboard(textToCopy, 'text');
            message.textContent = '已复制 ' + result.length + ' 条内容到剪贴板。';
        } catch (error) {
            console.error('XPath 错误:', error);
            message.textContent = 'XPath 路径有误,请检查。';
        }
    });

})();