ToC as side bar

Restrict width and move Table of Content to the right side.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        ToC as side bar
// @namespace   https://franklinyu.name
// @match       https://wiki.archlinux.org/title/*
// @exclude     https://wiki.archlinux.org/title/Special:*
// @grant       GM_addStyle
// @version     0.3
// @author      Franklin Yu
// @description Restrict width and move Table of Content to the right side.
// ==/UserScript==

function main() {
  const toc = document.getElementById('toc')
  const container = document.getElementById('mw-content-text')
  if (!toc) {
    console.info('ToC not found.')
    return
  }

  // TODO: find out why it broke on Firefox.
  if (navigator.userAgent.indexOf('Chrome') === -1) {
    console.warn('Only Chrome is supported now.')
  } else {
    container.append(toc)
  }

  GM_addStyle(`
  #mw-content-text {
    display: flex;
  }
  .mw-parser-output {
    max-width: 850px;
  }
  #mw-content-text > #toc {
    position: sticky;
    top: 10px;
    margin-left: 10px;
    max-height: 95vh;
  }
  #toc > ul {
    max-height: 90vh;
    overflow: auto;
  }
  `)
}

// When there is a Wikitable, typically it would be very wide. Examples:
//   - https://wiki.archlinux.org/title/XDG_Base_Directory
//   - https://wiki.archlinux.org/title/Arch_boot_process
//
// TODO: allow pages where all tables are small
if (document.getElementsByClassName('wikitable').length === 0) {
  main()
}

// QA: To test this page, visit following sites
//   - simple example https://wiki.archlinux.org/title/KVM
//   - long ToC https://wiki.archlinux.org/title/QEMU
//   - Wikitable https://wiki.archlinux.org/title/XDG_Base_Directory