AO3 Entire work auto link and jump

Open "Entire work" directly if the word/chapter ratio is less than 12,000. │ Make the "Entire work" button jump down to the chapter you were reading. │ Add "Entire next→" button at the end of a chapter, switch to "Entire work" and jump to the next chapter without scrolling. │ Shorten the button names to "←Previous" and "Next→" for mobile.

// ==UserScript==
// @name        AO3 Entire work auto link and jump
// @description Open "Entire work" directly if the word/chapter ratio is less than 12,000. │ Make the "Entire work" button jump down to the chapter you were reading. │ Add "Entire next→" button at the end of a chapter, switch to "Entire work" and jump to the next chapter without scrolling. │ Shorten the button names to "←Previous" and "Next→" for mobile.
// @author      C89sd
// @version     1.6
// @match       https://archiveofourown.org/collections/*
// @match       https://archiveofourown.org/tags/*
// @match       https://archiveofourown.org/works/*
// @match       https://archiveofourown.org/works?*
// @match       https://archiveofourown.org/users/*
// @match       https://archiveofourown.org/series/*
// @namespace    https://greasyfork.org/users/1376767
// ==/UserScript==

// Words/Chapter can have commas 1,234
const isDotComma = /[,\.]/g;
function removeCommaAndDot(str) { return str.replace(isDotComma, ''); }

// 1. Make "Entire Work" button jump to current chapter by appending #chapter-N
const button = document.querySelector('li.chapter.entire > a');
if (button) {
  const firstChapter = document.querySelector('div.chapter');
  const id = firstChapter.id;
  if (id) {
    const entireLink = button.href.split('#')[0];
    button.href = entireLink + "#" + id;

    // 1.2. For every button
    // Rename "Previous Chapter" -> "Previous"
    // Rename "Next Chapter"     -> "Next"
    const nextButtonBars = document.querySelectorAll('ul.actions[role="menu"], ul.actions[role="navigation"]:not(.navigation)'); // 2 bars, top & bottom
    for (const bar of nextButtonBars) {
      const previousButton = Array.from(bar.children).find(li => /^←\ ?Previous/.test(li.textContent));
      const nextButton     = Array.from(bar.children).find(li => li.textContent.startsWith("Next"));

      if (previousButton) {
        previousButton.firstChild.textContent = "← Previous";
      }
      if (nextButton) {
        nextButton.firstChild.textContent     = "Next →";

        let isBottomNextButton = nextButton.classList.length === 0; // Top one has ["chapter", "next"]

        // 1.3 Add an "Entire next" button that jumps to the next chapter in Entire work
        if (isBottomNextButton) {
          const customLi   = document.createElement("li");
          const customLink = document.createElement("a");
          const idNext = 1 + parseInt(removeCommaAndDot(id.split('-')[1]), 10)
          customLink.href = entireLink + "#chapter-" + idNext;
          customLink.textContent = "Entire next →";
          customLi.appendChild(customLink);

          nextButton.after(customLi);

          // Add gap
          const spaceNode = document.createTextNode(" ");
          nextButton.after(spaceNode, customLi);
        }
      }
    }
  }
}

const isWork = /works\/(\d+)$/;

// 2. Add '?view_full_work=true' to all links conditionally on word/chapter ratio.
const articles = document.querySelectorAll('li[role="article"]');
for (const article of articles) {

  const link = article.querySelector('h4.heading > a:first-of-type');
  const words    = article.querySelector('dl.stats > dd.words');
  const chapters = article.querySelector('dl.stats > dd.chapters');

  let shouldAdd = true;
  if (words && chapters) {
    const w = parseInt(removeCommaAndDot(words.textContent), 10);
    const c = parseInt(removeCommaAndDot(chapters.textContent).split('/')[0], 10);
    const ratio = Math.floor(w / c);

    if (ratio > 12000) shouldAdd = false;
    console.log(ratio, shouldAdd, w, c, link.href)
  }

  if (shouldAdd) {
    if (isWork.exec(link.href)) {
      link.href += '?view_full_work=true';
    }
  }
}