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

当前为 2023-05-30 提交的版本,查看 最新版本

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