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.0
  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. (function() {
  13. 'use strict';
  14.  
  15. // Function to add the "Go to Chapter Index" button on work pages — it's up there, with the other buttons
  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 a 'Chapters' button on the user dashboard —right next to the 'Add Chapter' button.
  37. function addButtonOnDashboard() {
  38. const workElements = document.querySelectorAll('[id^="work_"] ul.actions');
  39.  
  40. workElements.forEach(function(workElement) {
  41. const workId = workElement.closest('[id^="work_"]').id.replace('work_', '');
  42. if (workId) {
  43. const smallButton = document.createElement("a");
  44. smallButton.innerHTML = "Chapters";
  45. smallButton.classList.add("button");
  46. smallButton.style.cursor = "pointer";
  47. smallButton.href = `https://archiveofourown.org/works/${workId}/navigate`;
  48.  
  49. workElement.appendChild(smallButton);
  50. }
  51. });
  52. }
  53.  
  54. // Function to add "Chapter List" text link to other users' works — and in your own dashoard, because just one wasn't enough
  55. function addChapterListLinkToOtherUsersWorks() {
  56. const workElements = document.querySelectorAll('[id^="work_"] dl');
  57.  
  58. workElements.forEach(function(workElement) {
  59. const workId = workElement.closest('[id^="work_"]').id.replace('work_', '');
  60. if (workId) {
  61. const chapterListElement = document.createElement("dd");
  62. const chapterListLink = document.createElement("a");
  63.  
  64. chapterListLink.innerHTML = "Chapter List";
  65. chapterListLink.href = `https://archiveofourown.org/works/${workId}/navigate`;
  66. chapterListLink.style.cursor = "pointer";
  67.  
  68. chapterListElement.appendChild(chapterListLink);
  69. workElement.appendChild(chapterListElement);
  70. }
  71. });
  72. }
  73.  
  74. // Function to handle adding buttons across the entire site — determines which of the three functions above to run.
  75. function handlePage() {
  76. const currentURL = window.location.href;
  77.  
  78. if (currentURL.includes("/works/")) {
  79. // For work pages
  80. addButtonOnWorkPage();
  81. addChapterListLinkToOtherUsersWorks();
  82. }
  83.  
  84. else if (currentURL.includes("/users/")) {
  85. // For user-specific pages (dashboard, etc.)
  86. addButtonOnDashboard();
  87. addChapterListLinkToOtherUsersWorks();
  88. } else {
  89. // For other pages where works might be listed, such as tags — likely a lazy solution
  90. addChapterListLinkToOtherUsersWorks();
  91. addButtonOnDashboard();
  92. }
  93. }
  94.  
  95. // Run the handler when the page loads
  96. window.onload = function() {
  97. handlePage();
  98. };
  99.  
  100. })();