Bilibili Cookie Getter

获取哔哩哔哩网站的cookie

当前为 2024-12-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bilibili Cookie Getter
// @namespace    http://tampermonkey.net/
// @version      0.3
// @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) {
        return cookieStr;
    }

    // 显示通知
    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();
    }
})();