同时复制指定xpath页面内容和当前页面URL

Add a button to copy specified XPath text and current URL to clipboard

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         同时复制指定xpath页面内容和当前页面URL
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Add a button to copy specified XPath text and current URL to clipboard
// @author       You
// @match        *://*/*
// @grant        GM_setClipboard
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 在这里替换成你要复制的XPath表达式
    const xpathExpression = '//*[@id="summary-val"]'; // 例如,这里是一个示例XPath表达式

    // 创建按钮和提示元素
    const addButtonAndMessage = () => {
        const button = document.createElement('button');
        button.innerHTML = '复制bug标题和URL';
        button.style.position = 'fixed';
        button.style.top = '20px';
        button.style.right = '20px';
        button.style.zIndex = '9999';
        button.addEventListener('click', copyTextAndURL);
        button.style.padding = '15px'; // 设置按钮内边距
        button.style.borderRadius = '5px'; // 设置按钮圆角

        document.body.appendChild(button);

        const message = document.createElement('div');
        message.style.position = 'fixed';
        message.style.top = '80px';
        message.style.right = '10px';
        message.style.padding = '10px';
        message.style.backgroundColor = '#4CAF50'; // 绿色背景,可以根据需要调整颜色
        message.style.color = 'white';
        message.style.zIndex = '9998';
        message.style.display = 'none';
        document.body.appendChild(message);

        return message;
    };

    // 复制文本和URL到剪切板
    const copyTextAndURL = () => {
        const xpathResult = document.evaluate(xpathExpression, document, null, XPathResult.ANY_TYPE, null);
        const textNode = xpathResult.iterateNext();
        const textContent = textNode ? textNode.textContent : 'XPath text not found';
        const currentURL = window.location.href;

        // 复制到剪切板
        const copyText = `${currentURL}\n${textContent}`;
        GM_setClipboard(copyText, 'text');

        // 显示成功消息
        showMessage('bug标题和URL已复制成功');
    };

    // 显示消息
    const showMessage = (text) => {
        const message = addButtonAndMessage();
        message.textContent = text;
        message.style.display = 'block';

        // 3秒后隐藏消息
        setTimeout(() => {
            message.style.display = 'none';
        }, 3000);
    };

    // 等待页面加载完成后添加按钮和提示元素
    window.addEventListener('load', addButtonAndMessage);
})();