Sort directories before files

Sort directories before files in repository tree view

// ==UserScript==
// @name        Sort directories before files
// @description Sort directories before files in repository tree view
// @author      anhkhoakz
// @version     1.0.1
// @match       *://git.sr.ht/*/tree(?:/*)?
// @namespace   anhkhoakz
// @icon        https://git.sr.ht/static/logo.png
// @license     GPL-3.0; https://www.gnu.org/licenses/gpl-3.0.html
// ==/UserScript==

(()=>{"use strict";const e=document.querySelector(".tree-list");if(!e)return;const t=[],a=[],n=e.querySelectorAll(".name");for(let e=0;e<n.length;e++){const s=n[e];s.classList.contains("tree")?t.push(e):s.classList.contains("blob")&&a.push(e)}const o=e.children.length,i=n.length,s=o/i;if(s*i!==o)return;const r=Array.from(e.children).sort((n,o)=>{const i=Array.prototype.indexOf.call(e.children,n),a=Array.prototype.indexOf.call(e.children,o),c=i/s|0,l=a/s|0,r=t.indexOf(c)!==-1,d=t.indexOf(l)!==-1;return r!==d?r?-1:1:i-a});for(const t of r)e.appendChild(t)})()