Text Saver

Creates a button to save and copy text, with drag-and-drop file upload.

当前为 2025-04-05 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Text Saver
// @namespace    http://tampermonkey.net/
// @version      3.3
// @description  Creates a button to save and copy text, with drag-and-drop file upload.
// @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 previous session?');
        if (!shouldRestore) {
            savedTexts = [];
            GM_setValue('savedTexts', []);
        }
    }

    function generateRandomString(length) {
        let result = '';
        const characters = '0123456789';
        const charactersLength = characters.length;
        for (let i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
    }

    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);

    const fileButton = document.createElement('button');
    fileButton.textContent = '📄';
    fileButton.style.position = 'absolute';
    fileButton.style.top = '5px';
    fileButton.style.left = '5px';
    fileButton.style.padding = '5px';
    fileButton.style.fontSize = '14px';
    fileButton.style.cursor = 'pointer';
    modal.appendChild(fileButton);

    const dragWindow = document.createElement('div');
    dragWindow.style.position = 'fixed';
    dragWindow.style.top = '20%';
    dragWindow.style.left = '30%';
    dragWindow.style.width = '400px';
    dragWindow.style.height = '300px';
    dragWindow.style.backgroundColor = 'white';
    dragWindow.style.border = '2px dashed #ccc';
    dragWindow.style.borderRadius = '10px';
    dragWindow.style.zIndex = '1002';
    dragWindow.style.display = 'none';
    dragWindow.style.textAlign = 'center';
    dragWindow.style.lineHeight = '300px';
    dragWindow.textContent = 'Drag file here';
    document.body.appendChild(dragWindow);

    let isDragWindowDragging = false;
    let dragWindowOffset = { x: 0, y: 0 };

    dragWindow.addEventListener('mousedown', (e) => {
        isDragWindowDragging = true;
        dragWindowOffset.x = e.clientX - dragWindow.offsetLeft;
        dragWindowOffset.y = e.clientY - dragWindow.offsetTop;
    });

    document.addEventListener('mousemove', (e) => {
        if (!isDragWindowDragging) return;

        dragWindow.style.left = (e.clientX - dragWindowOffset.x) + 'px';
        dragWindow.style.top = (e.clientY - dragWindowOffset.y) + 'px';
    });

    document.addEventListener('mouseup', () => {
        isDragWindowDragging = false;
    });

    dragWindow.addEventListener('dragover', (e) => {
        e.preventDefault();
        dragWindow.style.borderColor = 'blue';
    });

    dragWindow.addEventListener('dragleave', () => {
        dragWindow.style.borderColor = '#ccc';
    });

    dragWindow.addEventListener('drop', (e) => {
        e.preventDefault();
        dragWindow.style.borderColor = '#ccc';

        const file = e.dataTransfer.files[0];
        if (file) {
            const randomString = generateRandomString(9);
            const fileLink = `https://mpp.file.download/${randomString}`;
            addTextToClipboard(fileLink);
             GM_notification({
                title: 'Text Saver',
                text: 'File link added to text saver',
                timeout: 3000
            });
        }
        dragWindow.style.display = 'none';
    });

    dragWindow.addEventListener('paste', (e) => {
        const items = (e.clipboardData || e.originalEvent.clipboardData).items;
        for (let index in items) {
            const item = items[index];
            if (item.kind === 'file') {
                const file = item.getAsFile();
                 if (file) {
                    const randomString = generateRandomString(9);
                    const fileLink = `https://mpp.file.download/${randomString}`;
                    addTextToClipboard(fileLink);
                    GM_notification({
                        title: 'Text Saver',
                        text: 'File link added to text saver',
                        timeout: 3000
                    });
                }
                dragWindow.style.display = 'none';
                return;
            }
        }
    });

    fileButton.addEventListener('click', () => {
        dragWindow.style.display = dragWindow.style.display === 'none' ? 'block' : 'none';
    });

    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;
        }

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

        .selected-text-item {
            background-color: #add8e6;
        }

        @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;
        }
    `);

    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 Saver',
            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 copied: ${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(`Text deleted: ${deletedText.substring(0, 50)}${deletedText.length > 50 ? '...' : ''}`, 'error');
        }
    }

   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(`Text changed: ${newText.substring(0, 50)}${newText.length > 50 ? '...' : ''}`);
        }
    }

    function deleteAllTexts() {
        savedTexts = [];
        lastCopiedIndex = -1;
        editingIndex = -1;
        updateTextContainer();
        showMessage('All texts deleted!', 'error');
    }

    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();
            }
        }
    });
})();