// ==UserScript== // @name GitHub Table of Contents // @version 2.0.5 // @description A userscript that adds a table of contents to readme & wiki pages // @license MIT // @author Rob Garrison // @namespace https://github.com/Mottie // @include https://github.com/* // @include https://gist.github.com/* // @run-at document-idle // @grant GM_registerMenuCommand // @grant GM_getValue // @grant GM.getValue // @grant GM_setValue // @grant GM.setValue // @grant GM_addStyle // @grant GM.addStyle // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=666427 // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103 // @icon https://github.githubassets.com/pinned-octocat.svg // @downloadURL none // ==/UserScript== (async () => { "use strict"; const defaults = { title: "Table of Contents", // popup title top: "64px", // popup top position when reset left: "auto", // popup left position when reset right: "10px", // popup right position when reset headerPad: "48px", // padding added to header when TOC is collapsed headerSelector: [".header", ".Header"], headerWrap: ".js-header-wrapper", toggle: "g+t", // keyboard toggle shortcut restore: "g+r", // keyboard reset popup position shortcut delay: 1000, // ms between keyboard shortcuts }; GM.addStyle(` /* z-index > 1000 to be above the */ .ghus-toc { position:fixed; z-index:1001; min-width:200px; top:${defaults.top}; right:${defaults.right}; } .ghus-toc h3 { cursor:move; } .ghus-toc-title { padding-left:20px; } /* icon toggles TOC container & subgroups */ .ghus-toc .ghus-toc-icon { vertical-align:baseline; } .ghus-toc h3 .ghus-toc-icon, .ghus-toc li.collapsible .ghus-toc-icon { cursor:pointer; } .ghus-toc .ghus-toc-toggle { position:absolute; width:28px; height:38px; top:0px; left:0px; } .ghus-toc .ghus-toc-toggle svg { margin-top:10px; margin-left:9px; } .ghus-toc .ghus-toc-docs { float:right; } /* move collapsed TOC to top right corner */ .ghus-toc.collapsed { width:30px; height:30px; min-width:auto; overflow:hidden; top:16px !important; left:auto !important; right:10px !important; border:1px solid rgba(128, 128, 128, 0.5); border-radius:3px; } .ghus-toc.collapsed > h3 { cursor:pointer; padding-top:5px; border:none; background:#222; color:#ddd; } .ghus-toc.collapsed .ghus-toc-docs { display:none; } .ghus-toc:not(.ghus-toc-hidden).collapsed + .Header { padding-right: ${defaults.headerPad} !important; } /* move header text out-of-view when collapsed */ .ghus-toc.collapsed > h3 svg { margin-top:6px; } .ghus-toc-hidden, .ghus-toc.collapsed .boxed-group-inner, .ghus-toc li:not(.collapsible) .ghus-toc-icon { display:none; } .ghus-toc .boxed-group-inner { max-width:250px; max-height:400px; overflow-y:auto; overflow-x:hidden; } .ghus-toc ul { list-style:none; } .ghus-toc li { max-width:230px; white-space:nowrap; overflow-x:hidden; text-overflow:ellipsis; } .ghus-toc .ghus-toc-h1 { padding-left:15px; } .ghus-toc .ghus-toc-h2 { padding-left:30px; } .ghus-toc .ghus-toc-h3 { padding-left:45px; } .ghus-toc .ghus-toc-h4 { padding-left:60px; } .ghus-toc .ghus-toc-h5 { padding-left:75px; } .ghus-toc .ghus-toc-h6 { padding-left:90px; } /* anchor collapsible icon */ .ghus-toc li.collapsible .ghus-toc-icon { width:16px; height:10px; display:inline-block; margin-left:-16px; background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGNsYXNzPSdvY3RpY29uJyBoZWlnaHQ9JzE0JyB2aWV3Qm94PScwIDAgMTIgMTYnPjxwYXRoIGQ9J00wIDVsNiA2IDYtNkgweic+PC9wYXRoPjwvc3ZnPg==) left center no-repeat; } /* on rotate, height becomes width, so this is keeping things lined up */ .ghus-toc li.collapsible.collapsed .ghus-toc-icon { -webkit-transform:rotate(-90deg); transform:rotate(-90deg); height:10px; width:12px; margin-right:2px; } .ghus-toc-icon svg, .ghus-toc-docs svg { pointer-events:none; } .ghus-toc-no-selection { -webkit-user-select:none !important; -moz-user-select:none !important; user-select:none !important; } /* prevent google translate from breaking links */ .ghus-toc li a font { pointer-events:none; } `); let tocInit = false; // modifiable title let title = await GM.getValue("github-toc-title", defaults.title); const container = document.createElement("div"); const useClient = !!document.all; // keyboard shortcuts const keyboard = { timer: null, lastKey: null }; // drag variables const drag = { el: null, elmX: 0, elmY: 0, time: 0, unsel: null }; const stopPropag = event => { event.preventDefault(); event.stopPropagation(); }; // drag code adapted from http://jsfiddle.net/tovic/Xcb8d/light/ function dragInit(event) { if (!container.classList.contains("collapsed")) { const x = useClient ? window.event.clientX : event.pageX; const y = useClient ? window.event.clientY : event.pageY; drag.el = container; drag.elmX = x - drag.el.offsetLeft; drag.elmY = y - drag.el.offsetTop; selectionToggle(true); } else { drag.el = null; } drag.time = new Date().getTime() + 500; } function dragMove(event) { if (drag.el !== null) { const x = useClient ? window.event.clientX : event.pageX; const y = useClient ? window.event.clientY : event.pageY; drag.el.style.left = (x - drag.elmX) + "px"; drag.el.style.top = (y - drag.elmY) + "px"; drag.el.style.right = "auto"; } } function dragStop() { if (drag.el !== null) { dragSave(); selectionToggle(); } drag.el = null; } async function dragSave(restore) { let adjLeft = null; let top = null; let val = null; if (restore) { // position restore (reset) popup to default position setPosition(defaults.left, defaults.top, defaults.right); } else { // Adjust saved left position to be measured from the center of the window // See issue #102 const winHalf = window.innerWidth / 2; const left = winHalf - parseInt(container.style.left, 10); adjLeft = left * (left > winHalf ? 1 : -1); top = parseInt(container.style.top, 10); val = [adjLeft, top]; } drag.elmX = adjLeft; drag.elmY = top; await GM.setValue("github-toc-location", val); } function resize(_, left = drag.elmX, top = drag.elmY) { if (left !== null) { drag.elmX = left; drag.elmY = top; setPosition(((window.innerWidth / 2) + left) + "px", top + "px"); } } function setPosition(left, top, right = "auto") { container.style.left = left; container.style.right = right; container.style.top = top; } // stop text selection while dragging function selectionToggle(disable) { const body = $("body"); if (disable) { // save current "unselectable" value drag.unsel = body.getAttribute("unselectable"); body.setAttribute("unselectable", "on"); body.classList.add("ghus-toc-no-selection"); on(body, "onselectstart", stopPropag); } else { if (drag.unsel) { body.setAttribute("unselectable", drag.unsel); } body.classList.remove("ghus-toc-no-selection"); body.removeEventListener("onselectstart", stopPropag); } removeSelection(); } function removeSelection() { // remove text selection - http://stackoverflow.com/a/3171348/145346 const sel = window.getSelection ? window.getSelection() : document.selection; if (sel) { if (sel.removeAllRanges) { sel.removeAllRanges(); } else if (sel.empty) { sel.empty(); } } } async function tocShow() { container.classList.remove("collapsed"); await GM.setValue("github-toc-hidden", false); } async function tocHide() { container.classList.add("collapsed"); await GM.setValue("github-toc-hidden", true); } function tocToggle() { // don't toggle content on long clicks if (drag.time > new Date().getTime()) { if (container.classList.contains("collapsed")) { tocShow(); } else { tocHide(); } } } // hide TOC entirely, if no rendered markdown detected function tocView(isVisible) { const toc = $(".ghus-toc"); if (toc) { toc.classList.toggle("ghus-toc-hidden", !isVisible); } } function tocAdd() { if (!tocInit) { return; } const wrapper = $("#wiki-body, #readme"); if (wrapper) { let indx, header, anchor, txt; let content = "