Better GitHub Gists Code Area

Resizes the editor to fit the viewport height, subtracting 500px. Handles window resizing.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Better GitHub Gists Code Area
// @namespace    https://github.com/palemoky
// @version      20250325
// @description  Resizes the editor to fit the viewport height, subtracting 500px. Handles window resizing.
// @author       Palemoky
// @license      Apache
// @match        https://gist.github.com/*
// @icon         https://raw.githubusercontent.com/homarr-labs/dashboard-icons/refs/heads/main/webp/github.webp
// @run-at       document-body
// @grant        none
// ==/UserScript==

'use strict';

function adjustCodeMirrorHeight() {
    requestAnimationFrame(() => {
        const codeMirrorDiv = document.querySelector('.CodeMirror');

        if (codeMirrorDiv && codeMirrorDiv.CodeMirror) {
            const newHeight = document.documentElement.clientHeight - 457;
            codeMirrorDiv.style.height = Math.max(0, newHeight) + 'px';
            codeMirrorDiv.CodeMirror.refresh();
        }
    });
}

function debounce(func, wait) {
    let timeout;
    return function(...args) {
        const context = this;
        clearTimeout(timeout);
        timeout = setTimeout(() => func.apply(context, args), wait);
    };
}

const debouncedAdjustCodeMirrorHeight = debounce(adjustCodeMirrorHeight, 200);

setTimeout(debouncedAdjustCodeMirrorHeight, 100);

const observer = new MutationObserver(debouncedAdjustCodeMirrorHeight);
observer.observe(document.body, { childList: true, subtree: true });

window.addEventListener('resize', debouncedAdjustCodeMirrorHeight);