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.0-20230530_122204
  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. // 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)
  20. if (document.querySelector(`ul.work.navigation.actions li[class="chapter"]`) != null) {
  21.  
  22. // Find the "Chapter Index" button on the main work navbar
  23. var ch_indx_btn = document.querySelector(`ul.work.navigation.actions li[class="chapter"]`);
  24.  
  25. // Get the "Full-page index" button using XPATH
  26. var ini_fpi_btn = document.evaluate(`//*[@id="chapter_index"]/li[*/text()[contains(.,'Full-page index')]]`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  27.  
  28. // Get the href attribute of the "Full-page index" button that normally lives within the "Chapter Index" dropdown
  29. var fpi_href = ini_fpi_btn.querySelector(`a`).getAttribute(`href`);
  30.  
  31. // Create a slightly modified copy of the "Full-page index" button element
  32. var fpi_button = Object.assign(document.createElement(`a`), {
  33. id: `navbar_full_page_index_btn`,
  34. href: `${fpi_href}`,
  35. innerHTML: `Full-page index`
  36. });
  37.  
  38. // Append the modified copy to the "Chapter Index" button element such that it appears to the right of it in the main work navbar
  39. ch_indx_btn.append(fpi_button);
  40.  
  41. // Code to run if the original "Full-page index" button is to be removed
  42. if (remove_ini_fpi_btn == true) {
  43.  
  44. // Remove the "Full-page index" button from the "Chapter Index" dropdown if remove_ini_fpi_btn is set to true
  45. ini_fpi_btn.remove();
  46.  
  47. // Remove the padding-right on the "Go" button in the "Chapter Index" dropdown
  48. document.querySelector(`ul#chapter_index select#selected_id+span`).setAttribute(`style`, `padding-right: 0 !important;`);
  49.  
  50. }
  51.  
  52. }
  53. else if (document.querySelector(`ul.work.navigation.actions li.chapter.bychapter`) != null) {
  54.  
  55. // Find the "Chapter by Chapter" button on the main work navbar
  56. var ch_by_ch_btn = document.querySelector(`ul.work.navigation.actions li.chapter.bychapter`);
  57.  
  58. // Set up RegEx to get the work ID
  59. const re_wid = /(^https?:\/\/)(.*\.)?(archiveofourown\.org)(.*?)(\/works\/\d+)\/?.*$/gmi;
  60.  
  61. // Construct href for the "Full-page Index" button
  62. var fpi_href = `${window.location.href.replace(re_wid, `$5`)}/navigate`;
  63.  
  64. // Create the "Full-page index" button element
  65. var fpi_button = Object.assign(document.createElement(`a`), {
  66. id: `navbar_full_page_index_btn`,
  67. href: `${fpi_href}`,
  68. style: `margin-left: 0.3em`,
  69. innerHTML: `Full-page index`
  70. });
  71.  
  72. // Append the "Full-page index" button to the "Chapter by Chapter" button
  73. ch_by_ch_btn.append(fpi_button);
  74. }
  75.  
  76. })();