milanfarkas / Gitlab MR info to clipboard

// ==UserScript==
// @name         Gitlab MR info to clipboard
// @version      2.0.4
// @copyright    2023 - Milan Farkas, Dominik Varadi, Peter Avarkeszi, Zoltan Szepe / WRD Labs Zrt. (https://www.wrd.hu)
// @namespace    www.wrd.hu
// @description  This adds a button to GitLab to allow easy copying of MR info to Clipboard. For self hosted instances add your GitLab URL to the user includes section, eg. /https?:\/\/gitlab.yourdomain.com/.*/merge_requests/.* Tested with GitLab v15+
// @author       milanfarkas
// @license      MIT
// @include      /https?:\/\/gitlab.com/.*/merge_requests/.*
// @require      https://code.jquery.com/jquery-3.5.0.min.js
// @require      https://rawgit.com/notifyjs/notifyjs/master/dist/notify.js
// @updateURL    https://openuserjs.org/meta/milanfarkas/Gitlab_MR_info_to_clipboard.meta.js
// @downloadURL  https://openuserjs.org/install/milanfarkas/Gitlab_MR_info_to_clipboard.user.js
// ==/UserScript==

$(function () {
  $('.detail-page-header-actions').prepend(
    "<button class='d-none d-sm-none d-md-block btn btn-grouped copy-task-button' style='margin-right:0.5rem'>Copy MR</button>");
  $(".copy-task-button").click(getTaskKeyAndTitle);
});

function getTaskKeyAndTitle() {
  let titleH1 = $(".detail-page-header-body h1.title");

  if (titleH1.length === 0) {
    titleH1 = $("h2.qa-title");
  }
  let copyHtml = titleH1.html();
  let copyTextBase = titleH1.text().trim();
  copyHtml += " - <a href='" + window.location.href + "'>" + window.location.href + "</a>";
  let copyText = copyTextBase + " - " + window.location.href;
  copyToClip(copyHtml, copyText);
  $.notify(copyTextBase, 'success');
}

function copyToClip(htmlText, plainText) {
  function listener(e) {
    e.clipboardData.setData("text/html", htmlText);
    e.clipboardData.setData("text/plain", plainText);
    e.preventDefault();
  }
  document.addEventListener("copy", listener);
  document.execCommand("copy");
  document.removeEventListener("copy", listener);
}

(function () {
  'use strict';
})();