GitHub Sort Content

A userscript that makes some lists & markdown tables sortable

当前为 2017-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Sort Content
  3. // @version 1.1.5
  4. // @description A userscript that makes some lists & markdown tables sortable
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace https://github.com/Mottie
  7. // @include https://github.com/*
  8. // @grant GM_addStyle
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.3.6/tinysort.min.js
  10. // @run-at document-idle
  11. // @author Rob Garrison
  12. // ==/UserScript==
  13. (() => {
  14. "use strict";
  15. /* example pages:
  16. tables - https://github.com/Mottie/GitHub-userscripts
  17. Contribute repos & Your Repos - https://github.com/
  18. organization repos - https://github.com/jquery
  19. organization members - https://github.com/orgs/jquery/people
  20. pinned & no pinned repos - https://github.com/addyosmani
  21. repos - https://github.com/addyosmani?tab=repositories
  22. stars - https://github.com/stars
  23. watching - https://github.com/watching
  24. */
  25. const sorts = ["asc", "desc"],
  26. icons = {
  27. white: {
  28. unsorted: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDhIMWw3LTh6bTAgMUgxbDcgN3oiIGZpbGw9IiNkZGQiIG9wYWNpdHk9Ii4yIi8+PC9zdmc+",
  29. asc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDhIMWw3LTh6IiBmaWxsPSIjZGRkIi8+PHBhdGggZD0iTTE1IDlIMWw3IDd6IiBmaWxsPSIjZGRkIiBvcGFjaXR5PSIuMiIvPjwvc3ZnPg==",
  30. desc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDhIMWw3LTh6IiBmaWxsPSIjZGRkIiBvcGFjaXR5PSIuMiIvPjxwYXRoIGQ9Ik0xNSA5SDFsNyA3eiIgZmlsbD0iI2RkZCIvPjwvc3ZnPg=="
  31. },
  32. black: {
  33. unsorted: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDhIMWw3LTh6bTAgMUgxbDcgN3oiIGZpbGw9IiMyMjIiIG9wYWNpdHk9Ii4yIi8+PC9zdmc+",
  34. asc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDhIMWw3LTh6IiBmaWxsPSIjMjIyIi8+PHBhdGggZD0iTTE1IDlIMWw3IDd6IiBmaWxsPSIjMjIyIiBvcGFjaXR5PSIuMiIvPjwvc3ZnPg==",
  35. desc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDhIMWw3LTh6IiBmaWxsPSIjMjIyIiBvcGFjaXR5PSIuMiIvPjxwYXRoIGQ9Ik0xNSA5SDFsNyA3eiIgZmlsbD0iIzIyMiIvPjwvc3ZnPg=="
  36. }
  37. },
  38. // toolbars - target for sort arrows
  39. regexBars = new RegExp(
  40. "\\b(" +
  41. [
  42. "TableObject", // org repos
  43. "org-toolbar", // org people
  44. "sort-bar", // https://github.com/stars
  45. "tabnav-tabs", // https://github.com/:user/follower(s|ing)
  46. "Box-header|flex-auto", // watching
  47. "user-profile-nav" // user repos
  48. ].join("|") +
  49. ")\\b"
  50. );
  51.  
  52. function initSortTable(el) {
  53. removeSelection();
  54. const dir = el.classList.contains(sorts[0]) ? sorts[1] : sorts[0],
  55. table = closest("table", el);
  56. tinysort($$("tbody tr", table), {
  57. order: dir,
  58. natural: true,
  59. selector: `td:nth-child(${el.cellIndex + 1})`
  60. });
  61. $$("th", table).forEach(elm => {
  62. elm.classList.remove(...sorts);
  63. });
  64. el.classList.add(dir);
  65. }
  66.  
  67. function initSortUl(arrows, list, selector) {
  68. if (list && list.children) {
  69. removeSelection();
  70. const dir = arrows.classList.contains(sorts[0]) ? sorts[1] : sorts[0],
  71. options = {
  72. order: dir,
  73. natural: true
  74. };
  75. if (selector) {
  76. options.selector = selector;
  77. }
  78. // using children because the big repo contains UL > DIV
  79. tinysort(list.children, options);
  80. arrows.classList.remove(...sorts);
  81. arrows.classList.add(dir);
  82. }
  83. }
  84.  
  85. function needDarkTheme() {
  86. let brightest = 0,
  87. // color will be "rgb(#, #, #)" or "rgba(#, #, #, #)"
  88. color = window.getComputedStyle(document.body).backgroundColor;
  89. const rgb = (color || "")
  90. .replace(/\s/g, "")
  91. .match(/^rgba?\((\d+),(\d+),(\d+)/i);
  92. if (rgb) {
  93. color = rgb.slice(1); // remove "rgb.." part from match
  94. color.forEach(c => {
  95. // http://stackoverflow.com/a/15794784/145346
  96. brightest = Math.max(brightest, parseInt(c, 10));
  97. });
  98. // return true if we have a dark background
  99. return brightest < 128;
  100. }
  101. // fallback to bright background
  102. return false;
  103. }
  104.  
  105. function $(str, el) {
  106. return (el || document).querySelector(str);
  107. }
  108.  
  109. function $$(str, el) {
  110. return Array.from((el || document).querySelectorAll(str));
  111. }
  112.  
  113. function closest(selector, el) {
  114. while (el && el.nodeType === 1) {
  115. if (el.matches(selector)) {
  116. return el;
  117. }
  118. el = el.parentNode;
  119. }
  120. return null;
  121. }
  122.  
  123. function removeSelection() {
  124. // remove text selection - http://stackoverflow.com/a/3171348/145346
  125. const sel = window.getSelection ?
  126. window.getSelection() :
  127. document.selection;
  128. if (sel) {
  129. if (sel.removeAllRanges) {
  130. sel.removeAllRanges();
  131. } else if (sel.empty) {
  132. sel.empty();
  133. }
  134. }
  135. }
  136.  
  137. function init() {
  138. const styles = needDarkTheme() ? icons.white : icons.black;
  139.  
  140. GM_addStyle(`
  141. /* unsorted icon */
  142. .markdown-body table thead th {
  143. cursor:pointer;
  144. padding-right:22px !important;
  145. background-image:url(${styles.unsorted}) !important;
  146. background-repeat:no-repeat !important;
  147. background-position:calc(100% - 5px) center !important;
  148. }
  149. div.js-pinned-repos-reorder-container > h3,
  150. .dashboard-sidebar .boxed-group > h3,
  151. .sort-bar, h2 + .tabnav > .tabnav-tabs, .org-toolbar,
  152. .org-profile .TableObject-item--primary,
  153. .subscriptions-content .Box-header, div.user-profile-nav.js-sticky {
  154. cursor:pointer;
  155. padding-right:10px;
  156. background-image:url(${styles.unsorted}) !important;
  157. background-repeat:no-repeat !important;
  158. background-position:calc(100% - 5px) center !important;
  159. }
  160. /* https://github.com/ -> your repositories */
  161. .dashboard-sidebar .user-repos h3 {
  162. background-position: 175px 10px !important;
  163. }
  164. /* https://github.com/:user?tab=repositories */
  165. div.user-profile-nav.js-sticky {
  166. background-position:calc(100% - 80px) 22px !important;
  167. }
  168. /* https://github.com/:organization repos & people */
  169. .org-profile .TableObject-item--primary, .org-toolbar {
  170. background-position:calc(100% - 5px) 10px !important;
  171. }
  172. .TableObject-item--primary input {
  173. width: 97.5% !important;
  174. }
  175. /* https://github.com/stars */
  176. .sort-bar {
  177. background-position:525px 10px !important;
  178. }
  179. /* https://github.com/watching */
  180. .subscriptions-content .Box-header {
  181. background-position:160px 15px !important;
  182. }
  183. /* asc/dec icons */
  184. table thead th.asc, div.boxed-group h3.asc, div.user-profile-nav.asc,
  185. div.js-repo-filter.asc, .org-toolbar.asc,
  186. .TableObject-item--primary.asc, .sort-bar.asc,
  187. h2 + .tabnav > .tabnav-tabs.asc,
  188. .subscriptions-content .Box-header.asc {
  189. background-image:url(${styles.asc}) !important;
  190. background-repeat:no-repeat !important;
  191. }
  192. table thead th.desc, div.boxed-group h3.desc, div.user-profile-nav.desc,
  193. div.js-repo-filter.desc, .org-toolbar.desc,
  194. .TableObject-item--primary.desc, .sort-bar.desc,
  195. h2 + .tabnav > .tabnav-tabs.desc,
  196. .subscriptions-content .Box-header.desc {
  197. background-image:url(${styles.desc}) !important;
  198. background-repeat:no-repeat !important;
  199. }
  200. /* remove sort arrows */
  201. .popular-repos + div.boxed-group h3 {
  202. background-image:none !important;
  203. cursor:default;
  204. }
  205. /* move "Customize your pinned..." - https://github.com/:self */
  206. .pinned-repos-setting-link {
  207. margin-right:14px;
  208. }
  209. `);
  210.  
  211. document.body.addEventListener("click", event => {
  212. let el;
  213. const target = event.target,
  214. name = target.nodeName;
  215. if (target && target.nodeType === 1 && (
  216. // nodes th|h3 & form for stars page
  217. name === "H3" || name === "TH" || name === "FORM" ||
  218. // https://github.com/:organization filter bar
  219. // filter: .TableObject-item--primary, repo wrapper: .org-profile
  220. // https://github.com/stars (sort-bar)
  221. // https://github.com/:user/followers (tabnav-tabs)
  222. // https://github.com/:user/following (tabnav-tabs)
  223. // https://github.com/:user?tab=repositories (user-profile-nav)
  224. // https://github.com/:user?tab=stars (user-profile-nav)
  225. // https://github.com/:user?tab=followers (user-profile-nav)
  226. // https://github.com/:user?tab=followering (user-profile-nav)
  227. regexBars.test(target.className)
  228. )) {
  229. // don't sort tables not inside of markdown
  230. if (name === "TH" && closest(".markdown-body", target)) {
  231. return initSortTable(target);
  232. }
  233.  
  234. // following
  235. el = $("ol.follow-list", closest(".container", target));
  236. if (el) {
  237. return initSortUl(target, el, ".follow-list-name a");
  238. }
  239.  
  240. // organization people - https://github.com/orgs/:organization/people
  241. el = $("ul.member-listing-next", target.parentNode);
  242. if (el) {
  243. return initSortUl(target, el, ".member-info a");
  244. }
  245.  
  246. // stars - https://github.com/stars
  247. el = closest(".sort-bar", target);
  248. if (el && $(".repo-list", el.parentNode)) {
  249. return initSortUl(el, $(".repo-list", el.parentNode), "h3 a");
  250. }
  251.  
  252. // org repos - https://github.com/:organization
  253. el = closest(".org-profile", target);
  254. if (el && $(".repo-list", el)) {
  255. return initSortUl(target, $(".repo-list", el), "h3 a");
  256. }
  257.  
  258. // https://github.com/watching
  259. el = closest(".subscriptions-content", target);
  260. if (el && $(".repo-list", el)) {
  261. return initSortUl($(".Box-header", el), $(".repo-list", el), "li a");
  262. }
  263.  
  264. // mini-repo listings with & without filter - https://github.com/
  265. // and pinned repo lists
  266. el = closest(".boxed-group", target);
  267. // prevent clicking on the H3 header of filtered repos
  268. if (el && name === "H3" && (
  269. el.classList.contains("js-repo-filter") ||
  270. el.classList.contains("js-pinned-repos-reorder-container")
  271. )) {
  272. return initSortUl(target, $(".mini-repo-list", el));
  273. }
  274.  
  275. // user sticky navigation
  276. if (target.classList.contains("user-profile-nav")) {
  277. el = $(".underline-nav-item.selected", target);
  278. if (el) {
  279. if (el.textContent.indexOf("Overview") > -1) {
  280. return initSortUl(target, $(".pinned-repos-list"), ".repo");
  281. } else if (el.href.indexOf("tab=repo") > -1) {
  282. return initSortUl(target, $(".js-repo-list"), "h3 a");
  283. } else if (el.href.indexOf("tab=stars") > -1) {
  284. return initSortUl(target, $(".js-repo-filter"), "h3 a");
  285. } else if (el.href.indexOf("tab=follow") > -1) {
  286. return initSortUl(target, $(".js-repo-filter"), "a .f4");
  287. }
  288. }
  289. }
  290. }
  291. });
  292. }
  293.  
  294. init();
  295. })();