Copy URL and Title

Adds a floating button to copy the current page's title and URL

目前为 2024-09-14 提交的版本。查看 最新版本

// ==UserScript==
// @name         Copy URL and Title
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a floating button to copy the current page's title and URL
// @match        *://*/*
// @grant        GM_setClipboard
// @license      GPL-3.0
// ==/UserScript==

(function() {
    'use strict';

    // Create floating button
    const button = document.createElement('div');
    button.innerHTML = '📋';
    button.style.cssText = `
        position: fixed;
        bottom: 20px;
        right: 20px;
        width: 50px;
        height: 50px;
        background-color: #4CAF50;
        color: white;
        border-radius: 50%;
        text-align: center;
        line-height: 50px;
        font-size: 24px;
        cursor: pointer;
        z-index: 9999;
        box-shadow: 0 2px 5px rgba(0,0,0,0.3);
    `;

    // Create popup
    const popup = document.createElement('div');
    popup.style.cssText = `
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: white;
        padding: 20px;
        border-radius: 5px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.2);
        z-index: 10000;
        display: none;
    `;

    // Add button and popup to the page
    document.body.appendChild(button);
    document.body.appendChild(popup);

    // Button click event
    button.addEventListener('click', function() {
        const pageTitle = document.title;
        const pageUrl = window.location.href;
        const copyText = `${pageTitle}\n${pageUrl}`;

        // Copy to clipboard
        GM_setClipboard(copyText, 'text');

        // Show popup
        popup.innerHTML = `
            <h3>Copied to clipboard:</h3>
            <p><strong>Title:</strong> ${pageTitle}</p>
            <p><strong>URL:</strong> ${pageUrl}</p>
            <button id="closePopup" style="margin-top: 10px;">Close</button>
        `;
        popup.style.display = 'block';

        // Close popup button
        document.getElementById('closePopup').addEventListener('click', function() {
            popup.style.display = 'none';
        });
    });
})();