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

当前为 2024-03-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AO3: Visible Full-page Index Button
  3. // @namespace https://github.com/w4tchdoge
  4. // @version 1.3.2-20240314_205855
  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/*works/*
  9. // @exclude *://archiveofourown.org/*works/*/bookmarks
  10. // @icon https://archiveofourown.org/favicon.ico
  11. // @license AGPL-3.0-or-later
  12. // @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
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. `use strict`;
  17.  
  18. /* IMPORTANT – Change this value depending on whether or not you want the initial "Full-page index" button remove from the "Chapter Index" dropdown – IMPORTANT */
  19. const remove_ini_fpi_btn = true;
  20.  
  21. /* IMPORTANT- Change this value to false if you do not want a "Full-page index" button when viewing an entire work */
  22. const enti_wrk_fpi_btn = true;
  23.  
  24. // 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)
  25. if (document.querySelector(`ul.work.navigation.actions li[class="chapter"]`) != null) {
  26.  
  27. // Find the "Chapter Index" button on the main work navbar
  28. var ch_indx_btn = document.querySelector(`ul.work.navigation.actions li[class="chapter"]`);
  29.  
  30. // Get the "Full-page index" button using XPATH
  31. var ini_fpi_btn = document.evaluate(`//*[@id="chapter_index"]/li[*/text()[contains(.,'Full-page index')]]`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  32.  
  33. // Get the href attribute of the "Full-page index" button that normally lives within the "Chapter Index" dropdown
  34. var fpi_href = ini_fpi_btn.querySelector(`a`).getAttribute(`href`);
  35.  
  36. // Create a slightly modified copy of the "Full-page index" button element
  37. var fpi_button = Object.assign(document.createElement(`a`), {
  38. id: `navbar_full_page_index_btn`,
  39. href: `${fpi_href}`,
  40. innerHTML: `Full-page index`
  41. });
  42.  
  43. // Append the modified copy to the "Chapter Index" button element such that it appears to the right of it in the main work navbar
  44. ch_indx_btn.append(fpi_button);
  45.  
  46. // Code to run if the original "Full-page index" button is to be removed
  47. if (remove_ini_fpi_btn == true) {
  48.  
  49. // Remove the "Full-page index" button from the "Chapter Index" dropdown if remove_ini_fpi_btn is set to true
  50. ini_fpi_btn.remove();
  51.  
  52. // Remove the padding-right on the "Go" button in the "Chapter Index" dropdown
  53. document.querySelector(`ul#chapter_index select#selected_id+span`).setAttribute(`style`, `padding-right: 0 !important;`);
  54.  
  55. }
  56.  
  57. }
  58. // Add a "Full-page index" button when viewing an entire work (enti_wrk_fpi_btn must be set to true)
  59. else if (document.querySelector(`ul.work.navigation.actions li.chapter.bychapter`) != null && enti_wrk_fpi_btn) {
  60.  
  61. // Find the "Chapter by Chapter" button on the main work navbar
  62. var ch_by_ch_btn = document.querySelector(`ul.work.navigation.actions li.chapter.bychapter`);
  63.  
  64. // Set up RegEx to get the work ID
  65. const re_wid = /(^https?:\/\/)(.*\.)?(archiveofourown\.org)(.*?)(\/works\/\d+)\/?.*$/gmi;
  66.  
  67. // Construct href for the "Full-page Index" button
  68. var fpi_href = `${window.location.href.replace(re_wid, `$5`)}/navigate`;
  69.  
  70. // Create the "Full-page index" button element
  71. var fpi_button = Object.assign(document.createElement(`a`), {
  72. id: `navbar_full_page_index_btn`,
  73. href: `${fpi_href}`,
  74. style: `margin-left: 0.3em`,
  75. innerHTML: `Full-page index`
  76. });
  77.  
  78. // Append the "Full-page index" button to the "Chapter by Chapter" button
  79. ch_by_ch_btn.append(fpi_button);
  80. }
  81.  
  82. })();