Better GitHub Gists Code Area

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);