Links to Next and Previous Posts

Add links to next and previous posts to each post

目前为 2019-11-23 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Links to Next and Previous Posts
  3. // @namespace https://greasyfork.org/en/users/163551-vannius
  4. // @version 1.5
  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. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. if (window.location.host === "www.alternatehistory.com") {
  15. // Get hash links from divTags and store in hashLinks
  16. const divTags = document.getElementsByClassName('messageBarNumber');
  17. const hashLinks = Array.from(divTags).map((element) => element.children[0].href);
  18. // Very first post of thread's divTag.children[0].href don't have #.
  19. if (window.location.href.split('/')[6] === '') hashLinks[0] += '#' + divTags[0].parentNode.parentNode.id;
  20.  
  21. for (let i = 0; i < divTags.length; i++) {
  22. // Add a link to next post
  23. if (i !== divTags.length - 1) {
  24. const nextPost = document.createElement('a');
  25. nextPost.title = "Next post";
  26. nextPost.href = hashLinks[i + 1];
  27. nextPost.appendChild(document.createTextNode('▼'));
  28. divTags[i].appendChild(nextPost);
  29. }
  30. // Add a link to prev post
  31. if (i !== 0) {
  32. const prevPost = document.createElement('a');
  33. prevPost.title = "Prev post";
  34. prevPost.href = hashLinks[i - 1];
  35. prevPost.appendChild(document.createTextNode('▲'));
  36. const fragment = document.createDocumentFragment();
  37. fragment.appendChild(prevPost);
  38. fragment.appendChild(document.createTextNode('\n'));
  39. divTags[i].insertBefore(fragment, divTags[i].children[0]);
  40. }
  41. // Add a link to current post
  42. if (i === 0 || i === divTags.length - 1) {
  43. const currentPost = document.createElement('a');
  44. currentPost.title = "Current post";
  45. currentPost.href = hashLinks[i];
  46. currentPost.appendChild(document.createTextNode('◈'));
  47. if (i === 0) {
  48. const fragment = document.createDocumentFragment();
  49. fragment.appendChild(currentPost);
  50. fragment.appendChild(document.createTextNode('\n'));
  51. divTags[i].insertBefore(fragment, divTags[i].children[0]);
  52. } else if (i === divTags.length - 1) {
  53. divTags[i].appendChild(currentPost);
  54. }
  55. }
  56. }
  57. } else if (window.location.host === "forums.spacebattles.com") {
  58. // Get message ids from messageTags and make in hashLinks
  59. const messageTags = document.querySelectorAll('.message-attribution-opposite--list > li:nth-child(3)');
  60. const hashLinks = Array.from(messageTags).map(element => '#' + element.closest('article').id);
  61.  
  62. for (let i = 0; i < messageTags.length; i++) {
  63. // Add a link to next post
  64. if (i !== messageTags.length - 1) {
  65. const nextPost = document.createElement('a');
  66. nextPost.title = "Next post";
  67. nextPost.href = hashLinks[i + 1];
  68. nextPost.appendChild(document.createTextNode('▼'));
  69. const liTag = document.createElement('li');
  70. liTag.appendChild(nextPost);
  71. messageTags[i].parentElement.appendChild(liTag);
  72. }
  73.  
  74. // Add a link to prev post
  75. if (i !== 0) {
  76. const prevPost = document.createElement('a');
  77. prevPost.title = "Prev post";
  78. prevPost.href = hashLinks[i - 1];
  79. prevPost.appendChild(document.createTextNode('▲'));
  80. const liTag = document.createElement('li');
  81. liTag.appendChild(prevPost);
  82. messageTags[i].parentElement.insertBefore(liTag, messageTags[i]);
  83. }
  84.  
  85. // Add a link to current post
  86. if (i === 0 || i === messageTags.length - 1) {
  87. const currentPost = document.createElement('a');
  88. currentPost.title = "Current post";
  89. currentPost.href = hashLinks[i];
  90. currentPost.appendChild(document.createTextNode('◈'));
  91. const liTag = document.createElement('li');
  92. liTag.appendChild(currentPost);
  93. if (i === 0) {
  94. messageTags[i].parentElement.insertBefore(liTag, messageTags[i]);
  95. } else if (i === messageTags.length - 1) {
  96. messageTags[i].parentElement.appendChild(liTag);
  97. }
  98. }
  99. }
  100. }
  101. })();