Skip to content

Instantly share code, notes, and snippets.

@rigwild
Created July 4, 2025 10:38
Show Gist options
  • Save rigwild/d9326e78084c90c2bd3902bb64cb0bc9 to your computer and use it in GitHub Desktop.
Save rigwild/d9326e78084c90c2bd3902bb64cb0bc9 to your computer and use it in GitHub Desktop.
Fix Python sidebar table of contents links are missing
// ==UserScript==
// @name Python doc fix sidebar links
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Fix Python documentation sidebar table of contents to include header links
// @author rigwild
// @match https://docs.python.org/*/library/*.html*
// @icon https://www.google.com/s2/favicons?sz=64&domain=python.org
// @grant none
// ==/UserScript==
;(function () {
'use strict'
const sidebarMenuUl = document.querySelector('.sphinxsidebar ul ul')
const headerLinks = [...document.querySelectorAll('.headerlink')]
if (!sidebarMenuUl || headerLinks.length === 0) return
const createLink = (href, text) => {
const listItem = document.createElement('li')
listItem.innerHTML = `<a class="reference internal" href="${href}"><code class="xref py py-class docutils literal notranslate"><span class="pre">${text}</span></code></a>`
return listItem
}
sidebarMenuUl.appendChild(createLink('#', '')) // Spacer at the top
headerLinks.forEach(link => {
const href = link.getAttribute('href')
const ref = href?.split('#')[1]
if (href) {
const listItem = createLink(href, ref)
sidebarMenuUl.appendChild(listItem)
}
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment