Skip to content

Instantly share code, notes, and snippets.

@pesla
Created October 21, 2014 07:56
Show Gist options
  • Save pesla/f8e5995f3af3c2472b85 to your computer and use it in GitHub Desktop.
Save pesla/f8e5995f3af3c2472b85 to your computer and use it in GitHub Desktop.
Open ticket in Zendesk from custom id field with TamperMonkey
// ==UserScript==
// @name Zendesk ID-field
// @namespace https://procurios.zendesk.com/agent
// @version 0.1
// @description Adds ID-field to Zendesk
// @match https://procurios.zendesk.com/agent/*
// ==/UserScript==
var ZD = ZD || {};
ZD.idField = (function () {
// List of used variables
var elements = {},
tabInterval;
// List of used functions
var init, buildComponent, removeInterval, onInputKeyDown;
init = function () {
// First, initialize
elements.tabContainer = document.querySelector("#tabs");
if (elements.tabContainer) {
buildComponent();
} else {
tabInterval = window.setInterval( function () {
if (document.querySelector("#tabs")) {
elements.tabContainer = document.querySelector("#tabs");
buildComponent();
removeInterval();
}
}, 500);
}
};
buildComponent = function () {
// Add input field to tab component
var liElem = document.createElement("li"),
inputElem = document.createElement("input"),
liStyle = "position: absolute; top: 9px; right: 60px; width: 50px; z-index: 10000000;",
inputStyle = "padding: 3px; border: 1px solid #a8ca4a; border-radius: 3px;";
liElem.setAttribute("id", "zd-open-ticket-by-id");
liElem.setAttribute("style", liStyle);
inputElem.setAttribute("type", "text");
inputElem.setAttribute("placeholder", "#");
inputElem.setAttribute("style", inputStyle);
liElem.appendChild(inputElem);
elements.tabContainer.appendChild(liElem);
// Attach listener to input field
inputElem.addEventListener("keydown", onInputKeyDown);
elements.inputElem = inputElem;
};
removeInterval = function () {
clearInterval(tabInterval);
};
onInputKeyDown = function (event) {
var key = event.keyCode,
val = elements.inputElem.value;
if (key === 13) {
// Return key
if (!isNaN(parseFloat(val)) && isFinite(val)) {
// Value is a numer
Zd.Routes.goToHashNow("#/tickets/" + val);
elements.inputElem.value = "";
}
}
};
return {
init: init
};
}());
document.addEventListener("DOMContentLoaded", function() {
ZD.idField.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment