Cobmais - DevOps

Botão para Copiar o Nome da Tarefa ao Criar Pull request

  1. // ==UserScript==
  2. // @name Cobmais - DevOps
  3. // @namespace http://app.cobmais.com.br/
  4. // @version 1.0.0
  5. // @description Botão para Copiar o Nome da Tarefa ao Criar Pull request
  6. // @author Rodrigo Mescua
  7. // @match http*://*dev.azure.com/cobmais/*/pullrequestcreate*
  8. // @icon https://raw.githubusercontent.com/romesc/cobmais-utils/master/favicon.ico
  9. // @require http://code.jquery.com/jquery-3.4.1.min.js
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js
  11. // @grant GM_addStyle
  12. // @grant GM_setClipboard
  13. // ==/UserScript==
  14. this.$ = this.jQuery = jQuery.noConflict(true) // eslint-disable-line no-undef
  15. waitForKeyElements ("body > div.full-size > div > div > div.flex-row.flex-grow.scroll-hidden > div.flex-column.flex-grow.scroll-hidden > div.v-scroll-auto.flex.flex-grow.relative.region-content > div > div.repos-pr-create-form.page-content.page-content-top.flex-column.flex-grow.flex-noshrink.rhythm-vertical-16 > div > div > div > div.rhythm-vertical-16.region-createPullRequestOverviewExtensions > div > div.bolt-table-container.flex-grow > table > tbody > a > td.bolt-table-two-line-cell.bolt-table-cell.bolt-list-cell > div > div:nth-child(1) > span.flex-grow.text-ellipsis", tarefaFunction);
  16. waitForKeyElements ("button.btntarefafinal", tarefaCopy);
  17.  
  18. function tarefaFunction (jNode) {
  19. var eventos = document.querySelectorAll('body > div.full-size > div > div > div.flex-row.flex-grow.scroll-hidden > div.flex-column.flex-grow.scroll-hidden > div.v-scroll-auto.flex.flex-grow.relative.region-content > div > div.repos-pr-create-form.page-content.page-content-top.flex-column.flex-grow.flex-noshrink.rhythm-vertical-16 > div > div > div > div.rhythm-vertical-16.region-createPullRequestOverviewExtensions > div > div.bolt-table-container.flex-grow > table > tbody > a > td.bolt-table-two-line-cell.bolt-table-cell.bolt-list-cell > div > div:nth-child(1) > span.flex-grow.text-ellipsis');
  20. var teste = '';
  21. for ( var i = 0; i < eventos.length; i++ ) {
  22. var nomeTarefa = eventos[i].innerText.replace('Desenvolvimento ','');
  23. teste = '<button data-clipboard-text="' + nomeTarefa + '" style="cursor: auto;" class="bolt-header-command-item-button bolt-button bolt-icon-button enabled subtle bolt-focus-treatment" id="btnTarefaCob" type="button"><span class="left-icon flex-noshrink fabric-icon ms-Icon--Paste medium"></span><span class="bolt-button-text body-m">Copiar</span></button>';
  24. if (i == eventos.length - 1) {
  25. teste = '<button data-clipboard-text="' + nomeTarefa + '" style="cursor: auto;" class="btntarefafinal bolt-header-command-item-button bolt-button bolt-icon-button enabled subtle bolt-focus-treatment" id="btnTarefaCob" type="button"><span class="left-icon flex-noshrink fabric-icon ms-Icon--Paste medium"></span><span class="bolt-button-text body-m">Copiar</span></button>';
  26. }
  27. if (eventos[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML.indexOf(teste) == -1){
  28. eventos[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML = eventos[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML.concat(teste);
  29. }
  30. }
  31. }
  32.  
  33. function tarefaCopy (teste) {
  34. var btns1 = document.querySelectorAll('button.btntarefafinal');
  35. var clipboard = new ClipboardJS(btns1);
  36.  
  37. clipboard.on('success', function(e) {
  38. console.log('Nome da Tarefa copiado para a Área de Transferência');
  39. return;
  40. });
  41. }
  42.  
  43. /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
  44. that detects and handles AJAXed content.
  45.  
  46. Usage example:
  47.  
  48. waitForKeyElements (
  49. "div.comments"
  50. , commentCallbackFunction
  51. );
  52.  
  53. //--- Page-specific function to do what we want when the node is found.
  54. function commentCallbackFunction (jNode) {
  55. jNode.text ("This comment changed by waitForKeyElements().");
  56. }
  57.  
  58. IMPORTANT: This function requires your script to have loaded jQuery.
  59. */
  60. function waitForKeyElements (selectorTxt, actionFunction, bWaitOnce, iframeSelector) {
  61. var targetNodes, btargetsFound;
  62.  
  63. if (typeof iframeSelector == "undefined") {
  64. targetNodes = $(selectorTxt);
  65. }
  66. else {
  67. targetNodes = $(iframeSelector).contents().find(selectorTxt);
  68. }
  69.  
  70. if (targetNodes && targetNodes.length > 0) {
  71. btargetsFound = true;
  72. /*--- Found target node(s). Go through each and act if they
  73. are new.
  74. */
  75. targetNodes.each ( function () {
  76. var jThis = $(this);
  77. var alreadyFound = jThis.data ('alreadyFound') || false;
  78.  
  79. if (!alreadyFound) {
  80. //--- Call the payload function.
  81. var cancelFound = actionFunction (jThis);
  82. if (cancelFound)
  83. btargetsFound = false;
  84. else
  85. jThis.data ('alreadyFound', true);
  86. }
  87. } );
  88. }
  89. else {
  90. btargetsFound = false;
  91. }
  92.  
  93. //--- Get the timer-control variable for this selector.
  94. var controlObj = waitForKeyElements.controlObj || {};
  95. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  96. var timeControl = controlObj [controlKey];
  97.  
  98. //--- Now set or clear the timer as appropriate.
  99. if (btargetsFound && bWaitOnce && timeControl) {
  100. //--- The only condition where we need to clear the timer.
  101. clearInterval (timeControl);
  102. delete controlObj [controlKey]
  103. }
  104. else {
  105. //--- Set a timer, if needed.
  106. if ( ! timeControl) {
  107. timeControl = setInterval ( function () {
  108. waitForKeyElements ( selectorTxt,
  109. actionFunction,
  110. bWaitOnce,
  111. iframeSelector
  112. );
  113. },
  114. 300
  115. );
  116. controlObj [controlKey] = timeControl;
  117. }
  118. }
  119. waitForKeyElements.controlObj = controlObj;
  120. }