myconnectwise.net enhancements

Modifies Manage page to allow for collapsible ticket notes

目前为 2023-11-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name myconnectwise.net enhancements
  3. // @namespace Violentmonkey Scripts
  4. // @match https://aus.myconnectwise.net/v2022_2/connectwise.aspx
  5. // @grant none
  6. // @version 2.1
  7. // @author mikeInside
  8. // @description Modifies Manage page to allow for collapsible ticket notes
  9. // @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2
  10. // @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
  11. // ==/UserScript==
  12.  
  13. // watch the page for elements that are created dynamically and may not be ready even on document-end
  14. const disconnect = VM.observe(document.body, () => {
  15. // find the target node
  16. const node = document.querySelector('#SR_Service_RecID-input');
  17. const rowWrap = document.querySelectorAll('.TicketNote-rowWrap');
  18.  
  19. // runs on service board pages to stop autocomplete from blocking visibility
  20. if (node) {
  21. node.setAttribute("autocomplete", "off");
  22. return true;
  23. }
  24.  
  25. // runs on individual ticket pages
  26. if (rowWrap[0]) { // wait until the ticket notes have been generated before starting the script
  27.  
  28. let noteTab = document.querySelectorAll('.TicketNote-newNoteButton');
  29.  
  30. // start by generating the primary controller buttons
  31. if(noteTab[0]) {
  32. noteTab[0].after(createButton("Next"));
  33. noteTab[0].after(createButton("Previous"));
  34. noteTab[0].after(createButton("Expand All"));
  35. noteTab[0].after(createButton("Collapse All"));
  36. }
  37.  
  38. // wait an additonal couple of seconds for all ticket notes to finish loading
  39. setTimeout(function() {
  40. modifyNotes();
  41. }, 2000);
  42.  
  43. // disconnect observer
  44. return true;
  45. }
  46. });
  47.  
  48. // used for the controller buttons above the notes, they are all exactly the same except for their content
  49. function createButton(buttName){
  50. let bCss = "padding:3px; margin-left:12px; margin-bottom:0px; margin-top:22px; font-size:0.90em; width:125px";
  51. let butt;
  52.  
  53. butt = document.createElement("button");
  54. butt.style.cssText = bCss;
  55. butt.textContent = buttName;
  56. butt.addEventListener("click", buttonPress);
  57. return butt;
  58. }
  59.  
  60. // runs when a controller button is pressed, loops through ticket notes to set the display attribute
  61. function buttonPress(evt){
  62. let buttName = evt.currentTarget.textContent;
  63. let newDisplayState = 0;
  64. if (buttName == "Expand All") {
  65. newDisplayState = 1;
  66. }
  67. let coll = document.getElementsByClassName('collapsible');
  68. if (coll.length == 0) { // check if collapsibles have been wiped out (eg. by a ticket note refresh)
  69. modifyNotes();
  70. }
  71. let found = -1; // value to store the location of the first ticket note which is visible / not hidden
  72. for (let i = 0; i < coll.length; ++i) {
  73. let initialState = displayChange(coll[i], newDisplayState);
  74.  
  75. if(newDisplayState == 0 && initialState != "none" && found < 0) {
  76. found = i;
  77. }
  78. }
  79.  
  80. if(buttName == "Previous") {
  81. if (found > 0) { // open the note prior to the one that was open before the 'previous' button was clicked, unless...
  82. displayChange(coll[found-1], 1);
  83. } else { // ...if no note open, or the first note was open, then open the final note
  84. displayChange(coll[coll.length-1], 1);
  85. }
  86. } else if (buttName == "Next") {
  87. if (found < coll.length-1) { // if no note was open, or any note except the last note was open, then open the next note after the opened one
  88. displayChange(coll[found+1], 1);
  89. } else { // otherwise open the first note
  90. displayChange(coll[0], 1);
  91. }
  92. }
  93. }
  94.  
  95. function modifyNotes(){
  96. const rowWrap = document.querySelectorAll('.TicketNote-rowWrap');
  97. rowWrap.forEach((rowItem) => {
  98.  
  99. // create button to be placed above each ticket note
  100. let butt = document.createElement("button");
  101. butt.classList.add("collapsible");
  102. butt.style.cssText = 'padding:3px; margin-bottom:0px; margin-top:0px; width:100%; font-size:0.90em';
  103. butt.innerHTML = "";
  104.  
  105. let basicName = classText(rowItem, "TicketNote-basicName", "<strong>", "</strong>")
  106. let clickableName = classText(rowItem, "TicketNote-clickableName", "<strong>", "</strong>")
  107. butt.innerHTML += basicName + clickableName; // add name to button
  108.  
  109. // let timeDateText = classText(rowItem, "TimeText-date", " [","]"); // copy date to button without any changes - nah
  110. // let's instead change date to Australian locale:
  111. let timeDateChild = rowItem.getElementsByClassName("TimeText-date");
  112. if (timeDateChild[0]) {
  113. let timeDateText = timeDateChild[0].textContent;
  114. let timeDateHTML = timeDateChild[0].innerHTML;
  115. const regexDate = new RegExp(/(\d{1,2})\/(\d{1,2})\/(\d{4})/, "g"); //matches date, connectwise uses (en-US) M/D/YYYY format
  116. //let timeDateFormat = timeDateText.replace(regexDate, "$2/$1/$3"); // simple method to just swap month and day around - nah let's go all in
  117. let timeDateMatch = regexDate.exec(timeDateText);
  118. let dateObject = new Date(timeDateMatch[3],timeDateMatch[1]-1,timeDateMatch[2]); //creates JS Date object, note month parameter is inexplicably 0-11 to represent jan-dec
  119. const dateOptions = {
  120. weekday: "long",
  121. year: "numeric",
  122. month: "long",
  123. day: "numeric",
  124. };
  125. let dateFormat = dateObject.toLocaleString("en-AU", dateOptions);
  126. let timeDateFormat = timeDateText.replace(regexDate, dateFormat); //adds the time info back to formatted date
  127. butt.innerHTML += " [" + timeDateFormat + "]"; //adds date to button
  128. //timeDateChild[0].innerHTML = timeDateChild[0].innerHTML.replace(timeDateText, timeDateFormat); //replaces the existing date inside ticket note with formatted date - nah
  129. timeDateChild[0].innerHTML = timeDateChild[0].innerHTML.replace(timeDateText, ""); // no need to double up, we can just delete the existing date
  130. }
  131.  
  132. // this is the pill shaped icon that shows for 'resolved' or 'internal' notes
  133. let pill = rowItem.getElementsByClassName("TicketNote-pill");
  134. let pillText = "";
  135. if (pill[0]) {
  136. pillText = pill[0].innerText;
  137. }
  138.  
  139. // set custom styles for each ticket button
  140. if (pillText == "Internal") {
  141. butt.style.cssText += ";background-color:#026CCF;color:#DDEEFF;text-align:right";
  142. } else if (pillText == "Resolution") {
  143. butt.style.cssText += ";background-color:#549c05;color:#DDFFCC;text-align:right";
  144. } else if (clickableName.length > 0) { //only falco team have this class
  145. butt.style.cssText += ";background-color:#AABBEE;color:#3366AA;text-align:right";
  146. } else if (basicName.length > 0) { // only end users have this class
  147. butt.style.cssText += ";background-color:#CCAAEE;color:#6644AA;text-align:left";
  148. }
  149.  
  150. //rowItem.style.removeProperty('margin-top');
  151. rowItem.style.cssText = "margin-top:6px";
  152. // rowItem.parentElement.insertBefore(butt, rowItem); //previous method, works but causes issues
  153. // instead we place the button inside the "TicketNote-rowWrap" class so that it will get wiped if the ticket notes are refreshed
  154. let rowChild = rowItem.getElementsByClassName("TicketNote-row");
  155. if (rowChild[0]) {
  156. rowChild[0].before(butt);
  157. }
  158.  
  159. } );
  160.  
  161. //add click listener functions to all collapsible buttons
  162. var coll = document.getElementsByClassName("collapsible");
  163. for (let i = 0; i < coll.length; i++) {
  164. coll[i].addEventListener("click", function() {
  165. this.classList.toggle("active");
  166. displayChange(this, -1);
  167. });
  168. }
  169. }
  170.  
  171. // toggle the display state of the sibling element that is directly after the passed parameter
  172. // (We use this to hide or show the 'TicketNote-row' located directly after the 'collapsible' button in the DOM)
  173. function displayChange(collapsible, state = -1) {
  174. // state -1 toggle, 0 off, 1 on
  175. let content = collapsible.nextElementSibling;
  176. let initialState = content.style.display;
  177. if ((state != 0 && initialState === "none")) {
  178. content.style.display = "flex";
  179. } else if (state < 1) {
  180. content.style.display = "none";
  181. }
  182. return initialState; // returns the state the element was in *prior* to being changed
  183. }
  184.  
  185.  
  186. // find first matching classString that is a child of the startParent, return its innerText with optional pre/postfix text
  187. // ended up not using this much, special handling required too often
  188. function classText(startParent, classString, prefix = "", postfix = "") {
  189. let foundChild = startParent.getElementsByClassName(classString);
  190. if (foundChild[0]) {
  191. return prefix + foundChild[0].innerText + postfix;
  192. } else {
  193. return "";
  194. }
  195. }