AO3: Visible Full-page Index Button

Moves or copies (setting is user configurable) the "Full-page index" button to the main work navigation bar for ease of access

  1. // ==UserScript==
  2. // @name AO3: Visible Full-page Index Button
  3. // @namespace https://github.com/w4tchdoge
  4. // @version 1.3.4-20240611_164932
  5. // @description Moves or copies (setting is user configurable) the "Full-page index" button to the main work navigation bar for ease of access
  6. // @author w4tchdoge
  7. // @homepage https://github.com/w4tchdoge/MISC-UserScripts
  8. // @match *://archiveofourown.org/*chapters/*
  9. // @match *://archiveofourown.org/*works/*
  10. // @exclude *://archiveofourown.org/*works/*/bookmarks
  11. // @exclude *://archiveofourown.org/*works/*/navigate
  12. // @icon https://archiveofourown.org/favicon.ico
  13. // @license AGPL-3.0-or-later
  14. // @history 1.3.4 — Add @exclude rule so that userscript doesn't run on /navigate pages. Add match rule so that userscript runs on chaptes pages that don't include "works" in the URL
  15. // @history 1.3.3 — Minor cleanup
  16. // @history 1.3.2 — Modify the match rule so that it matches collections/*/works URLs as well; Add an exlude role so it doesn't work on works/*/bookmarks pages as it isn't designed to
  17. // ==/UserScript==
  18.  
  19. (function () {
  20. `use strict`;
  21.  
  22. /* IMPORTANT – Change this value depending on whether or not you want the initial "Full-page index" button remove from the "Chapter Index" dropdown – IMPORTANT */
  23. const remove_ini_fpi_btn = true;
  24.  
  25. /* IMPORTANT- Change this value to false if you do not want a "Full-page index" button when viewing an entire work */
  26. const enti_wrk_fpi_btn = true;
  27.  
  28.  
  29. // Only execute the rest of the script if the "Chapter Index" element exists (this is so it doesn't throw an error in the console when viewing an entire work or a single chapter work)
  30. if (document.querySelector(`ul.work.navigation.actions li[class="chapter"]`) != null) {
  31.  
  32. // Find the "Chapter Index" button on the main work navbar
  33. const ch_indx_btn = document.querySelector(`ul.work.navigation.actions li[class="chapter"]`);
  34.  
  35. // Get the "Full-page index" button using XPATH
  36. const ini_fpi_btn = document.evaluate(`//*[@id="chapter_index"]/li[*/text()[contains(.,'Full-page index')]]`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  37.  
  38. // Get the href attribute of the "Full-page index" button that normally lives within the "Chapter Index" dropdown
  39. const fpi_href = ini_fpi_btn.querySelector(`a`).getAttribute(`href`);
  40.  
  41. // Create a slightly modified copy of the "Full-page index" button element
  42. const fpi_button = Object.assign(document.createElement(`a`), {
  43. id: `navbar_full_page_index_btn`,
  44. href: fpi_href,
  45. innerHTML: `Full-page index`
  46. });
  47.  
  48. // Append the modified copy to the "Chapter Index" button element such that it appears to the right of it in the main work navbar
  49. ch_indx_btn.append(fpi_button);
  50.  
  51. // Code to run if the original "Full-page index" button is to be removed
  52. if (remove_ini_fpi_btn == true) {
  53. // Remove the "Full-page index" button from the "Chapter Index" dropdown if remove_ini_fpi_btn is set to true
  54. ini_fpi_btn.remove();
  55.  
  56. // Remove the padding-right on the "Go" button in the "Chapter Index" dropdown
  57. document.querySelector(`ul#chapter_index select#selected_id+span`).setAttribute(`style`, `padding-right: 0 !important;`);
  58. }
  59. }
  60.  
  61. // Add a "Full-page index" button when viewing an entire work (enti_wrk_fpi_btn must be set to true)
  62. if (document.querySelector(`ul.work.navigation.actions li.chapter.bychapter`) != null && enti_wrk_fpi_btn) {
  63.  
  64. // Find the "Chapter by Chapter" button on the main work navbar
  65. const ch_by_ch_btn = document.querySelector(`ul.work.navigation.actions li.chapter.bychapter`);
  66.  
  67. // Construct href for the "Full-page Index" button
  68. const fpi_href = (function () {
  69. const
  70. curr_page_url = new URL(window.location),
  71. fpi_href = `${curr_page_url.pathname}/navigate`;
  72. return fpi_href;
  73. })();
  74.  
  75. // Create the "Full-page index" button element
  76. const fpi_button = Object.assign(document.createElement(`a`), {
  77. id: `navbar_full_page_index_btn`,
  78. href: fpi_href,
  79. style: `margin-left: 0.3em`,
  80. innerHTML: `Full-page index`
  81. });
  82.  
  83. // Append the "Full-page index" button to the "Chapter by Chapter" button
  84. ch_by_ch_btn.append(fpi_button);
  85. }
  86.  
  87. })();