myconnectwise.net enhancements

Modifies Manage page to allow for collapsible ticket notes

目前為 2023-12-01 提交的版本,檢視 最新版本

  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.11
  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. // start by generating the primary controller buttons
  28. generateControls();
  29.  
  30. // wait an additonal couple of seconds for all ticket notes to finish loading
  31. setTimeout(function() {
  32. modifyNotes();
  33. }, 2000);
  34.  
  35. // disconnect observer
  36. return true;
  37. }
  38. });
  39.  
  40.  
  41. function generateControls(){
  42. let noteTab = document.querySelectorAll(".TicketNote-newNoteButton");
  43. let controlButtons = document.getElementsByClassName("control-button"); // make sure we don't add them more than once
  44.  
  45. if(noteTab[0] && controlButtons.length == 0) {
  46. noteTab[0].nextElementSibling.style.display = "none"; //hide the buttons that filter by ticket type - this conflicts with my changes and sometimes causes the entire notes section to disappear
  47. noteTab[0].after(createButton("Next"));
  48. noteTab[0].after(createButton("Previous"));
  49. noteTab[0].after(createButton("Expand All"));
  50. noteTab[0].after(createButton("Collapse All"));
  51. }
  52. }
  53.  
  54. // used for the controller buttons above the notes, they are all exactly the same except for their content
  55. function createButton(buttName){
  56. let bCss = "padding:3px; margin-left:12px; margin-bottom:0px; margin-top:22px; font-size:0.90em; width:125px";
  57. let butt;
  58.  
  59. butt = document.createElement("button");
  60. butt.classList.add("control-button");
  61. butt.style.cssText = bCss;
  62. butt.textContent = buttName;
  63. butt.addEventListener("click", buttonPress);
  64. return butt;
  65. }
  66.  
  67. // runs when a controller button is pressed, loops through ticket notes to set the display attribute
  68. function buttonPress(evt){
  69. let buttName = evt.currentTarget.textContent;
  70. let newDisplayState = 0;
  71. if (buttName == "Expand All") {
  72. newDisplayState = 1;
  73. }
  74. let coll = document.getElementsByClassName('collapsible');
  75. if (coll.length == 0) { // check if collapsibles have been wiped out (eg. by a ticket note refresh)
  76. modifyNotes();
  77. }
  78. let found = -1; // value to store the location of the first ticket note which is visible / not hidden
  79. for (let i = 0; i < coll.length; ++i) {
  80. let initialState = displayChange(coll[i], newDisplayState);
  81.  
  82. if(newDisplayState == 0 && initialState != "none" && found < 0) {
  83. found = i;
  84. }
  85. }
  86.  
  87. if(buttName == "Previous") {
  88. if (found > 0) { // open the note prior to the one that was open before the 'previous' button was clicked, unless...
  89. displayChange(coll[found-1], 1);
  90. } else { // ...if no note open, or the first note was open, then open the final note
  91. displayChange(coll[coll.length-1], 1);
  92. }
  93. } else if (buttName == "Next") {
  94. 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
  95. displayChange(coll[found+1], 1);
  96. } else { // otherwise open the first note
  97. displayChange(coll[0], 1);
  98. }
  99. }
  100. }
  101.  
  102. function modifyNotes(){
  103. const rowWrap = document.querySelectorAll('.TicketNote-rowWrap');
  104. rowWrap.forEach((rowItem) => {
  105.  
  106. // create button to be placed above each ticket note
  107. let butt = document.createElement("button");
  108. butt.classList.add("collapsible");
  109. butt.style.cssText = 'padding:3px; margin-bottom:0px; margin-top:0px; width:100%; font-size:0.90em';
  110. butt.innerHTML = "";
  111.  
  112. let basicName = classText(rowItem, "TicketNote-basicName", "<strong>", "</strong>")
  113. let clickableName = classText(rowItem, "TicketNote-clickableName", "<strong>", "</strong>")
  114. butt.innerHTML += basicName + clickableName; // add name to button
  115.  
  116. // let timeDateText = classText(rowItem, "TimeText-date", " [","]"); // copy date to button without any changes - nah
  117. // let's instead change date to Australian locale:
  118. let timeDateChild = rowItem.getElementsByClassName("TimeText-date");
  119. if (timeDateChild[0]) {
  120. let timeDateText = timeDateChild[0].textContent;
  121. let timeDateHTML = timeDateChild[0].innerHTML;
  122. const regexDate = new RegExp(/(\d{1,2})\/(\d{1,2})\/(\d{4})/, "g"); //matches date, connectwise uses (en-US) M/D/YYYY format
  123. //let timeDateFormat = timeDateText.replace(regexDate, "$2/$1/$3"); // simple method to just swap month and day around - nah let's go all in
  124. let timeDateMatch = regexDate.exec(timeDateText);
  125. 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
  126. const dateOptions = {
  127. weekday: "long",
  128. year: "numeric",
  129. month: "long",
  130. day: "numeric",
  131. };
  132. let dateFormat = dateObject.toLocaleString("en-AU", dateOptions);
  133. let timeDateFormat = timeDateText.replace(regexDate, dateFormat); //adds the time info back to formatted date
  134. butt.innerHTML += " [" + timeDateFormat + "]"; //adds date to button
  135. //timeDateChild[0].innerHTML = timeDateChild[0].innerHTML.replace(timeDateText, timeDateFormat); //replaces the existing date inside ticket note with formatted date - nah
  136. timeDateChild[0].innerHTML = timeDateChild[0].innerHTML.replace(timeDateText, ""); // no need to double up, we can just delete the existing date
  137. }
  138.  
  139. // this is the pill shaped icon that shows for 'resolved' or 'internal' notes
  140. let pill = rowItem.getElementsByClassName("TicketNote-pill");
  141. let pillText = "";
  142. if (pill[0]) {
  143. pillText = pill[0].innerText;
  144. }
  145.  
  146. // set custom styles for each ticket button
  147. if (pillText == "Internal") {
  148. butt.style.cssText += ";background-color:#026CCF;color:#DDEEFF;text-align:right";
  149. } else if (pillText == "Resolution") {
  150. butt.style.cssText += ";background-color:#549c05;color:#DDFFCC;text-align:right";
  151. } else if (clickableName.length > 0) { //only falco team have this class
  152. butt.style.cssText += ";background-color:#AABBEE;color:#3366AA;text-align:right";
  153. } else if (basicName.length > 0) { // only end users have this class
  154. butt.style.cssText += ";background-color:#CCAAEE;color:#6644AA;text-align:left";
  155. }
  156.  
  157. //rowItem.style.removeProperty('margin-top');
  158. rowItem.style.cssText = "margin-top:6px";
  159. // rowItem.parentElement.insertBefore(butt, rowItem); //previous method, works but causes issues
  160. // instead we place the button inside the "TicketNote-rowWrap" class so that it will get wiped if the ticket notes are refreshed
  161. let rowChild = rowItem.getElementsByClassName("TicketNote-row");
  162. if (rowChild[0]) {
  163. rowChild[0].before(butt);
  164. }
  165.  
  166. } );
  167.  
  168. //add click listener functions to all collapsible buttons
  169. var coll = document.getElementsByClassName("collapsible");
  170. for (let i = 0; i < coll.length; i++) {
  171. coll[i].addEventListener("click", function() {
  172. this.classList.toggle("active");
  173. displayChange(this, -1);
  174. });
  175. }
  176. }
  177.  
  178. // toggle the display state of the sibling element that is directly after the passed parameter
  179. // (We use this to hide or show the 'TicketNote-row' located directly after the 'collapsible' button in the DOM)
  180. function displayChange(collapsible, state = -1) {
  181. // state -1 toggle, 0 off, 1 on
  182. let content = collapsible.nextElementSibling;
  183. let initialState = content.style.display;
  184. if ((state != 0 && initialState === "none")) {
  185. content.style.display = "flex";
  186. } else if (state < 1) {
  187. content.style.display = "none";
  188. }
  189. return initialState; // returns the state the element was in *prior* to being changed
  190. }
  191.  
  192.  
  193. // find first matching classString that is a child of the startParent, return its innerText with optional pre/postfix text
  194. // ended up not using this much, special handling required too often
  195. function classText(startParent, classString, prefix = "", postfix = "") {
  196. let foundChild = startParent.getElementsByClassName(classString);
  197. if (foundChild[0]) {
  198. return prefix + foundChild[0].innerText + postfix;
  199. } else {
  200. return "";
  201. }
  202. }
  203.  
  204. // Press Control-i in order to regenerate the controls and buttons
  205. VM.shortcut.register('c-i', () => {
  206. generateControls();
  207. modifyNotes();
  208. // just a reminder to self on ways to debug output:
  209. // console.log('You just pressed Ctrl-I');
  210. // alert("I am an alert box!");
  211. });
  212.  
  213.