// ==UserScript== // @name GitHub TOC // @version 1.1.0 // @description A userscript that adds a table of contents to readme & wiki pages // @license https://creativecommons.org/licenses/by-sa/4.0/ // @namespace http://github.com/Mottie // @include https://github.com/* // @run-at document-idle // @grant GM_registerMenuCommand // @grant GM_getValue // @grant GM_setValue // @grant GM_addStyle // @author Rob Garrison // @downloadURL none // ==/UserScript== /* global GM_registerMenuCommand, GM_getValue, GM_setValue, GM_addStyle */ /*jshint unused:true */ (function() { "use strict"; GM_addStyle([ ".github-toc { position:fixed; z-index:75; min-width:200px; top:55px; right:10px; }", ".github-toc h3 { cursor:move; }", // icon toggles TOC container & subgroups ".github-toc h3 svg, .github-toc li.collapsible .github-toc-icon { cursor:pointer; }", // move collapsed TOC to top right corner ".github-toc.collapsed {", "width:30px; height:30px; min-width:auto; overflow:hidden; top:10px !important; left:auto !important;", "right:10px !important; border:1px solid #d8d8d8; border-radius:3px;", "}", ".github-toc.collapsed > h3 { cursor:pointer; padding-top:5px; border:none; }", // move header text out-of-view when collapsed ".github-toc.collapsed > h3 svg { margin-bottom: 10px; }", ".github-toc-hidden, .github-toc.collapsed .boxed-group-inner,", ".github-toc li:not(.collapsible) .github-toc-icon { display:none; }", ".github-toc .boxed-group-inner { max-width:250px; max-height:400px; overflow-y:auto; overflow-x:hidden; }", ".github-toc ul { list-style:none; }", ".github-toc li { max-width:230px; white-space:nowrap; overflow-x:hidden; text-overflow:ellipsis; }", ".github-toc .github-toc-h1 { padding-left:15px; }", ".github-toc .github-toc-h2 { padding-left:30px; }", ".github-toc .github-toc-h3 { padding-left:45px; }", ".github-toc .github-toc-h4 { padding-left:60px; }", ".github-toc .github-toc-h5 { padding-left:75px; }", ".github-toc .github-toc-h6 { padding-left:90px; }", // anchor collapsible icon ".github-toc li.collapsible .github-toc-icon {", "width:16px; height:16px; 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 ".github-toc li.collapsible.collapsed .github-toc-icon { -webkit-transform:rotate(-90deg); transform:rotate(-90deg); height:10px; width:12px; margin-right:2px; }", ".github-toc-no-selection { -webkit-user-select:none !important; -moz-user-select:none !important; user-select:none !important; }" ].join("")); var targets, // modifiable title title = GM_getValue("github-toc-title", "Table of Contents"), container = document.createElement("div"), busy = false, tocInit = false, // keyboard shortcuts keyboard = { toggle : "g+t", restore : "g+r", timer : null, lastKey : null, delay : 1000 // ms between keyboard shortcuts }, // drag variables drag = { el : null, pos : [ 0, 0 ], elm : [ 0, 0 ], time : 0, unsel: null }; // drag code adapted from http://jsfiddle.net/tovic/Xcb8d/light/ function dragInit() { if (!container.classList.contains("collapsed")) { drag.el = container; drag.elm[0] = drag.pos[0] - drag.el.offsetLeft; drag.elm[1] = drag.pos[1] - drag.el.offsetTop; selectionToggle(true); } else { drag.el = null; } drag.time = new Date().getTime() + 500; } function dragMove(event) { drag.pos[0] = document.all ? window.event.clientX : event.pageX; drag.pos[1] = document.all ? window.event.clientY : event.pageY; if (drag.el !== null) { drag.el.style.left = (drag.pos[0] - drag.elm[0]) + "px"; drag.el.style.top = (drag.pos[1] - drag.elm[1]) + "px"; drag.el.style.right = "auto"; } } function dragStop() { if (drag.el !== null) { dragSave(); selectionToggle(); } drag.el = null; } function dragSave(clear) { var val = clear ? null : [container.style.left, container.style.top]; GM_setValue("github-toc-location", val); } // stop text selection while dragging function selectionToggle(disable) { var body = $("body"); if (disable) { // save current "unselectable" value drag.unsel = body.getAttribute("unselectable"); body.setAttribute("unselectable", "on"); body.classList.add("github-toc-no-selection"); on(body, "onselectstart", selectionStop); } else { if (drag.unsel) { body.setAttribute("unselectable", drag.unsel); } body.classList.remove("github-toc-no-selection"); body.removeEventListener("onselectstart", selectionStop); } removeSelection(); } function selectionStop() { return false; } function removeSelection() { // remove text selection - http://stackoverflow.com/a/3171348/145346 var sel = window.getSelection ? window.getSelection() : document.selection; if ( sel ) { if ( sel.removeAllRanges ) { sel.removeAllRanges(); } else if ( sel.empty ) { sel.empty(); } } } function tocShow() { container.classList.remove("collapsed"); GM_setValue("github-toc-hidden", false); } function tocHide() { container.classList.add("collapsed"); 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(mode) { var toc = $(".github-toc"); if (toc) { toc.style.display = mode || "none"; } } function tocAdd() { // make sure the script is initialized init(); if (!tocInit) { return; } if ($("#wiki-content, #readme")) { var indx, header, anchor, txt, content = "