AO3 Direct Chapter Index and List Button (Entire Website)

A lazy solution to add buttons to navigate directly to the Chapter Index on AO3 work pages, user dashboard, and other users' works, styled to match existing buttons, working across the entire AO3 site. Or something. It's probably a mess, frankly.

目前为 2024-10-17 提交的版本,查看 最新版本

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