Cookie Manager Pro

Advanced cookie management with import/export

// ==UserScript==
// @name         Cookie Manager Pro
// @namespace    http://tampermonkey.net/
// @version      3.0
// @description  Advanced cookie management with import/export
// @author       Your Name
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建界面元素
    const floatBtn = createFloatButton();
    const manager = createManagerPanel();
    let isDragging = false;

    // 初始化事件
    initDragEvents();

    function createFloatButton() {
        const btn = document.createElement('div');
        btn.innerHTML = '🍪';
        btn.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            width: 40px;
            height: 40px;
            background: #2196F3;
            color: white;
            border-radius: 50%;
            text-align: center;
            line-height: 40px;
            cursor: move;
            z-index: 9999;
            box-shadow: 0 2px 5px rgba(0,0,0,0.3);
            user-select: none;
        `;
        document.body.appendChild(btn);
        return btn;
    }

    function createManagerPanel() {
        const panel = document.createElement('div');
        panel.style.cssText = `
            display: none;
            position: fixed;
            background: #f5f5f5;
            border: 1px solid #ddd;
            border-radius: 8px;
            width: 85%;
            max-width: 450px;
            max-height: 80vh;
            overflow: hidden;
            z-index: 9998;
            padding: 15px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        `;
        panel.innerHTML = `
            <div style="margin-bottom:15px;font-weight:bold;color:#333;">
                Cookie管理 - <span id="currentDomain">${location.hostname}</span>
            </div>
            <div style="display:flex;gap:10px;margin-bottom:12px;">
                <button onclick="exportCookies()" style="flex:1;padding:8px;background:#2196F3;color:white;border:none;border-radius:4px;cursor:pointer">
                    导出
                </button>
                <button onclick="importCookies()" style="flex:1;padding:8px;background:#4CAF50;color:white;border:none;border-radius:4px;cursor:pointer">
                    导入
                </button>
            </div>
            <div id="cookieContent" style="background:white;border-radius:6px;padding:10px;height:200px;overflow:auto;margin-bottom:12px;">
                <!-- Cookie列表将在此显示 -->
            </div>
            <div style="background:#fff;padding:12px;border-radius:6px;">
                <div style="display:flex;gap:8px;margin-bottom:8px;">
                    <input type="text" id="newName" placeholder="名称" style="flex:1;padding:6px;">
                    <input type="text" id="newValue" placeholder="值" style="flex:1;padding:6px;">
                </div>
                <button onclick="addCookie()" style="width:100%;padding:8px;background:#FF9800;color:white;border:none;border-radius:4px;cursor:pointer">
                    添加Cookie
                </button>
            </div>
        `;
        document.body.appendChild(panel);
        return panel;
    }

    // 初始化拖动事件
    function initDragEvents() {
        let startX, startY, initialX, initialY;

        floatBtn.addEventListener('mousedown', (e) => {
            isDragging = true;
            startX = e.clientX;
            startY = e.clientY;
            initialX = floatBtn.offsetLeft;
            initialY = floatBtn.offsetTop;
            floatBtn.style.transition = 'none';
        });

        document.addEventListener('mousemove', (e) => {
            if (!isDragging) return;
            const dx = e.clientX - startX;
            const dy = e.clientY - startY;
            floatBtn.style.left = `${initialX + dx}px`;
            floatBtn.style.top = `${initialY + dy}px`;
            adjustPanelPosition();
        });

        document.addEventListener('mouseup', () => {
            isDragging = false;
            floatBtn.style.transition = 'all 0.3s ease';
        });

        floatBtn.addEventListener('click', () => {
            if (isDragging) return;
            manager.style.display = manager.style.display === 'none' ? 'block' : 'none';
            adjustPanelPosition();
            refreshCookies();
        });
    }

    function adjustPanelPosition() {
        const btnRect = floatBtn.getBoundingClientRect();
        const panelWidth = manager.offsetWidth;
        const viewportWidth = window.innerWidth;
        
        let leftPos = btnRect.left - panelWidth/2 + 20;
        if (leftPos < 10) leftPos = 10;
        if (leftPos + panelWidth > viewportWidth - 10) {
            leftPos = viewportWidth - panelWidth - 10;
        }
        
        manager.style.left = `${leftPos}px`;
        manager.style.top = `${btnRect.top - manager.offsetHeight - 20}px`;
    }

    // 刷新Cookie显示
    function refreshCookies() {
        const content = manager.querySelector('#cookieContent');
        content.innerHTML = document.cookie.split(';').map(cookie => {
            const [name, value] = cookie.trim().split('=');
            return `
                <div style="display:flex;align-items:center;padding:8px;border-bottom:1px solid #eee;">
                    <div style="flex:1;min-width:0;">
                        <div style="color:#2196F3;font-size:14px;overflow:hidden;text-overflow:ellipsis">${name}</div>
                        <div style="color:#666;font-size:12px;overflow:hidden;text-overflow:ellipsis">${decodeURIComponent(value)}</div>
                    </div>
                    <div style="display:flex;gap:6px;margin-left:10px;">
                        <button onclick="editCookie('${name}')" style="padding:4px 8px;background:#2196F3;color:white;border:none;border-radius:3px;cursor:pointer">
                            编辑
                        </button>
                        <button onclick="deleteCookie('${name}')" style="padding:4px 8px;background:#f44336;color:white;border:none;border-radius:3px;cursor:pointer">
                            删除
                        </button>
                    </div>
                </div>
            `;
        }).join('');
    }

    // 导出功能
    window.exportCookies = function() {
        const cookies = document.cookie.split(';').map(cookie => {
            const [name, value] = cookie.trim().split('=');
            return `${name}=${decodeURIComponent(value)}`;
        }).join('\n');

        const ta = document.createElement('textarea');
        ta.value = cookies;
        document.body.appendChild(ta);
        ta.select();
        document.execCommand('copy');
        document.body.removeChild(ta);
        alert('Cookie已复制到剪贴板');
    }

    // 导入功能
    window.importCookies = function() {
        const input = prompt('粘贴要导入的Cookie(每行一个Cookie):');
        if (!input) return;

        input.split('\n').forEach(line => {
            const [name, value] = line.split('=').map(s => s.trim());
            if (name && value) {
                setCookie(name, value);
            }
        });
        refreshCookies();
        alert(`成功导入${input.split('\n').length}个Cookie`);
    }

    // 公共方法
    window.addCookie = function() {
        const name = document.getElementById('newName').value;
        const value = document.getElementById('newValue').value;
        if (name && value) {
            setCookie(name, value);
            document.getElementById('newName').value = '';
            document.getElementById('newValue').value = '';
        }
    }

    window.editCookie = function(name) {
        const currentValue = getCookie(name);
        const newName = prompt('新名称:', name);
        const newValue = prompt('新值:', currentValue);
        
        if (newName && newValue !== null) {
            deleteCookie(name);
            setCookie(newName, newValue);
        }
    }

    window.deleteCookie = function(name) {
        if (confirm(`确定删除 ${name} 吗?`)) {
            document.cookie = `${encodeURIComponent(name)}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
            refreshCookies();
        }
    }

    // Cookie操作函数
    function setCookie(name, value, days = 365) {
        const date = new Date();
        date.setTime(date.getTime() + (days * 86400000));
        document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}; expires=${date.toUTCString()}; path=/`;
        refreshCookies();
    }

    function getCookie(name) {
        const cookie = document.cookie
            .split('; ')
            .find(row => row.startsWith(`${name}=`));
        return cookie ? decodeURIComponent(cookie.split('=')[1]) : null;
    }
})()