github merge blocker

Disable merge buttons for the base branches you wish to restrict.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         github merge blocker
// @namespace    [email protected]
// @version      2024-05-10
// @description  Disable merge buttons for the base branches you wish to restrict.
// @author       maoanran
// @match        https://github.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let RESTRICTED_BASE_BRANCHES = ['main', 'master'] // Change the array to add more branches to prevent merging

    function runWhenReady(readySelector, callback) {
        var numAttempts = 0;
        var tryNow = function() {
            var elem = document.querySelector(readySelector);
            if (elem) {
                callback(elem);
            } else {
                numAttempts++;
                if (numAttempts >= 10) {
                    console.warn('Giving up after 10 attempts. Could not find: ' + readySelector);
                } else {
                    setTimeout(tryNow, 250 * Math.pow(1.1, numAttempts));
                }
            }
        };
        tryNow();
    }

    let baseBranch = document.querySelectorAll('.commit-ref.base-ref span');

    if (baseBranch.length == 1 && RESTRICTED_BASE_BRANCHES.includes(baseBranch[0].innerText)) {
        runWhenReady('.BtnGroup button', function() {
            document.querySelectorAll('.BtnGroup button').forEach(button => button.setAttribute("disabled","disabled"));
        })
    }

})();