Greasy Fork

Greasy Fork is available in English.

Perplexity 代码框限制高度

限制Perplexity网站代码框高度为400px并添加复制按钮

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Perplexity 代码框限制高度
// @name:en      Perplexity Code Height Limiter
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  限制Perplexity网站代码框高度为400px并添加复制按钮
// @description:en  Limit code block height to 400px and add copy button on Perplexity
// @author       Dost
// @match        https://www.perplexity.ai/*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Add styles for code block and copy button
    GM_addStyle(`
        pre {
            max-height: 400px !important;
            overflow-y: auto !important;
            position: relative !important;
        }

        .copy-button {
            position: sticky !important;
            bottom: 10px !important;
            right: 10px !important;
            float: right !important;
            background-color: #4a5568 !important;
            color: white !important;
            border: none !important;
            border-radius: 4px !important;
            padding: 5px 10px !important;
            cursor: pointer !important;
            opacity: 0 !important;
            transition: opacity 0.3s ease !important;
            z-index: 1000 !important;
        }

        pre:hover .copy-button {
            opacity: 1 !important;
        }

        .copy-button:hover {
            background-color: #2d3748 !important;
        }
    `);

    // Function to create and add copy button
    function addCopyButtonToCodeBlocks() {
        const codeBlocks = document.querySelectorAll('pre');

        codeBlocks.forEach(pre => {
            if (!pre.querySelector('.copy-button')) {
                const copyButton = document.createElement('button');
                copyButton.textContent = '复制';
                copyButton.className = 'copy-button';

                copyButton.addEventListener('click', async () => {
                    const code = pre.textContent.replace('复制', '').trim();
                    try {
                        await navigator.clipboard.writeText(code);
                        copyButton.textContent = '已复制!';
                        setTimeout(() => {
                            copyButton.textContent = '复制';
                        }, 2000);
                    } catch (err) {
                        copyButton.textContent = '复制失败';
                        setTimeout(() => {
                            copyButton.textContent = '复制';
                        }, 2000);
                    }
                });

                pre.appendChild(copyButton);
            }
        });
    }

    // Create observer to handle dynamically loaded content
    const observer = new MutationObserver(() => {
        addCopyButtonToCodeBlocks();
    });

    // Start observing
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Initial run
    addCopyButtonToCodeBlocks();
})();