Zendesk Window Title

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Zendesk Window Title
// @namespace   http://www.software-architects.at
// @description Improves the browser window title when using zendesk agent by adding info like ticket id.
// @match       https://*.zendesk.com/agent/*
// @grant       none
// @version     1.8
// @copyright   2014-2024 software architects gmbh
// @author      Simon
// ==/UserScript==

var currentSection = null;
var isSectionPresent = false;
var initialWindowTitle = null;

function getTitle() {
    "use strict";

    var tabs = $("div[data-test-id='header-toolbar']");
    if (tabs.length === 1) {
        var selectedTabs = tabs.find("div[data-selected='true']");
        if (selectedTabs.length === 1) {
            var tabHeader = selectedTabs.find("div[data-test-id='header-tab-title']");
            if (tabHeader.length === 1) {
                return tabHeader[0].innerText;
            }
        } else {
            console.debug('ZendeskWindowTitle: getTitle: selected tab not found');
        }
    } else {
        console.debug('ZendeskWindowTitle: getTitle: tabs not found');
    }

    return null;
}

function getTicketInformation() {
    "use strict";

    var title = null;
    var user = null;
    var org = null;

    var mainPanes = $('#main_panes');
    if (mainPanes.length === 1) {
        var div = mainPanes.children('div.ember-view.workspace').not('[style*="none"]');
        if (div.length === 1) {
            var nav = div.find('nav.ember-view.btn-group');
            if (nav.length === 1) {
                var buttons = nav.children('span.btn');
                if (buttons.length === 3) {
                    user = $(buttons[1]).text().trim();
                    if (!$(buttons[0]).hasClass('create')) {
                        org = $(buttons[0]).text().trim();
                    } else {
                        console.debug('ZendeskWindowTitle: getTicketInformation: no org');
                    }

                    title = getTitle();
                } else {
                    console.debug('ZendeskWindowTitle: getTicketInformation: buttons not found');
                }
            } else {
                console.debug('ZendeskWindowTitle: getTicketInformation: nav not found');
            }
        } else {
            console.debug('ZendeskWindowTitle: getTicketInformation: div not found');
        }
    } else {
        console.debug('ZendeskWindowTitle: getTicketInformation: main panes not found');
    }

    if (title && user) {
        if (org) {
            return title + ' - ' + user + ' - ' + org;
        } else {
            return title + ' - ' + user;
        }
    }

    return null;
}

function updateWindowTitle() {
    "use strict";
    if (!isSectionPresent) {
        if (window.document.title === 'Zendesk...') {
            console.debug('ZendeskWindowTitle: dummy window title present');
            return;
        } else if (Zd.hasOwnProperty('section')) {
            isSectionPresent = true;
            initialWindowTitle = window.document.title;
            console.debug('ZendeskWindowTitle: section present');
        } else {
            console.debug('ZendeskWindowTitle: section still missing');
            return;
        }
    }

    if (Zd.section !== currentSection) {
        if (!Zd.section) {
            currentSection = Zd.section;
            console.debug('ZendeskWindowTitle: empty section');
            window.document.title = initialWindowTitle;
        } else if (Zd.section.indexOf('tickets/') === 0) {
            var id = Zd.section.substring(8);
            console.debug('ZendeskWindowTitle: focused ticket: ' + id);

            var info = getTicketInformation();
            if (info) {
                currentSection = Zd.section;
                window.document.title = initialWindowTitle + ' - #' + id + ' - ' + info;
            } else {
                // something did not check out, ensure that we query again
                currentSection = null;
                window.document.title = initialWindowTitle + ' - #' + id;
            }
        } else {
            currentSection = Zd.section;
            console.debug('ZendeskWindowTitle: focused: ' + Zd.section);
            window.document.title = initialWindowTitle + ' - ' + currentSection;
        }
    }
}

window.setInterval(updateWindowTitle, 1000);