GitHub Table of Contents

A userscript that adds a table of contents to readme & wiki pages

当前为 2020-07-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Table of Contents
  3. // @version 2.0.3
  4. // @description A userscript that adds a table of contents to readme & wiki pages
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @include https://gist.github.com/*
  10. // @run-at document-idle
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_getValue
  13. // @grant GM.getValue
  14. // @grant GM_setValue
  15. // @grant GM.setValue
  16. // @grant GM_addStyle
  17. // @grant GM.addStyle
  18. // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=666427
  19. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103
  20. // @icon https://github.githubassets.com/pinned-octocat.svg
  21. // ==/UserScript==
  22. (async () => {
  23. "use strict";
  24.  
  25. const defaults = {
  26. title: "Table of Contents", // popup title
  27. top: "64px", // popup top position when reset
  28. left: "auto", // popup left position when reset
  29. right: "10px", // popup right position when reset
  30. headerPad: "48px", // padding added to header when TOC is collapsed
  31. headerSelector: [".header", ".Header"],
  32. headerWrap: ".js-header-wrapper",
  33. toggle: "g+t", // keyboard toggle shortcut
  34. restore: "g+r", // keyboard reset popup position shortcut
  35. delay: 1000, // ms between keyboard shortcuts
  36. };
  37.  
  38. GM.addStyle(`
  39. /* z-index > 1000 to be above the */
  40. .ghus-toc { position:fixed; z-index:1001; min-width:200px; top:${defaults.top};
  41. right:${defaults.right}; }
  42. .ghus-toc h3 { cursor:move; }
  43. .ghus-toc-title { padding-left:20px; }
  44. /* icon toggles TOC container & subgroups */
  45. .ghus-toc .ghus-toc-icon { vertical-align:baseline; }
  46. .ghus-toc h3 .ghus-toc-icon, .ghus-toc li.collapsible .ghus-toc-icon { cursor:pointer; }
  47. .ghus-toc .ghus-toc-toggle { position:absolute; width:28px; height:38px; top:0px; left:0px; }
  48. .ghus-toc .ghus-toc-toggle svg { margin-top:10px; margin-left:9px; }
  49. .ghus-toc .ghus-toc-docs { float:right; }
  50. /* move collapsed TOC to top right corner */
  51. .ghus-toc.collapsed {
  52. width:30px; height:30px; min-width:auto; overflow:hidden; top:16px !important; left:auto !important;
  53. right:10px !important; border:1px solid rgba(128, 128, 128, 0.5); border-radius:3px;
  54. }
  55. .ghus-toc.collapsed > h3 { cursor:pointer; padding-top:5px; border:none; background:#222; color:#ddd; }
  56. .ghus-toc.collapsed .ghus-toc-docs { display:none; }
  57. .ghus-toc:not(.ghus-toc-hidden).collapsed + .Header { padding-right: ${defaults.headerPad} !important; }
  58. /* move header text out-of-view when collapsed */
  59. .ghus-toc.collapsed > h3 svg { margin-top:6px; }
  60. .ghus-toc-hidden, .ghus-toc.collapsed .boxed-group-inner,
  61. .ghus-toc li:not(.collapsible) .ghus-toc-icon { display:none; }
  62. .ghus-toc .boxed-group-inner { max-width:250px; max-height:400px; overflow-y:auto; overflow-x:hidden; }
  63. .ghus-toc ul { list-style:none; }
  64. .ghus-toc li { max-width:230px; white-space:nowrap; overflow-x:hidden; text-overflow:ellipsis; }
  65. .ghus-toc .ghus-toc-h1 { padding-left:15px; }
  66. .ghus-toc .ghus-toc-h2 { padding-left:30px; }
  67. .ghus-toc .ghus-toc-h3 { padding-left:45px; }
  68. .ghus-toc .ghus-toc-h4 { padding-left:60px; }
  69. .ghus-toc .ghus-toc-h5 { padding-left:75px; }
  70. .ghus-toc .ghus-toc-h6 { padding-left:90px; }
  71. /* anchor collapsible icon */
  72. .ghus-toc li.collapsible .ghus-toc-icon {
  73. width:16px; height:10px; display:inline-block; margin-left:-16px;
  74. background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGNsYXNzPSdvY3RpY29uJyBoZWlnaHQ9JzE0JyB2aWV3Qm94PScwIDAgMTIgMTYnPjxwYXRoIGQ9J00wIDVsNiA2IDYtNkgweic+PC9wYXRoPjwvc3ZnPg==) left center no-repeat;
  75. }
  76. /* on rotate, height becomes width, so this is keeping things lined up */
  77. .ghus-toc li.collapsible.collapsed .ghus-toc-icon { -webkit-transform:rotate(-90deg); transform:rotate(-90deg); height:10px; width:12px; margin-right:2px; }
  78. .ghus-toc-icon svg, .ghus-toc-docs svg { pointer-events:none; }
  79. .ghus-toc-no-selection { -webkit-user-select:none !important; -moz-user-select:none !important; user-select:none !important; }
  80. /* prevent google translate from breaking links */
  81. .ghus-toc li a font { pointer-events:none; }
  82. `);
  83.  
  84. let tocInit = false;
  85.  
  86. // modifiable title
  87. let title = await GM.getValue("github-toc-title", defaults.title);
  88.  
  89. const container = document.createElement("div");
  90. const useClient = !!document.all;
  91.  
  92. // keyboard shortcuts
  93. const keyboard = {
  94. timer: null,
  95. lastKey: null
  96. };
  97.  
  98. // drag variables
  99. const drag = {
  100. el: null,
  101. elmX: 0,
  102. elmY: 0,
  103. time: 0,
  104. unsel: null
  105. };
  106.  
  107. const stopPropag = event => {
  108. event.preventDefault();
  109. event.stopPropagation();
  110. };
  111.  
  112. // drag code adapted from http://jsfiddle.net/tovic/Xcb8d/light/
  113. function dragInit(event) {
  114. if (!container.classList.contains("collapsed")) {
  115. const x = useClient ? window.event.clientX : event.pageX;
  116. const y = useClient ? window.event.clientY : event.pageY;
  117. drag.el = container;
  118. drag.elmX = x - drag.el.offsetLeft;
  119. drag.elmY = y - drag.el.offsetTop;
  120. selectionToggle(true);
  121. } else {
  122. drag.el = null;
  123. }
  124. drag.time = new Date().getTime() + 500;
  125. }
  126.  
  127. function dragMove(event) {
  128. if (drag.el !== null) {
  129. const x = useClient ? window.event.clientX : event.pageX;
  130. const y = useClient ? window.event.clientY : event.pageY;
  131. drag.el.style.left = (x - drag.elmX) + "px";
  132. drag.el.style.top = (y - drag.elmY) + "px";
  133. drag.el.style.right = "auto";
  134. }
  135. }
  136.  
  137. function dragStop() {
  138. if (drag.el !== null) {
  139. dragSave();
  140. selectionToggle();
  141. }
  142. drag.el = null;
  143. }
  144.  
  145. async function dragSave(restore) {
  146. let adjLeft = null;
  147. let top = null;
  148. let val = null;
  149. if (restore) {
  150. // position restore (reset) popup to default position
  151. setPosition(defaults.left, defaults.top, defaults.right);
  152. } else {
  153. // Adjust saved left position to be measured from the center of the window
  154. // See issue #102
  155. const winHalf = window.innerWidth / 2;
  156. const left = winHalf - parseInt(container.style.left, 10);
  157. adjLeft = left * (left > winHalf ? 1 : -1);
  158. top = parseInt(container.style.top, 10);
  159. val = [adjLeft, top];
  160. }
  161. drag.elmX = adjLeft;
  162. drag.elmY = top;
  163. await GM.setValue("github-toc-location", val);
  164. }
  165.  
  166. function resize(_, left = drag.elmX, top = drag.elmY) {
  167. if (left !== null) {
  168. drag.elmX = left;
  169. drag.elmY = top;
  170. setPosition(((window.innerWidth / 2) + left) + "px", top + "px");
  171. }
  172. }
  173.  
  174. function setPosition(left, top, right = "auto") {
  175. container.style.left = left;
  176. container.style.right = right;
  177. container.style.top = top;
  178. }
  179.  
  180. // stop text selection while dragging
  181. function selectionToggle(disable) {
  182. const body = $("body");
  183. if (disable) {
  184. // save current "unselectable" value
  185. drag.unsel = body.getAttribute("unselectable");
  186. body.setAttribute("unselectable", "on");
  187. body.classList.add("ghus-toc-no-selection");
  188. on(body, "onselectstart", stopPropag);
  189. } else {
  190. if (drag.unsel) {
  191. body.setAttribute("unselectable", drag.unsel);
  192. }
  193. body.classList.remove("ghus-toc-no-selection");
  194. body.removeEventListener("onselectstart", stopPropag);
  195. }
  196. removeSelection();
  197. }
  198.  
  199. function removeSelection() {
  200. // remove text selection - http://stackoverflow.com/a/3171348/145346
  201. const sel = window.getSelection ? window.getSelection() : document.selection;
  202. if (sel) {
  203. if (sel.removeAllRanges) {
  204. sel.removeAllRanges();
  205. } else if (sel.empty) {
  206. sel.empty();
  207. }
  208. }
  209. }
  210.  
  211. async function tocShow() {
  212. container.classList.remove("collapsed");
  213. await GM.setValue("github-toc-hidden", false);
  214. }
  215.  
  216. async function tocHide() {
  217. container.classList.add("collapsed");
  218. await GM.setValue("github-toc-hidden", true);
  219. }
  220.  
  221. function tocToggle() {
  222. // don't toggle content on long clicks
  223. if (drag.time > new Date().getTime()) {
  224. if (container.classList.contains("collapsed")) {
  225. tocShow();
  226. } else {
  227. tocHide();
  228. }
  229. }
  230. }
  231. // hide TOC entirely, if no rendered markdown detected
  232. function tocView(isVisible) {
  233. const toc = $(".ghus-toc");
  234. if (toc) {
  235. toc.classList.toggle("ghus-toc-hidden", !isVisible);
  236. }
  237. }
  238.  
  239. function tocAdd() {
  240. if (!tocInit) {
  241. return;
  242. }
  243. if ($("#wiki-content, #readme")) {
  244. let indx, header, anchor, txt;
  245. let content = "<ul>";
  246. const anchors = $$(".markdown-body .anchor");
  247. const len = anchors.length;
  248. if (len > 1) {
  249. for (indx = 0; indx < len; indx++) {
  250. anchor = anchors[indx];
  251. if (anchor.parentElement) {
  252. header = anchor.parentElement;
  253. // replace single & double quotes with right angled quotes
  254. txt = header.textContent.trim().replace(/'/g, "&#8217;").replace(/"/g, "&#8221;");
  255. content += `
  256. <li class="ghus-toc-${header.nodeName.toLowerCase()}">
  257. <span class="ghus-toc-icon octicon ghd-invert"></span>
  258. <a href="${anchor.hash}" title="${txt}">${txt}</a>
  259. </li>
  260. `;
  261. }
  262. }
  263. $(".boxed-group-inner", container).innerHTML = content + "</ul>";
  264. tocView(true);
  265. listCollapsible();
  266. } else {
  267. tocView();
  268. }
  269. } else {
  270. tocView();
  271. }
  272. }
  273.  
  274. function listCollapsible() {
  275. let indx, el, next, count, num, group;
  276. const els = $$("li", container);
  277. const len = els.length;
  278. const regex = /\d/;
  279. for (indx = 0; indx < len; indx++) {
  280. count = 0;
  281. group = [];
  282. el = els[indx];
  283. next = el && el.nextElementSibling;
  284. if (next) {
  285. num = el.className.match(regex)[0];
  286. while (next && !next.classList.contains("ghus-toc-h" + num)) {
  287. if (next.className.match(regex)[0] > num) {
  288. count++;
  289. group[group.length] = next;
  290. }
  291. next = next.nextElementSibling;
  292. }
  293. if (count > 0) {
  294. el.className += " collapsible collapsible-" + indx;
  295. addClass(group, "ghus-toc-childof-" + indx);
  296. }
  297. }
  298. }
  299. group = [];
  300. on(container, "click", event => {
  301. // Allow doc link to work
  302. if (event.target.nodeName === "A") {
  303. return;
  304. }
  305. stopPropag(event);
  306. // click on icon, then target LI parent
  307. let els, name, indx;
  308. const el = event.target.parentElement;
  309. const collapse = el.classList.contains("collapsed");
  310. if (event.target.classList.contains("ghus-toc-icon")) {
  311. if (event.shiftKey) {
  312. name = el.className.match(/ghus-toc-h\d/);
  313. els = name ? $$("." + name, container) : [];
  314. indx = els.length;
  315. while (indx--) {
  316. collapseChildren(els[indx], collapse);
  317. }
  318. } else {
  319. collapseChildren(el, collapse);
  320. }
  321. removeSelection();
  322. }
  323. });
  324. }
  325.  
  326. function collapseChildren(el, collapse) {
  327. const name = el && el.className.match(/collapsible-(\d+)/);
  328. const children = name ? $$(".ghus-toc-childof-" + name[1], container) : null;
  329. if (children) {
  330. if (collapse) {
  331. el.classList.remove("collapsed");
  332. removeClass(children, "ghus-toc-hidden");
  333. } else {
  334. el.classList.add("collapsed");
  335. addClass(children, "ghus-toc-hidden");
  336. }
  337. }
  338. }
  339.  
  340. // keyboard shortcuts
  341. // GitHub hotkeys are set up to only go to a url, so rolling our own
  342. function keyboardCheck(event) {
  343. clearTimeout(keyboard.timer);
  344. // use "g+t" to toggle the panel; "g+r" to reset the position
  345. // keypress may be needed for non-alphanumeric keys
  346. const tocToggleKeys = defaults.toggle.split("+");
  347. const tocReset = defaults.restore.split("+");
  348. const key = String.fromCharCode(event.which).toLowerCase();
  349. const panelHidden = container.classList.contains("collapsed");
  350.  
  351. // press escape to close the panel
  352. if (event.which === 27 && !panelHidden) {
  353. tocHide();
  354. return;
  355. }
  356. // prevent opening panel while typing in comments
  357. if (/(input|textarea)/i.test(document.activeElement.nodeName)) {
  358. return;
  359. }
  360. // toggle TOC (g+t)
  361. if (keyboard.lastKey === tocToggleKeys[0] && key === tocToggleKeys[1]) {
  362. if (panelHidden) {
  363. tocShow();
  364. } else {
  365. tocHide();
  366. }
  367. }
  368. // reset TOC window position (g+r)
  369. if (keyboard.lastKey === tocReset[0] && key === tocReset[1]) {
  370. container.setAttribute("style", "");
  371. dragSave(true);
  372. }
  373. keyboard.lastKey = key;
  374. keyboard.timer = setTimeout(() => {
  375. keyboard.lastKey = null;
  376. }, defaults.delay);
  377. }
  378.  
  379. async function init() {
  380. // there is no ".header" on github.com/contact; and some other pages
  381. const header = $([...defaults.headerSelector, defaults.headerWrap].join(","));
  382. if (!header || tocInit) {
  383. return;
  384. }
  385. // insert TOC after header
  386. const location = await GM.getValue("github-toc-location", null);
  387. // restore last position
  388. resize(null, ...(location ? location : [null]));
  389.  
  390. // TOC saved state
  391. const hidden = await GM.getValue("github-toc-hidden", false);
  392. container.className = "ghus-toc boxed-group wiki-pages-box readability-sidebar" + (hidden ? " collapsed" : "");
  393. container.setAttribute("role", "navigation");
  394. container.setAttribute("unselectable", "on");
  395. container.setAttribute("index", "0");
  396. container.innerHTML = `
  397. <h3 class="js-wiki-toggle-collapse wiki-auxiliary-content" data-hotkey="${defaults.toggle.replace(/\+/g, " ")}">
  398. <span class="ghus-toc-toggle ghus-toc-icon">
  399. <svg class="octicon" height="14" width="14" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 16 12">
  400. <path d="M2 13c0 .6 0 1-.6 1H.6c-.6 0-.6-.4-.6-1s0-1 .6-1h.8c.6 0 .6.4.6 1zm2.6-9h6.8c.6 0 .6-.4.6-1s0-1-.6-1H4.6C4 2 4 2.4 4 3s0 1 .6 1zM1.4 7H.6C0 7 0 7.4 0 8s0 1 .6 1h.8C2 9 2 8.6 2 8s0-1-.6-1zm0-5H.6C0 2 0 2.4 0 3s0 1 .6 1h.8C2 4 2 3.6 2 3s0-1-.6-1zm10 5H4.6C4 7 4 7.4 4 8s0 1 .6 1h6.8c.6 0 .6-.4.6-1s0-1-.6-1zm0 5H4.6c-.6 0-.6.4-.6 1s0 1 .6 1h6.8c.6 0 .6-.4.6-1s0-1-.6-1z"/>
  401. </svg>
  402. </span>
  403. <span class="ghus-toc-title">${title}</span>
  404. <a class="ghus-toc-docs tooltipped tooltipped-w" aria-label="Go to documentation" href="https://github.com/Mottie/GitHub-userscripts/wiki/GitHub-table-of-contents">
  405. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" height="16" width="14" viewBox="0 0 16 14">
  406. <path d="M6 10h2v2H6V10z m4-3.5c0 2.14-2 2.5-2 2.5H6c0-0.55 0.45-1 1-1h0.5c0.28 0 0.5-0.22 0.5-0.5v-1c0-0.28-0.22-0.5-0.5-0.5h-1c-0.28 0-0.5 0.22-0.5 0.5v0.5H4c0-1.5 1.5-3 3-3s3 1 3 2.5zM7 2.3c3.14 0 5.7 2.56 5.7 5.7S10.14 13.7 7 13.7 1.3 11.14 1.3 8s2.56-5.7 5.7-5.7m0-1.3C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7S10.86 1 7 1z" />
  407. </svg>
  408. </a>
  409. </h3>
  410. <div class="boxed-group-inner wiki-auxiliary-content wiki-auxiliary-content-no-bg"></div>
  411. `;
  412.  
  413. // add container
  414. const el = $(defaults.headerSelector.join(","));
  415. el.parentElement.insertBefore(container, el);
  416.  
  417. // make draggable
  418. on($("h3", container), "mousedown", dragInit);
  419. on(document, "mousemove", dragMove);
  420. on(document, "mouseup", dragStop);
  421. // toggle TOC
  422. on($(".ghus-toc-icon", container), "mouseup", tocToggle);
  423. on($("h3", container), "dblclick", tocToggle);
  424. // prevent container content selection
  425. on(container, "onselectstart", stopPropag);
  426. // keyboard shortcuts
  427. on(document, "keydown", keyboardCheck);
  428. // keep window relative to middle on resize
  429. on(window, "resize", resize);
  430.  
  431. tocInit = true;
  432. tocAdd();
  433. }
  434.  
  435. function $(str, el) {
  436. return (el || document).querySelector(str);
  437. }
  438.  
  439. function $$(str, el) {
  440. return Array.from((el || document).querySelectorAll(str));
  441. }
  442.  
  443. function on(el, name, handler) {
  444. el.addEventListener(name, handler);
  445. }
  446.  
  447. function addClass(els, name) {
  448. let indx;
  449. const len = els.length;
  450. for (indx = 0; indx < len; indx++) {
  451. els[indx].classList.add(name);
  452. }
  453. }
  454.  
  455. function removeClass(els, name) {
  456. let indx;
  457. const len = els.length;
  458. for (indx = 0; indx < len; indx++) {
  459. els[indx].classList.remove(name);
  460. }
  461. }
  462.  
  463. // Add GM options
  464. GM.registerMenuCommand("Set Table of Contents Title", async () => {
  465. title = prompt("Table of Content Title:", title);
  466. await GM.setValue("github-toc-title", title);
  467. $("h3 .ghus-toc-title", container).textContent = title;
  468. });
  469.  
  470. on(document, "ghmo:container", tocAdd);
  471. on(document, "ghmo:preview", tocAdd);
  472. init();
  473.  
  474. })();