AO3: Bookmarks Button on Main Navbar

Adds a button called "Bookmarked Fics" to the main navbar (where the search bar is). Inspired by elli-lili-lunch's "Put Bookmarks Button on AO3 Home", which can be found at https://greasyfork.org/en/scripts/440048

  1. // ==UserScript==
  2. // @name AO3: Bookmarks Button on Main Navbar
  3. // @namespace https://github.com/w4tchdoge
  4. // @version 1.0.5-20240518_193303
  5. // @description Adds a button called "Bookmarked Fics" to the main navbar (where the search bar is). Inspired by elli-lili-lunch's "Put Bookmarks Button on AO3 Home", which can be found at https://greasyfork.org/en/scripts/440048
  6. // @author w4tchdoge
  7. // @homepage https://github.com/w4tchdoge/MISC-UserScripts
  8. // @match *://archiveofourown.org/*
  9. // @icon https://archiveofourown.org/favicon.ico
  10. // @run-at document-idle
  11. // @license AGPL-3.0-or-later
  12. // @history 1.0.5 — Some minor cleanup, details are: make initial bkmrks_btn clone the node instead of giving me a live node; make bkmrks_url, bkmrks_txt, and srch_bar consts because i'm not changing them in any way; change the single quotes used in the innerHTML of Object.assign to be double quotes so as to reflect the created element which uses double quotes; use .trim() on bkmrks_btn.textContent as a just-in-case
  13. // @history 1.0.4 — Rollback 1.0.2 because I've realised that having this script running sidewide was the intention and I was a big dumb by removing that
  14. // @history 1.0.3 — Modify the XPath to get the existing "My Bookmarks" button to accommodate the DOM element with id "greeting" not being a div element
  15. // @history 1.0.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
  16. // @history 1.0.1 — Change the displayed text to be the same as in the original bookmarks button from the user dropdown area
  17. // @history 1.0.0 — Initial Release
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. `use strict`;
  22.  
  23. // Get the user bookmark page button from the user dropdown
  24. var bkmrks_btn = document.evaluate(`.//*[@id="greeting"]//li//a[text()[contains(.,'My Bookmarks')]]`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.cloneNode(true);
  25.  
  26. // Get the URL and displayed text from the user bookmarks button
  27. const
  28. bkmrks_url = bkmrks_btn.getAttribute(`href`),
  29. bkmrks_txt = bkmrks_btn.textContent.trim();
  30.  
  31. // Create the new Bookmark button element
  32. var bkmrks_btn = Object.assign(document.createElement(`li`), {
  33. id: `navbar_bookmarks_button`,
  34. innerHTML: `<a href="${bkmrks_url}"><span class="dropdown-toggle">${bkmrks_txt}</span></a>`
  35. });
  36.  
  37. // Find Search bar
  38. const srch_bar = document.querySelector(`ul.primary.navigation.actions li.search`);
  39.  
  40. // Add created element before search bar
  41. srch_bar.before(bkmrks_btn);
  42.  
  43. })();