GitHub Branches Sorter

Allows sorting the branches list of a repository in ways additional to the default, modification time ordering

  1. // ==UserScript==
  2. // @name GitHub Branches Sorter
  3. // @namespace Violentmonkey Scripts
  4. // @match https://github.com/*/*
  5. // @grant none
  6. // @version 1.0.1
  7. // @license ISC
  8. // @author OctoSpacc
  9. // @description Allows sorting the branches list of a repository in ways additional to the default, modification time ordering
  10. // ==/UserScript==
  11.  
  12. var lastpath = window.location.pathname;
  13.  
  14. function onBranchesPage(){
  15. var pathElems = window.location.pathname.split('/').slice(3);
  16. return (pathElems[0] === 'branches' && pathElems[1]);
  17. }
  18.  
  19. function main(){
  20. lastpath = window.location.pathname;
  21. var tableQuery = 'table.Table__StyledTable-sc-jofqvq-0.gsRldM.Table';
  22.  
  23. var orderButtonElem = document.createElement('button');
  24. orderButtonElem.innerHTML = 'Sort Branches';
  25. orderButtonElem.onclick = function(){
  26. var tableElems = document.querySelectorAll(`${tableQuery} > tbody`);
  27. tableElems[0].style.display = (tableElems[0].style.display ? '' : 'none');
  28. tableElems[1].style.display = (tableElems[1].style.display ? '' : 'none');
  29. };
  30.  
  31. var tableAlphabElem = document.querySelector(`${tableQuery} > tbody`).cloneNode(false);
  32. tableAlphabElem.style.display = 'none';
  33.  
  34. var alphabRowElems = {};
  35. for (var branchRowElem of document.querySelectorAll(tableQuery + '> tbody > tr')) {
  36. var branchName = branchRowElem.querySelector('td > div > a').textContent;
  37. var branchRowElemNew = branchRowElem.cloneNode(true);
  38. alphabRowElems[branchName] = branchRowElemNew;
  39. }
  40.  
  41. alphabRowElems = Object.keys(alphabRowElems).sort().reduce(
  42. function(obj, key) {
  43. obj[key] = alphabRowElems[key];
  44. return obj;
  45. },
  46. {});
  47.  
  48. for (var branchRowElem of Object.values(alphabRowElems)) {
  49. tableAlphabElem.appendChild(branchRowElem);
  50. }
  51.  
  52. document.querySelector('.Box-sc-g0xbh4-0.lhFvfi').appendChild(orderButtonElem);
  53. document.querySelector(tableQuery).appendChild(tableAlphabElem);
  54. }
  55.  
  56. setInterval(function(){
  57. if (window.location.pathname !== lastpath && onBranchesPage()) {
  58. window.location.reload();
  59. }
  60. }, 1000);
  61.  
  62. if (onBranchesPage()) {
  63. main();
  64. }