GitLab tree selector

Select current file name in the MR tree

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         GitLab tree selector
// @namespace    http://tampermonkey.net/
// @version      2025-03-05
// @description  Select current file name in the MR tree
// @author       nw
// @match        https://gitlab.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gitlab.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const style = document.createElement('style');
    style.innerHTML = `
    .gl-truncate-component {
        font-weight: initial;
    }
    .diff-file-row.is-active .gl-truncate-component {
        font-weight: bold;
    }
    `;
    document.body.appendChild(style);

    const observer = new MutationObserver((mutations, obs) => {
        const treeList = document.querySelector('.mr-tree-list');

        if (!treeList) {
            return;
        }

        window.addEventListener('scroll', function(event) {
            const treeFiles = {};

            document.querySelectorAll('.mr-tree-list .file-row:not(.folder)').forEach(treeFile => {
                const fileName = treeFile.querySelector('.gl-truncate-component').textContent.replace(/[\u200B-\u200F\u202A-\u202E\u2060-\u206F]/g, '').trim();

                treeFile.style.removeProperty("background-color");
                //treeFile.classList.remove("is-active");

                treeFiles[fileName] = treeFile;
            });

            const frontFileHeaders = document.querySelectorAll('.diff-files-holder .vue-recycle-scroller__item-view .file-title');
            const paginationHeaderHeight = document.querySelector('.top-bar-fixed').getBoundingClientRect().height;
            const titleHeaderHeight = document.querySelector('.issue-sticky-header').getBoundingClientRect().height;
            const offsetTop = paginationHeaderHeight + titleHeaderHeight;

            const filesBelowCenter = [];

            frontFileHeaders.forEach(frontFileTitleHeader => {
                const frontFileTitleHeaderTop = frontFileTitleHeader.getBoundingClientRect().top;
                const viewportCenter = ((window.innerHeight - offsetTop) / 2) + offsetTop;

                if (frontFileTitleHeaderTop > 0 && frontFileTitleHeaderTop < viewportCenter) {
                    filesBelowCenter.push(frontFileTitleHeader);
                }
            });

            if (filesBelowCenter.length === 0) {
                return;
            }

            const currentFile = filesBelowCenter[filesBelowCenter.length - 1];
            const filePaths = currentFile.querySelectorAll('.file-header-content > a strong');
            const filePath = filePaths[filePaths.length - 1].title;
            const filePathArray = filePath.split('/');
            const fileName = filePathArray[filePathArray.length - 1];

            if (treeFiles[fileName] === undefined) {
                return;
            }

            treeFiles[fileName].style.setProperty("background-color", "var(--gray-50, #ececef)");
        });

        obs.disconnect();
    });

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