Greasy Fork 还支持 简体中文。

GitHub Sort Content

A userscript that makes some lists & markdown tables sortable

目前為 2016-07-29 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub Sort Content
  3. // @version 1.0.3
  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.repo-tab .filter-bar, .org-toolbar, .sort-bar, h2 + .tabnav > .tabnav-tabs {
  126. cursor:pointer;
  127. padding-right:10px;
  128. background-image:url(${styles.unsorted}) !important;
  129. background-repeat:no-repeat !important;
  130. background-position:calc(100% - 5px) center !important;
  131. }
  132. /* https://github.com/ */
  133. div.filter-repos { background-position:calc(100% - 5px) 80% !important; }
  134. /* https://github.com/:user?tab=repositories */
  135. div.repo-tab .filter-bar { background-position:338px 10px !important; }
  136. /* https://github.com/:organization */
  137. .org-toolbar { background-position:calc(100% - 5px) 10px !important; }
  138. /* https://github.com/stars */
  139. .sort-bar { background-position:525px 10px !important; }
  140. /* asc/dec icons */
  141. table thead th.asc, div.boxed-group h3.asc,
  142. div.filter-repos.asc, div.filter-bar.asc,
  143. .org-toolbar.asc, .sort-bar.asc, h2 + .tabnav > .tabnav-tabs.asc {
  144. background-image:url(${styles.asc}) !important;
  145. }
  146. table thead th.desc, div.boxed-group h3.desc,
  147. div.filter-repos.desc, div.filter-bar.desc,
  148. .org-toolbar.desc, .sort-bar.desc, h2 + .tabnav > .tabnav-tabs.desc {
  149. background-image:url(${styles.desc}) !important;
  150. }
  151. /* remove sort arrows */
  152. .popular-repos + div.boxed-group h3 {
  153. background-image:none !important;
  154. cursor:default;
  155. }
  156. /* Remove margin that overlaps sort arrow - https://github.com/:user?tab=repositories */
  157. .filter-bar li:last-child { margin-left: 0 !important; }
  158. /* move "Customize your pinned..." - https://github.com/:self */
  159. .pinned-repos-setting-link { margin-right:14px; }
  160. `);
  161.  
  162. document.body.addEventListener("click", event => {
  163. let el,
  164. target = event.target,
  165. name = target.nodeName;
  166. if (target && target.nodeType === 1 && (
  167. // nodes th|h3 - form for stars page
  168. name === "H3" || name === "TH" || name === "FORM" ||
  169. // mini-repo & https://github.com/:user?tab=repositories (filter-bar)
  170. // https://github.com/:organization filter bar (org-toolbar)
  171. // https://github.com/stars (sort-bar)
  172. regexBars.test(target.className)
  173. )) {
  174. // don't sort tables not inside of markdown
  175. if (name === "TH" && closest(target, ".markdown-body")) {
  176. return initSortTable(target);
  177. }
  178.  
  179. // following
  180. el = $("ol.follow-list", closest(target, ".container"));
  181. if (el) {
  182. return initSortUl(target, el, ".follow-list-name a");
  183. }
  184.  
  185. // organization people - https://github.com/orgs/:organization/people
  186. el = $("ul.member-listing", target.parentNode);
  187. if (el) {
  188. return initSortUl(target, el, ".member-link");
  189. }
  190.  
  191. // big repo list - https://github.com/:user?tab=repositories
  192. // stars - https://github.com/stars
  193. el = closest(target, ".sort-bar, .filter-bar, .org-toolbar");
  194. if (el && $(".repo-list", el.parentNode)) {
  195. return initSortUl(el, $(".repo-list", el.parentNode), ".repo-list-name a");
  196. }
  197.  
  198. // mini-repo listings with & without filter - https://github.com/
  199. el = closest(target, ".boxed-group");
  200. // prevent clicking on the H3 header of filtered repos
  201. if (el && !(name === "H3" && el.classList.contains("js-repo-filter"))) {
  202. return initSortUl(target, $(".mini-repo-list", el));
  203. }
  204. }
  205. });
  206. }
  207.  
  208. init();
  209.  
  210. })();