Links to Next and Previous Posts

Add links to next and previous posts to each post

  1. // ==UserScript==
  2. // @name Links to Next and Previous Posts
  3. // @namespace https://greasyfork.org/en/users/163551-vannius
  4. // @version 1.80
  5. // @license MIT
  6. // @description Add links to next and previous posts to each post
  7. // @author Vannius
  8. // @match https://www.alternatehistory.com/forum/threads/*
  9. // @match https://forums.spacebattles.com/threads/*
  10. // @match https://forums.sufficientvelocity.com/threads/*
  11. // @match https://forum.questionablequesting.com/threads/*
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. // Make <li> tag with new link.
  17. const makeLink = (id, title, symbol) => {
  18. const newLink = document.createElement('a');
  19. newLink.href = '#' + id;
  20. newLink.title = title;
  21. newLink.appendChild(document.createTextNode(symbol));
  22. newLink.addEventListener('click', function(e) {
  23. e.preventDefault();
  24. document.getElementById(id).scrollIntoView();
  25. }, false);
  26.  
  27. const liTag = document.createElement('li');
  28. liTag.appendChild(newLink);
  29. return liTag;
  30. }
  31.  
  32. // Get message ids from messageTags and make in hashLinks
  33. const messageTags = [...document.querySelectorAll('.message-attribution-opposite--list > li')]
  34. .filter(x => /^#[\d,]+$/.test(x.textContent.trim()));
  35. const ids = messageTags.map(x => x.closest('article').id);
  36.  
  37. for (let i = 0; i < messageTags.length; i++) {
  38. // Add a link to next post
  39. if (i !== messageTags.length - 1) {
  40. const nextPost = makeLink(ids[i + 1], "Next post", '▼');
  41. messageTags[i].parentElement.appendChild(nextPost);
  42. }
  43.  
  44. // Add a link to prev post
  45. if (i !== 0) {
  46. const prevPost = makeLink(ids[i - 1], "Prev post", '▲');
  47. messageTags[i].parentElement.insertBefore(prevPost, messageTags[i]);
  48. }
  49.  
  50. // Add a link to current post
  51. if (i === 0 || i === messageTags.length - 1) {
  52. const currentPost = makeLink(ids[i], "Current post", '◇');
  53. if (i === 0) {
  54. messageTags[i].parentElement.insertBefore(currentPost, messageTags[i]);
  55. } else if (i === messageTags.length - 1) {
  56. messageTags[i].parentElement.appendChild(currentPost);
  57. }
  58. }
  59. }
  60. })();