Zendesk Window Title Improved

Improves the browser window title when using zendesk agent by adding info like ticket id.

目前为 2024-09-03 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Zendesk Window Title Improved
  3. // @namespace http://www.software-architects.at
  4. // @description Improves the browser window title when using zendesk agent by adding info like ticket id.
  5. // @match https://*.zendesk.com/agent/*
  6. // @grant none
  7. // @version 1.7.1
  8. // @author Leo; Original Author: Simon
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. var currentSection = null;
  13. var isSectionPresent = false;
  14. var initialWindowTitle = null;
  15.  
  16. function getTitle() {
  17. "use strict";
  18.  
  19. var tabs = $("div[data-test-id='header-tablist']");
  20. if (tabs.length === 1) {
  21. var selectedTabs = tabs.find("div[data-selected='true']");
  22. if (selectedTabs.length === 1) {
  23. var tabHeader = selectedTabs.find("div[data-test-id='header-tab-title']");
  24. if (tabHeader.length === 1) {
  25. return tabHeader[0].innerText;
  26. }
  27. } else {
  28. console.debug('ZendeskWindowTitle: getTitle: selected tab not found');
  29. }
  30. } else {
  31. console.debug('ZendeskWindowTitle: getTitle: tabs not found');
  32. }
  33.  
  34. return null;
  35. }
  36.  
  37. function getTicketInformation() {
  38. "use strict";
  39.  
  40. var title = null;
  41. var user = null;
  42. var org = null;
  43.  
  44. var mainPanes = $('#main_panes');
  45. if (mainPanes.length === 1) {
  46. var div = mainPanes.children('div.ember-view.workspace').not('[style*="none"]');
  47. if (div.length === 1) {
  48. var nav = div.find('nav.ember-view.btn-group');
  49. if (nav.length === 1) {
  50. var buttons = nav.children('span.btn');
  51. if (buttons.length === 3) {
  52. user = $(buttons[1]).text().trim();
  53. if (!$(buttons[0]).hasClass('create')) {
  54. org = $(buttons[0]).text().trim();
  55. } else {
  56. console.debug('ZendeskWindowTitle: getTicketInformation: no org');
  57. }
  58.  
  59. title = getTitle();
  60. } else {
  61. console.debug('ZendeskWindowTitle: getTicketInformation: buttons not found');
  62. }
  63. } else {
  64. console.debug('ZendeskWindowTitle: getTicketInformation: nav not found');
  65. }
  66. } else {
  67. console.debug('ZendeskWindowTitle: getTicketInformation: div not found');
  68. }
  69. } else {
  70. console.debug('ZendeskWindowTitle: getTicketInformation: main panes not found');
  71. }
  72.  
  73. if (title && user) {
  74. if (org) {
  75. return title + ' - ' + user + ' - ' + org;
  76. } else {
  77. return title + ' - ' + user;
  78. }
  79. }
  80.  
  81. return null;
  82. }
  83.  
  84. function updateWindowTitle() {
  85. "use strict";
  86. if (!isSectionPresent) {
  87. if (window.document.title === 'Zendesk...') {
  88. console.debug('ZendeskWindowTitle: dummy window title present');
  89. return;
  90. } else if (Zd.hasOwnProperty('section')) {
  91. isSectionPresent = true;
  92. initialWindowTitle = window.document.title;
  93. console.debug('ZendeskWindowTitle: section present');
  94. } else {
  95. console.debug('ZendeskWindowTitle: section still missing');
  96. return;
  97. }
  98. }
  99.  
  100. if (Zd.section !== currentSection) {
  101. if (!Zd.section) {
  102. currentSection = Zd.section;
  103. console.debug('ZendeskWindowTitle: empty section');
  104. window.document.title = initialWindowTitle;
  105. } else if (Zd.section.indexOf('tickets/') === 0) {
  106. var id = Zd.section.substring(8);
  107. console.debug('ZendeskWindowTitle: focused ticket: ' + id);
  108.  
  109. var info = getTicketInformation();
  110. if (info) {
  111. currentSection = Zd.section;
  112. window.document.title = '#' + id + ' - ' + info;
  113. } else {
  114. // something did not check out, ensure that we query again
  115. currentSection = null;
  116. window.document.title = '#' + id;
  117. }
  118. } else {
  119. currentSection = Zd.section;
  120. console.debug('ZendeskWindowTitle: focused: ' + Zd.section);
  121. window.document.title = initialWindowTitle + ' - ' + currentSection;
  122. }
  123. }
  124. }
  125.  
  126. window.setInterval(updateWindowTitle, 1000);