Gitlab expand all discussions

Add a new button to expand all discussions

  1. // ==UserScript==
  2. // @name Gitlab expand all discussions
  3. // @description Add a new button to expand all discussions
  4. // @include https://gitlab.*/*
  5. // @require https://code.jquery.com/jquery-3.3.1.slim.min.js
  6. // @version 1
  7. // @grant none
  8. // @namespace https://gf.qytechs.cn/users/4947
  9. // ==/UserScript==
  10.  
  11. // https://gitlab.com/gitlab-org/gitlab-ce/issues/19149#note_103194373
  12.  
  13. 'use strict';
  14.  
  15. const intervalDelay = 1000;
  16. const buttonDelay = 200;
  17.  
  18. const State = Object.freeze({
  19. EMPTY: 'EMPTY',
  20. SOME_CLOSE: 'SOME_CLOSE',
  21. NO_CLOSE: 'NO_CLOSE',
  22. });
  23.  
  24. const textMap = Object.freeze({
  25. [State.SOME_CLOSE]: 'expand all discussions',
  26. [State.NO_CLOSE]: 'collapse all discussions',
  27. });
  28.  
  29. (function ready() {
  30. const templateDiv = $('.line-resolve-all');
  31.  
  32. if (!templateDiv.length)
  33. {
  34. setTimeout(ready, intervalDelay);
  35. return;
  36. }
  37.  
  38. function getToggleState() {
  39. const all = $('.discussion-toggle-button');
  40. if (!all.length) return State.EMPTY;
  41.  
  42. const closed = all.filter(':has(i.fa-chevron-down)');
  43.  
  44. return closed.length ? State.SOME_CLOSE : State.NO_CLOSE;
  45. }
  46.  
  47. function updateDiv(state) {
  48. if (!(state in State)) state = getToggleState();
  49.  
  50. if (state === State.EMPTY) {
  51. newDiv.hide();
  52. return;
  53. }
  54.  
  55. newDiv.find('.line-resolve-text > a').text(textMap[state]);
  56. }
  57.  
  58. function updateDivDelay() {
  59. setTimeout(updateDiv, buttonDelay);
  60. setTimeout(updateDiv, buttonDelay * 2);
  61. }
  62.  
  63. const newDiv = templateDiv.clone();
  64.  
  65. newDiv.find('.is-disabled').remove();
  66. newDiv.find('.line-resolve-text').attr('type', 'button').text('').append('<a>')
  67. newDiv.click(() => {
  68. const state = getToggleState();
  69. let nextState;
  70.  
  71. switch (state) {
  72. case State.SOME_CLOSE:
  73. $('.discussion-toggle-button:has(i.fa-chevron-down)').click();
  74. nextState = State.NO_CLOSE;
  75. break;
  76. case State.NO_CLOSE:
  77. $('.discussion-toggle-button').click();
  78. nextState = State.SOME_CLOSE;
  79. break;
  80. }
  81.  
  82. updateDiv(nextState);
  83. });
  84.  
  85. updateDiv();
  86.  
  87. templateDiv.after(newDiv);
  88.  
  89. $(document).on('click', 'button', updateDivDelay);
  90.  
  91. // Should we add toggle button? seems not useful
  92. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址