Skip to content

Instantly share code, notes, and snippets.

@elliotboney
Last active February 6, 2024 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotboney/3b9823cd7567067e7ee0edcf42364b3f to your computer and use it in GitHub Desktop.
Save elliotboney/3b9823cd7567067e7ee0edcf42364b3f to your computer and use it in GitHub Desktop.
Freshdesk Ticket Detail
// ==UserScript==
// @name Freshdesk Ticket Detail
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Freshdesk hacks
// @author Elliot Boney
// @match https://*.freshdesk.com/a/tickets/*
// @grant unsafeWindow
// @run-at document-end
// @updateURL https://gist.github.com/elliotboney/3b9823cd7567067e7ee0edcf42364b3f/raw/freshdesk.user.js
// @downloadURL https://gist.github.com/elliotboney/3b9823cd7567067e7ee0edcf42364b3f/raw/freshdesk.user.js
// @icon https://www.activepieces.com/_next/image?url=https%3A%2F%2Fcdn.activepieces.com%2Fpieces%2Ffreshdesk.png&w=128&q=75
// ==/UserScript==
(function($) {
let debouncing = false;
function addStuff() {
/**
* Debounce this being called twice when changing pages and such
*/
if ($('.tickets__list').length == 0) {
console.log('No tickets...')
setTimeout(addStuff,3000)
return false;
} else {
console.log("Running addstuff...")
}
// console.log('list of tickets',$('.tickets__list'));
unsafeWindow.$('.tickets__list').each(function(i) {
var $this = $(this);
if ($this.find('.elliotpreview').length) {
return true;
} else {
}
// console.log("LOG "+i+": ",$this);
// $this.css({'background-color':"pink"});
let ticketId =$this.find('[data-test-ticket-id]').data("testTicketId")
console.log("AMC: ",ticketId)
getStatus(ticketId, $this)
})
setTimeout(addStuff,5000)
}
function getStatus(id, $ticket) {
if (typeof id === "undefined") {
return false;
}
let storedTicket = localStorage.getItem(id);
if (storedTicket === null) {
$.ajax({
url: "https://thepaperlessagent.freshdesk.com/api/_/tickets/"+id+"/latest_note"
}).done(function(x,y,z) {
if ($ticket.find('.elliotpreview').length) {
return false;
}
if (typeof x.ticket !== "undefined") {
let html = "";
if (x.ticket.description.indexOf('Klaviyo ') > -1) {
html = $($.parseHTML(x.ticket.description)).find('.em_main_table').eq(2).html();
// console.log($($.parseHTML(x.ticket.description)).find('.em_main_table').eq(2).html());
} else {
html = x.ticket.description;
}
$('[data-test-ticket-content="'+id+'"]').append(addDiv(html));
localStorage.setItem(id,html)
} else if (typeof x.conversation !== "undefined") {
$('[data-test-ticket-content="'+id+'"]').append(addDiv('<div>'+x.conversation.user.name+' said:</div><hr/>'+x.conversation.body));
localStorage.setItem(id,"<div>"+x.conversation.user.name+' said:</div><hr/>'+x.conversation.body)
} else {
console.log("unhandled response",x)
}
$ticket.addClass("processed")
})
.fail(function() {
console.log("error with getStatus, id: "+id)
})
.always(function() {
$ticket.addClass("processed")
});
} else {
if ($ticket.find('.elliotpreview').length) {
return false;
}
console.log('restoring ticket '+id+' from local storage')
$('[data-test-ticket-content="'+id+'"]').append(addDiv(storedTicket));
$ticket.addClass("processed")
}
}
function addDiv(content) {
return '<div class="col-md-11 elliotpreview" style="max-height: 150px; overflow-y: scroll; overflow-x: scroll; margin-left: 90px; margin-top: 21px; border: 1px solid #d0d0d0; padding: 10px; background-color: #f5f5f5">'+content+'<div>';
}
let options = {
childList: true,
attributes: true,
characterData: false,
subtree: false,
attributeFilter: ['attr1', 'attr2'],
attributeOldValue: false,
characterDataOldValue: false
};
let observer = new MutationObserver(logMutations);
var timesChecked = 0;
checkPageReady();
function checkPageReady() {
timesChecked++;
if (typeof $('.page-content.ember-view').get(0) === 'object') {
pageReady();
} else {
console.info('Waiting for tickets...');
setTimeout(checkPageReady, 100);
}
}
function pageReady() {
observer.observe($('body').get(0), options);
}
function logMutations(mutations) {
// console.log("LOG: ",mutations);
for (let mutation of mutations) {
if (mutation.target.className == "ember-application") {
addStuff();
}
}
}
})(unsafeWindow.$)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment