Skip to content

Instantly share code, notes, and snippets.

@cllu cllu/

Last active Sep 25, 2016
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*/wiki
// @match*/wiki/*
// @match*/wiki/*/_edit
// ==UserScript==
// @name GitHub Wiki
// @namespace
// @version 0.1
// @source
// @description try to take over the world!
// @author Chunliang Lyu
// @match*/wiki
// @match*/wiki/*
// @match*/wiki/*/_edit
// @run-at document-end
// @require
// @require
// @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'; = 'gray'; = 'right'; = '14px';
wordCount.textContent = 'words: ' + content.textContent.split(' ').length;
content.insertAdjacentElement('afterend', wordCount);
// hide distractions
function hideElement(query) {
var node = document.querySelector(query);
if (node) { = 'none';
var node = document.querySelector('.gh-header-meta');
if (node) {
node.textContent = '';
// adjust styles
var main = document.querySelector('.wiki-wrapper'); = '40px'; = '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();
// 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
You can’t perform that action at this time.