Skip to content

Instantly share code, notes, and snippets.

@waldyrious
Created December 26, 2022 11:21
Show Gist options
  • Save waldyrious/8eea1db826e9890373a127415c70d3d3 to your computer and use it in GitHub Desktop.
Save waldyrious/8eea1db826e9890373a127415c70d3d3 to your computer and use it in GitHub Desktop.
Bookmarklet to add visible anchors to section headings
// Inspired by https://attacomsian.com/blog/deep-anchor-links-javascript
// How to use: Create a new browser bookmark with the code below as the URL.
// See https://www.freecodecamp.org/news/what-are-bookmarklets/
// for more background and detailed instructions.
javascript:document.querySelectorAll('h2[id], h3[id], h4[id], h5[id], h6[id]').forEach(heading => {
let anchor = document.createElement('a');
anchor.href = '#' + heading.getAttribute('id');
anchor.innerText = '#';
anchor.style = 'margin-left: 0.3em';
heading.appendChild(anchor);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment