AO3: Links to Last Chapter and Entire Works

Add links to last chapter and entire works right after title of story.

  1. // ==UserScript==
  2. // @name AO3: Links to Last Chapter and Entire Works
  3. // @namespace https://greasyfork.org/en/users/163551-vannius
  4. // @version 2.1
  5. // @license MIT
  6. // @description Add links to last chapter and entire works right after title of story.
  7. // @author Vannius
  8. // @match https://archiveofourown.org/*
  9. // @exclude /^https:\/\/archiveofourown\.org\/(collections\/[^/]+\/)?works\/\d+/
  10. // @exclude /^https:\/\/archiveofourown\.org\/collections$/
  11. // @exclude /^https:\/\/archiveofourown\.org\/collections(\?.+)$/
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. // Main
  16. const articles = document.getElementsByClassName('blurb');
  17. for (let article of articles) {
  18. // Scrape each article
  19. const headerTag = article.getElementsByClassName('header module')[0];
  20. if (headerTag.className === "mystery header picture module") {
  21. continue;
  22. }
  23. const titleTag = headerTag.firstElementChild.firstElementChild;
  24. const series = titleTag.href.indexOf("/series/") !== -1;
  25.  
  26. // When article isn't series page
  27. if (!series) {
  28. // Get last chapter
  29. const lastChapter = article.querySelector('dl .chapters > a');
  30.  
  31. // When lastChapter is a link
  32. if (lastChapter) {
  33. // Get href
  34. const splitedHref = titleTag.href.split('/');
  35. const href = splitedHref[3] === 'collections'
  36. ? splitedHref.slice(0, 3).concat(splitedHref.slice(5)).join('/') : titleTag.href;
  37.  
  38. // Make link to entire contents
  39. const entireLink = document.createElement('a');
  40. entireLink.href = href + "?view_full_work=true";
  41. entireLink.title = "Entire Contents";
  42. entireLink.appendChild(document.createTextNode('E'));
  43.  
  44. // Make link button to last chapter.
  45. const lastLink = document.createElement('a');
  46. lastLink.href = lastChapter.href;
  47. lastLink.title = "Last Chapter";
  48. lastLink.appendChild(document.createTextNode('L'));
  49.  
  50. // Add link to entire contents and link button to last chapter right after title of story.
  51. const fragment = document.createDocumentFragment();
  52. fragment.appendChild(document.createTextNode(' '));
  53. fragment.appendChild(entireLink);
  54. fragment.appendChild(document.createTextNode(' '));
  55. fragment.appendChild(lastLink);
  56.  
  57. titleTag.parentNode.insertBefore(fragment, titleTag.nextSibling);
  58. }
  59. }
  60. }
  61. })();