GitLab tree selector

Select current file name in the MR tree

当前为 2025-03-05 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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/*merge_requests/*/diffs
// @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 });
})();