Advanced Copy/Paste

The button, where's you can

目前為 2025-03-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Advanced Copy/Paste
// @namespace    http://tampermonkey.net/
// @version      3.1
// @description  The button, where's you can
// @author       You
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_setClipboard
// @grant        GM_notification
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let savedTexts = GM_getValue('savedTexts', []);
    let isModalVisible = false;
    let isDragging = false;
    let dragOffset = { x: 0, y: 0 };
    let lastCopiedIndex = -1;
    let editingIndex = -1;
    if (savedTexts.length > 0) {
        const shouldRestore = confirm('Restore the previous state?');
        if (!shouldRestore) {
            savedTexts = [];
            GM_setValue('savedTexts', []);
        }
    }
    const button = document.createElement('button');
    button.textContent = '<';
    button.style.position = 'fixed';
    button.style.top = '50%';
    button.style.right = '10px';
    button.style.transform = 'translateY(-50%)';
    button.style.zIndex = '1000';
    button.style.backgroundColor = 'black';
    button.style.color = 'white';
    button.style.padding = '10px 15px';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.fontSize = '16px';
    document.body.appendChild(button);
    const modal = document.createElement('div');
    modal.style.position = 'fixed';
    modal.style.top = '100px';
    modal.style.right = '50px';
    modal.style.width = '200px';
    modal.style.backgroundColor = 'white';
    modal.style.zIndex = '1001';
    modal.style.display = 'none';
    modal.style.border = '1px solid #ccc';
    modal.style.borderRadius = '5px';
    modal.style.padding = '10px';
    modal.style.transition = 'transform 0.3s ease-out';
    document.body.appendChild(modal);
    const modalHeader = document.createElement('div');
    modalHeader.style.backgroundColor = '#ddd';
    modalHeader.style.color = 'black';
    modalHeader.style.padding = '5px';
    modalHeader.style.cursor = 'move';
    modal.appendChild(modalHeader);
    const textContainer = document.createElement('div');
    textContainer.style.maxHeight = '300px';
    textContainer.style.overflowY = 'auto';
    modal.appendChild(modalHeader);
    modal.appendChild(textContainer);
    GM_addStyle(`
        .saved-text-item {
            cursor: pointer;
            margin-bottom: 5px;
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
            font-size: 14px;
            word-break: break-word; /* Allows word hyphenation */
        }

        .saved-text-item:hover {
            background-color: #f0f0f0;
        }

        .selected-text-item { /* The style for the selected element */
            background-color: #add8e6; /* Light Blue */
        }

        @keyframes pulse {
            0% {
                transform: scale(1);
                opacity: 0.5;
            }
            50% {
                transform: scale(1.1);
                opacity: 1;
            }
            100% {
                transform: scale(1);
                opacity: 0.5;
            }
        }

        .modal-enter {
            animation: pulse 0.5s ease-in-out;
        }

        .modal-exit {
            transform: scale(0.5);
            opacity: 0;
        }

        .editing-text-item {
             background-color: yellow; /* Yellow background for the editable element */
        }
    `);
    function updateTextContainer() {
        textContainer.innerHTML = '';
        savedTexts.forEach((text, index) => {
            const textItem = document.createElement('div');
            textItem.classList.add('saved-text-item');
            textItem.textContent = text;
            if (index === lastCopiedIndex) {
                textItem.classList.add('selected-text-item');
            }
            if (index === editingIndex) {
                textItem.classList.add('editing-text-item');
            }
            textItem.addEventListener('click', (event) => {
                if (event.detail === 2) {
                    startEditing(index);
                } else {
                     copyText(index);
                }
            });
            textContainer.appendChild(textItem);
        });
    }
    function showMessage(message, type = 'success') {
        GM_notification({
            title: 'Копилка',
            text: message,
            timeout: 3000
        });
    }
    function copyText(index) {
        if (index >= 0 && index < savedTexts.length) {
            const text = savedTexts[index];
            GM_setClipboard(text);
            lastCopiedIndex = index;
            updateTextContainer();
            showMessage(`Текст скопирован: ${text.substring(0, 50)}${text.length > 50 ? '...' : ''}`);
        }
    }
    function deleteText(index) {
        if (index >= 0 && index < savedTexts.length) {
            const deletedText = savedTexts[index];
            savedTexts.splice(index, 1);
            if (lastCopiedIndex === index) {
                lastCopiedIndex = -1;
            } else if (lastCopiedIndex > index) {
                lastCopiedIndex--;
            }
            if (editingIndex === index) {
                editingIndex = -1;
            } else if (editingIndex > index) {
                editingIndex--;
            }
            updateTextContainer();
            showMessage(`Текст удален: ${deletedText.substring(0, 50)}${deletedText.length > 50 ? '...' : ''}`, 'error');

            GM_setValue('savedTexts', savedTexts);
        }
    }
    function startEditing(index) {
        if (index >= 0 && index < savedTexts.length) {
            editingIndex = index;
            updateTextContainer();
            const textItem = textContainer.children[index];
            if (textItem) {
                textItem.contentEditable = 'true';
                textItem.focus();
                textItem.addEventListener('blur', () => {
                    stopEditing(index, textItem.textContent);
                }, { once: true });
            }
        }
    }
    function stopEditing(index, newText) {
        if (index >= 0 && index < savedTexts.length) {
            savedTexts[index] = newText;
            editingIndex = -1;
            updateTextContainer();
            showMessage(`Текст изменен: ${newText.substring(0, 50)}${newText.length > 50 ? '...' : ''}`);
            GM_setValue('savedTexts', savedTexts);
        }
    }
    function deleteAllTexts() {
        savedTexts = [];
        lastCopiedIndex = -1;
        editingIndex = -1;
        updateTextContainer();
        showMessage('Все тексты удалены!', 'error');
        GM_setValue('savedTexts', savedTexts);
    }
    button.addEventListener('click', () => {
        if (!isModalVisible) {
            modal.classList.remove('modal-exit');
            modal.classList.add('modal-enter');
            modal.style.display = 'block';
        } else {
            modal.classList.remove('modal-enter');
            modal.classList.add('modal-exit');
            setTimeout(() => {
                modal.style.display = 'none';
                modal.classList.remove('modal-exit');
            }, 300);
        }
        isModalVisible = !isModalVisible;
        updateTextContainer();
    });
    function addTextToClipboard(text) {
        const trimmedText = text.trim();
        if (trimmedText !== "") {
            savedTexts.push(trimmedText);
            lastCopiedIndex = savedTexts.length - 1;
            updateTextContainer();
            GM_setValue('savedTexts', savedTexts);
        }
    }
    document.addEventListener('copy', (event) => {
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            addTextToClipboard(selectedText);
        }
    });
    modalHeader.addEventListener('mousedown', (e) => {
        isDragging = true;
        dragOffset.x = e.clientX - modal.offsetLeft;
        dragOffset.y = e.clientY - modal.offsetTop;
    });
    document.addEventListener('mousemove', (e) => {
        if (!isDragging) return;
        modal.style.right = 'auto'; //
        modal.style.left = (e.clientX - dragOffset.x) + 'px';
        modal.style.top = (e.clientY - dragOffset.y) + 'px';
    });

    document.addEventListener('mouseup', () => {
        isDragging = false;
    });
    document.addEventListener('mouseleave', () => {
        isDragging = false;
    });
    document.addEventListener('keydown', (event) => {
        if (event.key === 'Delete' && isModalVisible) {
            if (editingIndex !== -1) {
                deleteText(editingIndex);
                event.stopImmediatePropagation();
                event.preventDefault();
            }
        }
    });
})();