Gitlab monkey copy

simple copy to cliboard formatted commit message

  1. // ==UserScript==
  2. // @name Gitlab monkey copy
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description simple copy to cliboard formatted commit message
  6. // @author You
  7. // @match http*://www.gitlab.com/*
  8. // @match http*://*.gitlab.com/*
  9. // @match http*://gitlab.com/*
  10. // @grant GM_addStyle
  11. // @run-at document-idle
  12. // @license https://creativecommons.org/licenses/by-sa/4.0
  13. // ==/UserScript==
  14.  
  15. const GIT_COPY_BUTTON_CLASS = 'superbutton';
  16. const GIT_COPY_BUTTON_MSG = 'Copy as Git commit message 🚀';
  17. const GIT_COPY_BUTTON_MSG_SUCCESS = 'Copied. Great work and happy commit! 🎉';
  18. const ISSUE_NUMBER_SELECTOR = '[data-qa-selector="breadcrumb_current_link"] a';
  19. const ISSUE_TITLE_SELECTOR = '[data-testid="issue-title"]';
  20.  
  21. void new class {
  22. constructor() {
  23. this.init();
  24. this.addStyles();
  25. }
  26.  
  27. init() {
  28. let gitlabDetailPageEl = document.querySelector('.js-detail-page-description');
  29. if (!gitlabDetailPageEl) return;
  30.  
  31. this.createGitCopyButton(gitlabDetailPageEl);
  32.  
  33. let gitCopyButtonEl = document.querySelector(`.${GIT_COPY_BUTTON_CLASS}`);
  34.  
  35. this.events(gitCopyButtonEl);
  36. }
  37.  
  38. events(gitCopyButtonEl) {
  39. gitCopyButtonEl.addEventListener('click', (e) => {
  40. e.preventDefault();
  41.  
  42. let commitMessage = this.createCommitMessage();
  43.  
  44. commitMessage && this.copyTextToClipboard(commitMessage).then(() => {
  45. e.target.textContent = GIT_COPY_BUTTON_MSG_SUCCESS;
  46. });
  47. });
  48. }
  49.  
  50. async copyTextToClipboard(text) {
  51. try {
  52. await navigator.clipboard.writeText(text);
  53. } catch (err) {
  54. console.error('Failed to copy: ', err);
  55. }
  56. }
  57.  
  58. createCommitMessage() {
  59. let issueNum = document.querySelector(`${ISSUE_NUMBER_SELECTOR}`);
  60. let issueTitle = document.querySelector(`${ISSUE_TITLE_SELECTOR}`);
  61.  
  62. return 'textContent' in issueNum && issueTitle && `FIX:${issueNum.textContent.replace('#','')} - ${issueTitle.textContent}`
  63. }
  64.  
  65. createGitCopyButton(parentEl) {
  66. parentEl.insertAdjacentHTML('afterbegin', `<button class="${GIT_COPY_BUTTON_CLASS}">${GIT_COPY_BUTTON_MSG}</button>`);
  67. }
  68.  
  69. addStyles() {
  70. GM_addStyle(`
  71. .${GIT_COPY_BUTTON_CLASS} {
  72. display: inline-block;
  73. outline: 0;
  74. cursor: pointer;
  75. border: 1px solid #000;
  76. border-radius: 6px;
  77. color: #000;
  78. background: #fff;
  79. font-size: 16px;
  80. font-weight: 500;
  81. line-height: 1.6;
  82. padding: 8px 20px;
  83. margin: 20px 0 30px;
  84. text-align:center;
  85. transition-duration: .15s;
  86. transition-property: all;
  87. transition-timing-function: cubic-bezier(.4,0,.2,1);
  88. }
  89. .${GIT_COPY_BUTTON_CLASS}:hover{
  90. background: rgb(251, 193, 245);
  91. }
  92. `);
  93. }
  94. }