GitHub Sort Content

A userscript that makes some lists & markdown tables sortable

目前为 2016-09-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Sort Content
  3. // @version 1.0.7
  4. // @description A userscript that makes some lists & markdown tables sortable
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace http://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. /* global GM_addStyle, tinysort */
  14. /* jshint esnext:true, unused:true */
  15. (() => {
  16. "use strict";
  17. /* example pages:
  18. tables - https://github.com/Mottie/GitHub-userscripts
  19. Contribute repos & Your Repos - https://github.com/
  20. organization repos - https://github.com/jquery
  21. organization members - https://github.com/orgs/jquery/people
  22. pinned & no pinned repos - https://github.com/addyosmani
  23. repos - https://github.com/addyosmani?tab=repositories
  24. stars - https://github.com/stars
  25. watching - https://github.com/watching
  26. */
  27. const sorts = ["asc", "desc"],
  28. icons = {
  29. white: {
  30. unsorted: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDggMSA4IDggMHpNMTUgOSAxIDkgOCAxNnoiIHN0eWxlPSJmaWxsOiNkZGQ7b3BhY2l0eTowLjIiLz48L3N2Zz4=",
  31. asc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDggMSA4IDggMHoiIHN0eWxlPSJmaWxsOiNkZGQiLz48cGF0aCBkPSJNMTUgOSAxIDkgOCAxNnoiIHN0eWxlPSJmaWxsOiNkZGQ7b3BhY2l0eTowLjIiLz48L3N2Zz4=",
  32. desc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDggMSA4IDggMHoiIHN0eWxlPSJmaWxsOiNkZGQ7b3BhY2l0eTowLjIiLz48cGF0aCBkPSJNMTUgOSAxIDkgOCAxNnoiIHN0eWxlPSJmaWxsOiNkZGQiLz48L3N2Zz4="
  33. },
  34. black: {
  35. unsorted: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDggMSA4IDggMHpNMTUgOSAxIDkgOCAxNnoiIHN0eWxlPSJmaWxsOiMyMjI7b3BhY2l0eTowLjIiLz48L3N2Zz4=",
  36. asc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDggMSA4IDggMHoiIHN0eWxlPSJmaWxsOiMyMjIiLz48cGF0aCBkPSJNMTUgOSAxIDkgOCAxNnoiIHN0eWxlPSJmaWxsOiMyMjI7b3BhY2l0eTowLjIiLz48L3N2Zz4=",
  37. desc: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+PHBhdGggZD0iTTE1IDggMSA4IDggMHoiIHN0eWxlPSJmaWxsOiMyMjI7b3BhY2l0eTowLjIiLz48cGF0aCBkPSJNMTUgOSAxIDkgOCAxNnoiIHN0eWxlPSJmaWxsOiMyMjIiLz48L3N2Zz4="
  38. }
  39. },
  40. // toolbars - target for sort arrows
  41. regexBars = /\b(filter-bar|org-toolbar|sort-bar|tabnav-tabs)\b/;
  42.  
  43. function initSortTable(el) {
  44. removeSelection();
  45. const dir = el.classList.contains(sorts[0]) ? sorts[1] : sorts[0],
  46. table = closest(el, "table");
  47. tinysort($$("tbody tr", table), {
  48. order: dir,
  49. natural: true,
  50. selector: `td:nth-child(${el.cellIndex + 1})`
  51. });
  52. $$("th", table).forEach(elm => {
  53. elm.classList.remove(...sorts);
  54. });
  55. el.classList.add(dir);
  56. }
  57.  
  58. function initSortUl(arrows, list, selector) {
  59. if (list && list.children) {
  60. removeSelection();
  61. const dir = arrows.classList.contains(sorts[0]) ? sorts[1] : sorts[0],
  62. options = {order: dir, natural: true};
  63. if (selector) {
  64. options.selector = selector;
  65. }
  66. // using children because the big repo contains UL > DIV
  67. tinysort(list.children, options);
  68. arrows.classList.remove(...sorts);
  69. arrows.classList.add(dir);
  70. }
  71. }
  72.  
  73. function needDarkTheme() {
  74. let brightest = 0,
  75. // color will be "rgb(#, #, #)" or "rgba(#, #, #, #)"
  76. color = window.getComputedStyle(document.body).backgroundColor;
  77. const rgb = (color || "").replace(/\s/g, "").match(/^rgba?\((\d+),(\d+),(\d+)/i);
  78. if (rgb) {
  79. color = rgb.slice(1); // remove "rgb.." part from match
  80. color.forEach(c => {
  81. // http://stackoverflow.com/a/15794784/145346
  82. brightest = Math.max(brightest, parseInt(c, 10));
  83. });
  84. // return true if we have a dark background
  85. return brightest < 128;
  86. }
  87. // fallback to bright background
  88. return false;
  89. }
  90.  
  91. function $(str, el) {
  92. return (el || document).querySelector(str);
  93. }
  94. function $$(str, el) {
  95. return Array.from((el || document).querySelectorAll(str));
  96. }
  97. function closest(el, selector) {
  98. while (el && el.nodeName !== "BODY" && !el.matches(selector)) {
  99. el = el.parentNode;
  100. }
  101. return el && el.matches(selector) ? el : null;
  102. }
  103. function removeSelection() {
  104. // remove text selection - http://stackoverflow.com/a/3171348/145346
  105. const sel = window.getSelection ? window.getSelection() : document.selection;
  106. if (sel) {
  107. if (sel.removeAllRanges) {
  108. sel.removeAllRanges();
  109. } else if (sel.empty) {
  110. sel.empty();
  111. }
  112. }
  113. }
  114.  
  115. function init() {
  116. const styles = needDarkTheme() ? icons.white : icons.black;
  117.  
  118. GM_addStyle(`
  119. /* unsorted icon */
  120. .markdown-body table thead th {
  121. cursor:pointer;
  122. padding-right:22px !important;
  123. background:url(${styles.unsorted}) no-repeat calc(100% - 5px) center !important;
  124. }
  125. div.js-pinned-repos-reorder-container > h3, .dashboard-sidebar .boxed-group > h3,
  126. div.filter-repos, div.js-repo-filter .filter-bar, .org-toolbar, .sort-bar,
  127. h2 + .tabnav > .tabnav-tabs, .subscriptions-content .boxed-group > h3 {
  128. cursor:pointer;
  129. padding-right:10px;
  130. background-image:url(${styles.unsorted}) !important;
  131. background-repeat:no-repeat !important;
  132. background-position:calc(100% - 5px) center !important;
  133. }
  134. /* https://github.com/ -> your repositories */
  135. .dashboard-sidebar .user-repos h3 { background-position: 175px 10px !important; }
  136. /* https://github.com/:user?tab=repositories */
  137. div.js-repo-filter .filter-bar { background-position:338px 10px !important; }
  138. /* https://github.com/:organization */
  139. .org-toolbar { background-position:calc(100% - 5px) 10px !important; }
  140. /* https://github.com/stars */
  141. .sort-bar { background-position:525px 10px !important; }
  142. /* https://github.com/watching */
  143. .subscriptions-content .boxed-group > h3 {
  144. background-position:150px 10px !important;
  145. }
  146. /* asc/dec icons */
  147. table thead th.asc, div.boxed-group h3.asc,
  148. div.js-repo-filter.asc, div.filter-bar.asc,
  149. .org-toolbar.asc, .sort-bar.asc, h2 + .tabnav > .tabnav-tabs.asc,
  150. .subscriptions-content .boxed-group > h3.asc {
  151. background-image:url(${styles.asc}) !important;
  152. background-repeat:no-repeat !important;
  153. }
  154. table thead th.desc, div.boxed-group h3.desc,
  155. div.js-repo-filter.desc, div.filter-bar.desc,
  156. .org-toolbar.desc, .sort-bar.desc, h2 + .tabnav > .tabnav-tabs.desc,
  157. .subscriptions-content .boxed-group > h3.desc {
  158. background-image:url(${styles.desc}) !important;
  159. background-repeat:no-repeat !important;
  160. }
  161. /* remove sort arrows */
  162. .popular-repos + div.boxed-group h3 {
  163. background-image:none !important;
  164. cursor:default;
  165. }
  166. /* Remove margin that overlaps sort arrow - https://github.com/:user?tab=repositories */
  167. .filter-bar li:last-child { margin-left: 0 !important; }
  168. /* move "Customize your pinned..." - https://github.com/:self */
  169. .pinned-repos-setting-link { margin-right:14px; }
  170. `);
  171.  
  172. document.body.addEventListener("click", event => {
  173. let el;
  174. const target = event.target,
  175. name = target.nodeName;
  176. if (target && target.nodeType === 1 && (
  177. // nodes th|h3 - form for stars page
  178. name === "H3" || name === "TH" || name === "FORM" ||
  179. // mini-repo & https://github.com/:user?tab=repositories (filter-bar)
  180. // https://github.com/:organization filter bar (org-toolbar)
  181. // https://github.com/stars (sort-bar)
  182. regexBars.test(target.className)
  183. )) {
  184. // don't sort tables not inside of markdown
  185. if (name === "TH" && closest(target, ".markdown-body")) {
  186. return initSortTable(target);
  187. }
  188.  
  189. // following
  190. el = $("ol.follow-list", closest(target, ".container"));
  191. if (el) {
  192. return initSortUl(target, el, ".follow-list-name a");
  193. }
  194.  
  195. // organization people - https://github.com/orgs/:organization/people
  196. el = $("ul.member-listing", target.parentNode);
  197. if (el) {
  198. return initSortUl(target, el, ".member-link");
  199. }
  200.  
  201. // big repo list - https://github.com/:user?tab=repositories
  202. // stars - https://github.com/stars
  203. el = closest(target, ".sort-bar, .filter-bar, .org-toolbar");
  204. if (el && $(".repo-list", el.parentNode)) {
  205. return initSortUl(el, $(".repo-list", el.parentNode), ".repo-list-name a");
  206. }
  207.  
  208. // https://github.com/watching
  209. el = closest(target, ".subscriptions-content");
  210. if (el && $(".repo-list", el)) {
  211. return initSortUl(target, $(".repo-list", el), "li a");
  212. }
  213.  
  214. // mini-repo listings with & without filter - https://github.com/
  215. // and pinned repo lists
  216. el = closest(target, ".boxed-group");
  217. // prevent clicking on the H3 header of filtered repos
  218. if (el && name === "H3" && (
  219. el.classList.contains("js-repo-filter") ||
  220. el.classList.contains("js-pinned-repos-reorder-container")
  221. )) {
  222. return initSortUl(target, $(".mini-repo-list", el));
  223. }
  224. }
  225. });
  226. }
  227.  
  228. init();
  229. })();