// ==UserScript== // @name GitHub Sort Content // @version 2.0.1 // @description A userscript that makes some lists & markdown tables sortable // @license MIT // @author Rob Garrison // @namespace https://github.com/Mottie // @include https://github.com/* // @run-at document-idle // @grant GM.addStyle // @grant GM_addStyle // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103 // @require https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.3.6/tinysort.min.js // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=634242 // @icon https://assets-cdn.github.com/pinned-octocat.svg // @downloadURL none // ==/UserScript== (() => { "use strict"; /** Example pages: * Tables (Readme & wikis) - https://github.com/Mottie/GitHub-userscripts * Repo files - https://github.com/Mottie/GitHub-userscripts (sort content, message or age) * Your Repos & Your Teams - https://github.com/ * Pinned repos (org & user)- https://github.com/:org * Organization repos - https://github.com/:org * Organization people - https://github.com/orgs/:org/people * Organization outside collaborators (own orgs) - https://github.com/orgs/:org/outside-collaborators * Organization teams - https://github.com/orgs/:org/teams * Repo stargazers - https://github.com/:user/:repo/stargazers * Repo watchers - https://github.com/:user/:repo/watchers * User repos - https://github.com/:user?tab=repositories * User stars - https://github.com/:user?tab=stars * User Followers - https://github.com/:user?tab=followers & https://github.com/:user/followers(/you_know) * User Following - https://github.com/:user?tab=following & https://github.com/:user/following(/you_know) * watching - https://github.com/watching */ /** * sortables[entry].setup - exec on userscript init (optional) * sortables[entry].check - exec on doc.body click; return truthy/falsy or * header element (passed to the sort) * sortables[entry].sort - exec if check returns true or a header element; * el param is the element returned by check or original click target */ const sortables = { // markdown tables "tables": { // init after a short delay to allow rendering of file list setup: () => setTimeout(() => addRepoFileThead(), 200), check: el => el.nodeName === "TH" && el.matches(".markdown-body table thead th, table.files thead th"), sort: el => initSortTable(el) }, // https://github.com (repo list & teams list) "feed": { check: el => el.classList.contains("Box-title") && el.closest(".Box.js-repos-container"), sort: el => initSortUl(el, $$(".Box-body li", el.closest(".Box"))) }, // https://github.com/orgs/:org/dashboard (repo list) "org-feed": { check: el => el.classList.contains("Box-title") && el.closest("#org_your_repos.js-repos-container"), sort: el => initSortUl(el, $$(".boxed-group-inner li", el)) }, // https://github.com/(:user|:org) (pinned repos) "pinned": { check: el => $(".js-pinned-repos-reorder-container") && el.matches(".org-profile.js-pinned-repos-reorder-container h2, .user-profile-nav"), sort: el => initSortUl(el, $(".pinned-repos-list").children) }, // https://github.com/:org "org-repos": { check: el => { // Org repos have weirdly nested forms if there are pinned repos let wrap = false; if ($(".org-repos.repo-list") && el.matches(".TableObject, .TableObject-item")) { wrap = el.closest("form[data-pjax='#org-repositories']"); if (wrap) { wrap = wrap.parentNode; } else { wrap = el; } return wrap && wrap.classList.contains("TableObject") ? wrap : false; } return wrap; }, sort: el => { const list = $(".org-repos.repo-list"); initSortUl(el, list.children); movePaginate(list); } }, // https://github.com/orgs/:org/people "org-people": { setup: () => checkOwnOrg(), check: (el, loc) => loc.href.indexOf("/people") > -1 && $("#org-members-table") && el.matches(".org-toolbar.ghsc-org-people"), sort: el => initSortUl(el, $$("#org-members-table li"), ".member-info a") }, // https://github.com/orgs/:org/outside-collaborators (own org) "org-collab-own": { check: (el, loc) => loc.href.indexOf("/outside-collaborators") > -1 && $("#org-outside-collaborators") && el.matches(".org-toolbar.ghsc-org-outside_collaborators"), sort: el => initSortUl(el, $$("#org-outside-collaborators li"), ".member-info a") }, // https://github.com/orgs/:org/teams "org-teams": { check: el => $("#org-teams") && el.matches(".ghsc-org-teams.subnav.org-toolbar"), sort: el => initSortUl(el, $$("#org-teams li"), ".team-name") }, // https://github.com/:user?tab=repositories "user-repos": { check: (el, loc) => loc.search.indexOf("tab=repositories") > -1 && el.classList.contains("user-profile-nav"), sort: el => initSortUl(el, $$("#user-repositories-list li")) }, // https://github.com/:user?tab=stars "user-stars": { check: (el, loc) => loc.search.indexOf("tab=stars") > -1 && el.classList.contains("user-profile-nav"), sort: el => { const list = $(".TableObject").parentNode; initSortUl(el, $$(".col-12", list), "h3 a"); movePaginate(list); } }, // https://github.com/:user?tab=follow(ers|ing) "user-tab-follow": { check: (el, loc) => loc.search.indexOf("tab=follow") > -1 && el.classList.contains("user-profile-nav"), sort: el => { const list = $(".table-fixed").parentNode; initSortUl(el, $$(".col-12", list), ".col-9 a.no-underline"); movePaginate(list); } }, // https://github.com/:user/follow(ers|ing) // https://github.com/:user/follow(ers|ing)/you_know "user-follow": { setup: () => { if (window.location.href.indexOf("/follow") > -1) { const repo = $(".userrepos, .follow-list"); const wrap = repo && repo.closest(".container"); if (wrap) { $("h2", wrap).classList.add("ghsc-header"); repo.classList.add("ghsc-active"); } } }, check: el => $(".userrepos.ghsc-active, .follow-list.ghsc-active") && el.matches("h2.ghsc-header"), sort: el => initSortUl(el, $$(".userrepos li, .follow-list li"), ".follow-list-name") }, // https://github.com/watching "user-watch": { check: (el, loc) => loc.href.indexOf("/watching") > -1 && el.matches(".subscriptions-content .Box-header h3, .subscriptions-content .Box-header .text-right"), sort: el => initSortUl(el.closest(".Box-header"), $$(".standalone.repo-list li")) }, // https://github.com/:user/repo/(stargazers|watchers) "repo-stars-or-watchers": { check: (el, loc) => (loc.href.indexOf("/stargazers") > -1 || loc.href.indexOf("/watchers") > -1) && $(".follow-list") && el.matches("#repos > h2"), sort: el => initSortUl(el, $$(".follow-list-item"), ".follow-list-name") } }; const sorts = ["asc", "desc"]; const icons = { unsorted: color => ``, asc: color => ``, desc: color => `` }; function getIcon(type, color) { return "data:image/svg+xml;charset=UTF-8," + encodeURIComponent(icons[type](color)); } function needDarkTheme() { // color will be "rgb(#, #, #)" or "rgba(#, #, #, #)" let color = window.getComputedStyle(document.body).backgroundColor; const rgb = (color || "") .replace(/\s/g, "") .match(/^rgba?\((\d+),(\d+),(\d+)/i); if (rgb) { // remove "rgb.." part from match & parse const colors = rgb.slice(1).map(Number); // http://stackoverflow.com/a/15794784/145346 const brightest = Math.max.apply(null, colors); // return true if we have a dark background return brightest < 128; } // fallback to bright background return false; } function addRepoFileThead() { const $table = $("table.files"); if ($table && !$(".ghsc-header", $table)) { const thead = document.createElement("thead"); thead.innerHTML = `