Reading Helper

Makes reading text on websites a more pleasant experience

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         Reading Helper
// @namespace    http://www.mikesbytes.net/userscripts
// @version      1.0.0
// @description  Makes reading text on websites a more pleasant experience
// @author       Michael Moreno
// @homepageURL  https://greasyfork.org/en/scripts/577351-reading-helper
// @match        http://*/*
// @match        https://*/*
// @license      GPL-3.0
// ==/UserScript==

function createLinksToHeadings(headingType) {
  const h2Tags = document.querySelectorAll(headingType);

  for (let i = 0; i < h2Tags.length; i++) {
    const tag = h2Tags[i];

    if (tag.closest("a")) {
      continue;
    }

    const newLink = document.createElement("a");
    newLink.className = "reading-helper-link";
    newLink.style.color = "inherit";
    newLink.style.textDecoration = "none";

    const existingTagId = tag.id;

    if (existingTagId) {
      newLink.href = `#${existingTagId}`;
    } else {
      const customId = `reading-helper-${headingType}-${i + 1}`;

      tag.id = customId;

      newLink.href = `#${customId}`;
    }

    tag.before(newLink);

    newLink.appendChild(tag);
  }
}

function main() {
  for (let i = 1; i < 7; i++) {
    createLinksToHeadings(`h${i}`);
  }
}

main();