Skip to content

Instantly share code, notes, and snippets.

@AlanVncs
Created October 5, 2022 21:31
Show Gist options
  • Select an option

  • Save AlanVncs/5ad3d1d04cda7a1769c534b36f899810 to your computer and use it in GitHub Desktop.

Select an option

Save AlanVncs/5ad3d1d04cda7a1769c534b36f899810 to your computer and use it in GitHub Desktop.
function sortZendeskItems() {
const NAME_INDEX = 5
const DATE_INDEX = 6
const $tbody = document.querySelector('tbody[data-garden-id="tables.body"]')
const $trs = document.querySelectorAll('tr[data-garden-id="tables.row"]')
if (!$tbody || $trs.length === 0) return false
const $sorted = [...$trs].sort(($tr1, $tr2) => {
const name1 = $tr1.querySelector(`td:nth-child(${NAME_INDEX})`).textContent
const date1 = $tr1.querySelector(`td:nth-child(${DATE_INDEX}) time`).getAttribute('datetime')
const name2 = $tr2.querySelector(`td:nth-child(${NAME_INDEX})`).textContent
const date2 = $tr2.querySelector(`td:nth-child(${DATE_INDEX}) time`).getAttribute('datetime')
const nameCompare = name1.localeCompare(name2)
// Nomes iguais
if (nameCompare === 0) {
// Datas iguais ou diferentes
return date1.localeCompare(date2)
}
// Nomes diferentes
return nameCompare
})
$tbody.append(...$sorted)
return true
}
let $tbodyOld = null
let $trsOld = []
function areSameNodes($tbody, $trs) {
if (!$tbody || !$tbodyOld || $trs.length == 0 || $trsOld.length == 0) return false
if (!$tbody.isSameNode($tbodyOld)) return false
const areSameTrs = $trs.every(($tr, index) => {
return $tr.isSameNode($trsOld[index])
})
return areSameTrs
}
const observer = new MutationObserver(() => {
const $tbody = document.querySelector('tbody[data-garden-id="tables.body"]')
const $trs = [...document.querySelectorAll('tr[data-garden-id="tables.row"]')]
if (!areSameNodes($tbody, $trs)) {
window.console.log('Ordenando itens')
try {
sortZendeskItems()
} catch (_) {}
}
$tbodyOld = $tbody
$trsOld = $trs
})
observer.observe(document, {
subtree: true,
attributes: true,
characterData: true,
childList: true,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment