GitHub Sort Content

A userscript that makes some lists & markdown tables sortable

当前为 2016-08-20 提交的版本,查看 最新版本

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