Freshdesk Monitor

Alerts you if the responder of the ticket you are viewing changes, there is a new ticket or one updates, and auto-refreshes the ticket page. New ticket and updated ticket notifications only work when you have a ticket list open in a tab

当前为 2016-07-29 提交的版本,查看 最新版本

// ==UserScript==
// @name         Freshdesk Monitor
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Alerts you if the responder of the ticket you are viewing changes, there is a new ticket or one updates, and auto-refreshes the ticket page. New ticket and updated ticket notifications only work when you have a ticket list open in a tab
// @author       Adrian Bradfield
// @match        https://*.freshdesk.com/*
// @grant        unsafeWindow
// @grant        GM_notification
// ==/UserScript==

/*
Created by Adrian Bradfield at Pastel UK & Ireland
Suppliers of accounting software
Visit: http://pastel.co.uk for more information
*/

(function() {
    function notify(title, text, highlight){
        GM_notification({title: title, text: text, highlight: highlight, timeout: 0, image: "https://s3.amazonaws.com/cdn.freshdesk.com/data/helpdesk/attachments/production/5003361067/logo/PASTEL%20AltMed.jpg?AWSAccessKeyId=AKIAJ2JSYZ7O3I4JO6DA&Expires=1472024111&Signature=k3vIkf0S7S%2BvHYsHqbuHPg5pd5c%3D"});
    }
    //Uncomment this code to test notifications
    /*unsafeWindow.$J("body").append("<button id='notification_allow'>Test Notifications</button>");
    unsafeWindow.$J("#notification_allow").click(function(){
        notify("Test Notification", "This is to check that notifications are being displayed as intended");
    });*/

    var fd_monitor_curpath;
    var fd_monitor_original;
    window.setInterval(function(){
        //Auto refresh pages
        var refreshAlert = unsafeWindow.$J("#index_refresh_alert");
        if(refreshAlert.is(":visible")){
            var updates = refreshAlert.children("#update_message").first().attr("data-count");
            var newt = refreshAlert.children("#new_ticket_message").first().attr("data-count");
            if(updates){
                console.log("update detected");
                var results = [];
                unsafeWindow.$J("table.tickets tr").each(function(row){
                    if(unsafeWindow.$J( this ).html().indexOf("source-detailed-auto-refresh") >= 0){
                        results.push(unsafeWindow.$J( this ));
                    }
                });
                for(var i = 0; i < results.length; i++){
                    notify("Updated Ticket", results[i].find(".ticket-description-tip").text(), false);
                }
            }
            if(newt){
                var string = '' + newt + ' new ticket';
                if(newt > 1){string += "s";}
                notify("New Tickets", string, false);
            }
            //notify("Freshdesk", "New or updated ticket", false);
            refreshAlert.click();//Re-enable after analysis
        }
        //Monitor for Agent Changes
        if(window.location.pathname.startsWith("/helpdesk/tickets/")){
            if(window.location.pathname != fd_monitor_curpath){
                fd_monitor_original = dom_helper_data.helpdesk_ticket.responder_name;
                fd_monitor_curpath = window.location.pathname;
                console.log("this tickets agent is: " + fd_monitor_original);
            }
            unsafeWindow.$J.ajax({url: window.location, success: function(result){
                var nodes = unsafeWindow.$J.parseHTML(result);

                var re = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;

                var match;
                while (match = re.exec(result)) {
                    if(match[1].indexOf("dom_helper_data = {") > -1){
                        re= /"responder_name":"([\s\S]*?)"/gm;
                        var responder_name = re.exec(match[1]);
                        if(fd_monitor_original != responder_name[1]){
                            notify("AGENT CHANGED", "This ticket has been claimed by "+responder_name[1]+"! Do not respond!", true)
                            //alert("This ticket has now been assigned to " + responder_name[1]);
                            fd_monitor_original = responder_name[1];
                        }
                        break;
                    }
                }
            }});
        }
    }, 5000);
})();