JombY / GitLab Accidental merge accept dialog

// ==UserScript==
// @name         GitLab Accidental merge accept dialog
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Workaround for https://gitlab.com/gitlab-org/gitlab/-/issues/29735
// @author       JombY
// @author       Martin
// @license      Apache-2.0
// @include      /https?:\/\/gitlab.*/merge_requests/.*
// @icon         https://www.google.com/s2/favicons?domain=gitlab.com
// @grant        none
// ==/UserScript==

/* jshint esversion: 8 */

const processedClass = '__CONFIRM_ADDED__';

(function () {
  const observer = new MutationObserver(mutations => {
    mutations.forEach(mutation => {
      mutation.addedNodes.forEach(node => {
        if (node.nodeType === Node.ELEMENT_NODE) process(node);
      });
    });
  });

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

function process(el) {
  el.querySelectorAll(`button.accept-merge-request:not(.${processedClass})`).forEach(mergeButton => {
    mergeButton.classList.add(processedClass);
    addConfirmDialog(mergeButton, "Are you sure you want to MERGE?");
    console.log('Found merge button', mergeButton);
  });
}

function addConfirmDialog(el, text) {
  el.addEventListener('click', e => {
    if (!confirm(text)) {
      e.stopImmediatePropagation();
    }
  }, true);
}