Grok Code Style

Shrink, collapse, compact, and download pre/code blocks on Grok pages

目前为 2025-03-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         Grok Code Style
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Shrink, collapse, compact, and download pre/code blocks on Grok pages
// @author       You
// @match        https://x.com/i/grok*
// @match        https://grok.com/*
// @match        https://grok.x.ai/*
// @match        https://x.ai/*
// @exclude      https://gf.qytechs.cn/*
// @exclude      https://*.org/*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加樣式
    GM_addStyle(`
        .code-wrapper {
            margin: 5px 0;
        }
        .code-header {
            display: flex;
            justify-content: space-between;
            background-color: #e0e0e0;
            padding: 2px 5px;
            border: 1px solid #ddd;
            cursor: pointer;
            font-size: 11px;
        }
        .code-content {
            max-height: 150px;
            overflow-y: auto;
            font-size: 11px;
            line-height: 1.2;
            background-color: #f5f5f5;
            padding: 4px;
            border: 1px solid #ddd;
            border-top: none;
            display: none; /* 預設摺疊 */
        }
        .code-content.expanded {
            display: block;
        }
        .download-btn {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 2px 5px;
            cursor: pointer;
            font-size: 10px;
        }
        .download-btn:hover {
            background-color: #45a049;
        }
    `);

    // 處理代碼塊
    function processCodeBlocks() {
        const codeElements = document.querySelectorAll(':not([class*="highlight"]:not([id*="highlight"])) > pre, :not([class*="highlight"]:not([id*="highlight"])) > code');
        codeElements.forEach((element, index) => {
            if (element.parentNode.classList.contains('code-wrapper')) return; // 避免重複處理

            // 創建包裹容器
            const wrapper = document.createElement('div');
            wrapper.className = 'code-wrapper';

            // 創建標頭(摺疊控制)
            const header = document.createElement('div');
            header.className = 'code-header';
            header.textContent = `代碼塊 ${index + 1} (點擊展開/摺疊)`;

            // 創建下載按鈕
            const downloadBtn = document.createElement('button');
            downloadBtn.className = 'download-btn';
            downloadBtn.textContent = '下載';
            downloadBtn.addEventListener('click', (e) => {
                e.stopPropagation(); // 防止觸發摺疊
                const blob = new Blob([element.textContent], { type: 'text/plain' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `code_block_${index + 1}.txt`;
                a.click();
                URL.revokeObjectURL(url);
            });
            header.appendChild(downloadBtn);

            // 移動原始代碼到新容器
            const content = document.createElement('div');
            content.className = 'code-content';
            content.appendChild(element.cloneNode(true));
            
            // 替換原始元素
            element.parentNode.replaceChild(wrapper, element);
            wrapper.appendChild(header);
            wrapper.appendChild(content);

            // 摺疊/展開事件
            header.addEventListener('click', () => {
                content.classList.toggle('expanded');
            });
        });
    }

    // 監聽頁面變化
    const observer = new MutationObserver(() => processCodeBlocks());
    observer.observe(document.body, { childList: true, subtree: true });
    processCodeBlocks(); // 初始執行
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址