Skip to content

Instantly share code, notes, and snippets.

@shtrom
Created April 21, 2021 07:57
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 shtrom/b8e4f81e9fc67f6459279a991f0e6a08 to your computer and use it in GitHub Desktop.
Save shtrom/b8e4f81e9fc67f6459279a991f0e6a08 to your computer and use it in GitHub Desktop.
GreaseMonkey script to fix ZenDesk conversation threads so they are not top-posted
// ==UserScript==
// @name Zendesk Conversation Fixer
// @version 2
// @grant none
// @match https://*.zendesk.com/agent/tickets/*
// ==/UserScript==
function fix(eventContainers) {
console.log("GreaseMonkey ZCF: fixing ...");
const nContainers = eventContainers.length;
for (var i=0; i<nContainers; i++) {
var container = eventContainers[i];
const children = container.getElementsByClassName("event");
var topNode = children[0];
// In case there are more divs on the way...
div = topNode.parentNode;
for (var j=1; j<children.length; j++) {
var currentNode = children[j]
div.insertBefore(currentNode, topNode);
topNode = currentNode;
}
container.insertAdjacentHTML("afterbegin",
'<div style="font-weight: bold; text-align: center; ">The following discussion is now in bottom-posting order</div>'
);
inputNode = container.parentNode.getElementsByClassName("comment_input");
if (inputNode) {
container.insertAdjacentElement("afterend", inputNode[0]);
inputNode[0].scrollIntoView();
}
}
console.log("GreaseMonkey ZCF: fixed.");
};
function fixOrRequeue(count, containerClass, elementClass, fixFunction) {
console.log("GreaseMonkey fixOrRequeue: waiting for ." + containerClass + '//.' + elementClass + "[] ...");
setTimeout(function() {
const events = document.getElementsByClassName(containerClass);
const newCount = events.length;
if (newCount > 0 && newCount == count) {
console.log("GreaseMonkey fixOrRequeue: fixing...");
fixFunction(document.getElementsByClassName(elementClass));
} else {
fixOrRequeue(newCount, containerClass, elementClass, fixFunction);
}
}, 2000);
};
window.addEventListener('load', function() {
setTimeout(fixOrRequeue(-1, 'event', 'event-container', fix), 2000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment