myconnectwise.net enhancements

Modifies Manage page to allow for collapsible ticket notes

目前为 2023-12-18 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name myconnectwise.net enhancements
  3. // @namespace Violentmonkey Scripts
  4. // @match https://aus.myconnectwise.net/v2022_2*ise.aspx
  5. // @grant none
  6. // @version 2.14
  7. // @author mike-Inside
  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, ""); // we could just delete the existing date, but people may want to copy/paste it
  136. timeDateFormat = "<span style=\"color:#fff;opacity:0.1\">" + timeDateFormat + "<\/span>"; //date can still be selected and copied, but is not too noticible
  137. timeDateChild[0].innerHTML = timeDateChild[0].innerHTML.replace(timeDateText, timeDateFormat); //replaces the existing date inside ticket note with formatted date
  138. }
  139.  
  140. // this is the pill shaped icon that shows for 'resolved' or 'internal' notes
  141. let pill = rowItem.getElementsByClassName("TicketNote-pill");
  142. let pillText = "";
  143. if (pill[0]) {
  144. pillText = pill[0].innerText;
  145. }
  146.  
  147. //rowItem.style.removeProperty('margin-top');
  148. rowItem.style.cssText = "margin-top:6px";
  149.  
  150. // set custom styles for each ticket button
  151. if (pillText == "Internal") {
  152. butt.style.cssText += ";background-color:#026CCF;color:#DDEEFF;text-align:right";
  153. rowItem.style.cssText += ";background-color:#ecf2ff";
  154. } else if (pillText == "Resolution") {
  155. butt.style.cssText += ";background-color:#549c05;color:#DDFFCC;text-align:right";
  156. rowItem.style.cssText += ";background-color:#ecfff2";
  157. } else if (clickableName.length > 0) { //only falco team have this class
  158. butt.style.cssText += ";background-color:#AABBEE;color:#3366AA;text-align:right";
  159. } else if (basicName.length > 0) { // only end users have this class
  160. butt.style.cssText += ";background-color:#CCAAEE;color:#6644AA;text-align:left";
  161. }
  162. // rowItem.parentElement.insertBefore(butt, rowItem); //previous method, works but causes issues
  163. // instead we place the button inside the "TicketNote-rowWrap" class so that it will get wiped if the ticket notes are refreshed
  164. let rowChild = rowItem.getElementsByClassName("TicketNote-row");
  165. if (rowChild[0]) {
  166. rowChild[0].before(butt);
  167. }
  168.  
  169. } );
  170.  
  171. //add click listener functions to all collapsible buttons
  172. var coll = document.getElementsByClassName("collapsible");
  173. for (let i = 0; i < coll.length; i++) {
  174. coll[i].addEventListener("click", function() {
  175. this.classList.toggle("active");
  176. displayChange(this, -1);
  177. });
  178. }
  179. }
  180.  
  181. // toggle the display state of the sibling element that is directly after the passed parameter
  182. // (We use this to hide or show the 'TicketNote-row' located directly after the 'collapsible' button in the DOM)
  183. function displayChange(collapsible, state = -1) {
  184. // state -1 toggle, 0 off, 1 on
  185. let content = collapsible.nextElementSibling;
  186. let initialState = content.style.display;
  187. if ((state != 0 && initialState === "none")) {
  188. content.style.display = "flex";
  189. } else if (state < 1) {
  190. content.style.display = "none";
  191. }
  192. return initialState; // returns the state the element was in *prior* to being changed
  193. }
  194.  
  195.  
  196. // find first matching classString that is a child of the startParent, return its innerText with optional pre/postfix text
  197. // ended up not using this much, special handling required too often
  198. function classText(startParent, classString, prefix = "", postfix = "") {
  199. let foundChild = startParent.getElementsByClassName(classString);
  200. if (foundChild[0]) {
  201. return prefix + foundChild[0].innerText + postfix;
  202. } else {
  203. return "";
  204. }
  205. }
  206.  
  207. // Press Control-i in order to regenerate the controls and buttons
  208. VM.shortcut.register('c-i', () => {
  209. generateControls();
  210. modifyNotes();
  211. // just a reminder to self on ways to debug output:
  212. // console.log('You just pressed Ctrl-I');
  213. // alert("I am an alert box!");
  214. });
  215.  
  216.