Instantly share code, notes, and snippets.

@cllu cllu/README.md
Last active Sep 25, 2016

Embed
What would you like to do?
GitHub Wiki TamperMonkey script

GitHub Wiki TamperMonkey script

  • add word count
  • remove unused stuff
  • add Table of Content to the right column
  • Add keyboard shortcuts: double click content area to edit

The script matches only wikis on my own GitHub repos, change it according to your preferences:

// @match        https://github.com/cllu/*/wiki
// @match        https://github.com/cllu/*/wiki/*
// @match        https://github.com/cllu/*/wiki/*/_edit
// ==UserScript==
// @name GitHub Wiki
// @namespace http://tampermonkey.net/
// @version 0.1
// @source https://gist.github.com/cllu/50eb9958c7709a521939
// @description try to take over the world!
// @author Chunliang Lyu
// @match https://github.com/cllu/*/wiki
// @match https://github.com/cllu/*/wiki/*
// @match https://github.com/cllu/*/wiki/*/_edit
// @run-at document-end
// @require http://code.jquery.com/jquery-2.1.1.min.js
// @require https://greasyfork.org/scripts/5542-toc/code/toc.js?version=20193
// @grant GM_addStyle
// ==/UserScript==
/* jshint -W097 */
/* jshint multistr: true */
/* global GM_addStyle,jQuery*/
'use strict';
// add word count to the end of the content
var content = document.querySelector('#wiki-body .markdown-body');
if (content) {
var wordCount = document.createElement('div');
wordCount.className = 'word-count';
wordCount.style.color = 'gray';
wordCount.style.float = 'right';
wordCount.style.fontSize = '14px';
wordCount.textContent = 'words: ' + content.textContent.split(' ').length;
content.insertAdjacentElement('afterend', wordCount);
}
// hide distractions
function hideElement(query) {
var node = document.querySelector(query);
if (node) {
node.style.display = 'none';
}
}
hideElement('.clone-url');
hideElement('.pagehead');
hideElement('.header');
hideElement('.site-footer');
var node = document.querySelector('.gh-header-meta');
if (node) {
node.textContent = '';
}
// adjust styles
var main = document.querySelector('.wiki-wrapper');
main.style.marginTop = '40px';
main.style.marginBottom = '40px';
GM_addStyle("#toc {margin-bottom:25px;padding:20px;} \
#toc h3 {margin: 5px 0 10px 0;}\
#toc ul {margin:0;padding:0;list-style:disc;margin-left:15px} \
#toc li a {padding:2px 0px}#toc a{text-decoration:none;display:block} \
#toc a:hover {background:#F3F4F8}#toc .toc-h1{padding-left:0} \
#toc .toc-h2 {padding-left:0} \
#toc .toc-h3 {padding-left:0px;margin-left:16px;list-style-type: circle;}\
#wiki-body .markdown-body h2 {padding-bottom:.3em;font-size:1.5em;line-height:1.225;border-bottom:2px solid #eee}\
#wiki-body .markdown-body h3 {padding-bottom:.3em;font-size:1.3em;line-height:1.225;border-bottom:1px solid #eee}");
// add table of content to the right
(function($) {
$('#wiki-rightbar .wiki-pages-box').prepend('<div id="toc" class="wiki-custom-sidebar"><h3>Table of Contents</h3><div class="toc-body"></div><div>');
$('#wiki-body .markdown-body').addClass('toc-mian-body');
$('#toc .toc-body').toc({
'selectors': 'h1,h2,h3', //elements to use as headings
'container': '.markdown-body.toc-mian-body', //element to find all selectors in
'smoothScrolling': true, //enable or disable smooth scrolling on click
'prefix': 'toc', //prefix for anchor tags and class names
'onHighlight': function(el) {}, //called when a new section is highlighted
'highlightOnScroll': true, //add class to heading that is currently in focus
'highlightOffset': 100, //offset to trigger the next headline
'anchorName': function(i, heading, prefix) { //custom function for anchor name
return prefix + i;
},
'headerText': function(i, heading, $heading) { //custom function building the header-item text
var rank = $heading.prop("tagName").substr(1);
return $heading.text();
},
});
$('.markdown-body.toc-mian-body').prepend("");
})(jQuery);
// add keyboard shortcuts
// double click to edit
node = document.querySelector('#wiki-body');
if (node) {
node.addEventListener('dblclick', function() {
document.querySelector('.gh-header-actions a').click();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment