AO3 Direct Chapter Index and List Button (Entire Website)

A solution to add buttons to navigate directly to the Chapter Index on AO3 work pages, chapter pages, user dashboard, bookmarks, and other users' works, styled to match existing buttons, working across the entire AO3 site.

  1. // ==UserScript==
  2. // @name AO3 Direct Chapter Index and List Button (Entire Website)
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.3
  5. // @description A solution to add buttons to navigate directly to the Chapter Index on AO3 work pages, chapter pages, user dashboard, bookmarks, and other users' works, styled to match existing buttons, working across the entire AO3 site.
  6. // @author stroke6
  7. // @license MIT
  8. // @match https://archiveofourown.org/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to add the "Go to Chapter Index" button on work pages
  16. function addButtonOnWorkPage() {
  17. const navContainer = document.querySelector("#main > ul.work.navigation.actions");
  18. const workIdMatch = window.location.href.match(/works\/(\d+)/);
  19.  
  20. if (navContainer && workIdMatch && workIdMatch[1]) {
  21. const newButtonItem = document.createElement("li");
  22. const button = document.createElement("a");
  23.  
  24. button.innerHTML = "Go to Chapter Index";
  25. button.classList.add("button");
  26. button.style.cursor = "pointer";
  27. button.href = `https://archiveofourown.org/works/${workIdMatch[1]}/navigate`;
  28.  
  29. newButtonItem.appendChild(button);
  30. navContainer.appendChild(newButtonItem);
  31. } else {
  32. console.error('Work ID not found in the URL or navigation container missing.');
  33. }
  34. }
  35.  
  36. // Function to add the "Go to Chapter Index" button on chapter pages
  37. function addButtonOnChapterPage() {
  38. const navContainer = document.querySelector("#main > div.work > ul");
  39. const workIdMatch = window.location.href.match(/works\/(\d+)/);
  40.  
  41. if (navContainer && workIdMatch && workIdMatch[1]) {
  42. const newButtonItem = document.createElement("li");
  43. const button = document.createElement("a");
  44.  
  45. button.innerHTML = "Go to Chapter Index";
  46. button.classList.add("button");
  47. button.style.cursor = "pointer";
  48. button.href = `https://archiveofourown.org/works/${workIdMatch[1]}/navigate`;
  49.  
  50. newButtonItem.appendChild(button);
  51. navContainer.appendChild(newButtonItem);
  52. } else {
  53. console.error('Work ID not found in the URL or navigation container missing.');
  54. }
  55. }
  56.  
  57. // Function to add a 'Chapter Index' button on the user dashboard
  58. function addButtonOnDashboard() {
  59. const workElements = document.querySelectorAll('[id^="work_"] ul.actions');
  60.  
  61. workElements.forEach(function(workElement) {
  62. const workId = workElement.closest('[id^="work_"]').id.replace('work_', '');
  63. if (workId) {
  64. const smallButton = document.createElement("a");
  65. smallButton.innerHTML = "Chapter Index";
  66. smallButton.classList.add("button");
  67. smallButton.style.cursor = "pointer";
  68. smallButton.href = `https://archiveofourown.org/works/${workId}/navigate`;
  69.  
  70. workElement.appendChild(smallButton);
  71. }
  72. });
  73. }
  74.  
  75. // Function to add "Chapter Index" text link to works or bookmarks
  76. function addChapterListLinkToWorksOrBookmarks() {
  77. const bookmarkElements = document.querySelectorAll('[id^="bookmark_"] dl');
  78. const workElements = document.querySelectorAll('[id^="work_"] dl');
  79.  
  80. // For bookmarks
  81. bookmarkElements.forEach(function(bookmarkElement) {
  82. const bookmarkId = bookmarkElement.closest('[id^="bookmark_"]').id.replace('bookmark_', '');
  83. const workId = bookmarkElement.querySelector('a[href*="/works/"]').href.match(/works\/(\d+)/)[1];
  84.  
  85. if (workId) {
  86. const chapterListElement = document.createElement("dd");
  87. const chapterListLink = document.createElement("a");
  88.  
  89. chapterListLink.innerHTML = "Chapter Index";
  90. chapterListLink.href = `https://archiveofourown.org/works/${workId}/navigate`;
  91. chapterListLink.style.cursor = "pointer";
  92.  
  93. chapterListElement.appendChild(chapterListLink);
  94. bookmarkElement.appendChild(chapterListElement);
  95. }
  96. });
  97.  
  98. // For works
  99. workElements.forEach(function(workElement) {
  100. const workId = workElement.closest('[id^="work_"]').id.replace('work_', '');
  101.  
  102. if (workId) {
  103. const chapterListElement = document.createElement("dd");
  104. const chapterListLink = document.createElement("a");
  105.  
  106. chapterListLink.innerHTML = "Chapter Index";
  107. chapterListLink.href = `https://archiveofourown.org/works/${workId}/navigate`;
  108. chapterListLink.style.cursor = "pointer";
  109.  
  110. chapterListElement.appendChild(chapterListLink);
  111. workElement.appendChild(chapterListElement);
  112. }
  113. });
  114. }
  115.  
  116. // Update handlePage function to detect chapter pages and add buttons accordingly
  117. function handlePage() {
  118. const currentURL = window.location.href;
  119.  
  120. if (currentURL.includes("/works/") && currentURL.includes("/chapters/")) {
  121. // For chapter pages
  122. addButtonOnChapterPage();
  123. }
  124.  
  125. else if (currentURL.includes("/works/")) {
  126. // For work pages
  127. addButtonOnWorkPage();
  128. addChapterListLinkToWorksOrBookmarks();
  129. }
  130.  
  131. else if (currentURL.includes("/collections/")) {
  132. // For collections
  133. addButtonOnWorkPage();
  134. addButtonOnDashboard();
  135. addChapterListLinkToWorksOrBookmarks();
  136. }
  137.  
  138. else if (currentURL.includes("/users/")) {
  139. // For user-specific pages (dashboard, etc.)
  140. addButtonOnDashboard();
  141. addChapterListLinkToWorksOrBookmarks();
  142. }
  143.  
  144. else if (currentURL.includes("/bookmarks/")) {
  145. // For bookmarks
  146. addChapterListLinkToWorksOrBookmarks();
  147. }
  148. else {
  149. // For other pages where works might be listed, such as tags
  150. addChapterListLinkToWorksOrBookmarks();
  151. }
  152. }
  153.  
  154. // Run the handler when the page loads
  155. window.onload = function() {
  156. handlePage();
  157. };
  158.  
  159. })();