Bilibili Cookie Getter

获取哔哩哔哩网站的cookie

目前为 2024-12-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         Bilibili Cookie Getter
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  获取哔哩哔哩网站的cookie
// @author       Your name
// @match        *://*.bilibili.com/*
// @grant        GM_setClipboard
// @grant        GM_notification
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';
    
    // 重要cookie列表
    const IMPORTANT_COOKIES = [
        'DedeUserID',
        'bili_jct',
        'SESSDATA',
        'bili_ticket',
        'sid'
    ];
    
    // 新增:指定的cookie
    const SPECIFIED_COOKIE = 'single_unread';
    
    // 格式化cookie字符串
    function formatCookies(cookieStr) {
        const cookieMap = new Map();
        cookieStr.split(';').forEach(cookie => {
            const [key, value] = cookie.trim().split('=');
            cookieMap.set(key, value);
        });

        // 首先添加重要的cookie
        let result = '// 重要Cookie:\n';
        IMPORTANT_COOKIES.forEach(key => {
            if (cookieMap.has(key)) {
                result += `${key}=${cookieMap.get(key)};\n`;
                cookieMap.delete(key);
            }
        });

        // 添加其他cookie
        result += '\n// 其他Cookie:\n';
        cookieMap.forEach((value, key) => {
            result += `${key}=${value};\n`;
        });

        return result;
    }

    // 只获取重要cookie
    function getImportantCookies(cookieStr) {
        const cookieMap = new Map();
        cookieStr.split(';').forEach(cookie => {
            const [key, value] = cookie.trim().split('=');
            if (IMPORTANT_COOKIES.includes(key)) {
                cookieMap.set(key, value);
            }
        });

        return Array.from(cookieMap)
            .map(([key, value]) => `${key}=${value}`)
            .join(';\n');
    }

    // 获取指定cookie
    function getSpecifiedCookie(cookieStr) {
        const cookieMap = new Map();
        cookieStr.split(';').forEach(cookie => {
            const [key, value] = cookie.trim().split('=');
            if (key.startsWith(SPECIFIED_COOKIE)) {
                cookieMap.set(key, value);
            }
        });

        if (cookieMap.size === 0) {
            return `${SPECIFIED_COOKIE}?build=0&mobi_app=web&unread_type=0`;
        }

        return Array.from(cookieMap)
            .map(([key, value]) => `${key}=${value}`)
            .join(';\n');
    }

    // 显示通知
    function showNotification(message) {
        const toast = document.createElement('div');
        toast.style.cssText = `
            position: fixed;
            top: 70px;
            right: 80px;
            background-color: rgba(34, 34, 34, 0.9);
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            z-index: 999999999;
            font-size: 14px;
            font-family: Arial, sans-serif;
            animation: fadeInOut 2s ease-in-out;
        `;
        
        const style = document.createElement('style');
        style.textContent = `
            @keyframes fadeInOut {
                0% { opacity: 0; transform: translateY(-20px); }
                15% { opacity: 1; transform: translateY(0); }
                85% { opacity: 1; transform: translateY(0); }
                100% { opacity: 0; transform: translateY(-20px); }
            }
        `;
        document.head.appendChild(style);
        
        toast.textContent = message;
        document.body.appendChild(toast);
        
        setTimeout(() => {
            toast.remove();
            style.remove();
        }, 2000);
    }

    // 创建按钮
    function addButtons() {
        const container = document.createElement('div');
        container.style.cssText = `
            position: fixed;
            top: 70px;
            right: 20px;
            display: flex;
            gap: 10px;
            z-index: 999999;
        `;

        // 完整Cookie按钮
        const fullButton = document.createElement('button');
        fullButton.textContent = '获取Cookie';
        fullButton.style.cssText = `
            padding: 8px 16px;
            background: #fb7299;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
        `;

        // 简易Cookie按钮
        const simpleButton = document.createElement('button');
        simpleButton.textContent = '简易获取';
        simpleButton.style.cssText = `
            padding: 8px 16px;
            background: #00a1d6;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
        `;

        // 新增:指定Cookie按钮
        const specifiedButton = document.createElement('button');
        specifiedButton.textContent = '获取指定Cookie';
        specifiedButton.style.cssText = `
            padding: 8px 16px;
            background: #6c757d;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
        `;

        // 添加点击事件
        fullButton.onclick = function() {
            const cookies = formatCookies(document.cookie);
            GM_setClipboard(cookies);
            showNotification('完整Cookie已复制到剪贴板');
            console.log('完整Cookies:\n', cookies);
        };

        simpleButton.onclick = function() {
            const cookies = getImportantCookies(document.cookie);
            GM_setClipboard(cookies);
            showNotification('重要Cookie已复制到剪贴板');
            console.log('重要Cookies:\n', cookies);
        };

        specifiedButton.onclick = function() {
            const cookies = getSpecifiedCookie(document.cookie);
            if (cookies) {
                GM_setClipboard(cookies);
                showNotification('指定Cookie已复制到剪贴板');
                console.log('指定Cookie:\n', cookies);
            } else {
                showNotification('未找到指定Cookie');
                console.log('未找到指定Cookie');
            }
        };

        container.appendChild(fullButton);
        container.appendChild(simpleButton);
        container.appendChild(specifiedButton);
        document.body.appendChild(container);
    }

    // 等待页面加载完成后添加按钮
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', addButtons);
    } else {
        addButtons();
    }
})();