Links to Next and Previous Posts

Add links to next and previous posts to each post

目前为 2019-01-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Links to Next and Previous Posts
  3. // @namespace https://greasyfork.org/en/users/163551-vannius
  4. // @version 1.4
  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('li .messageUserInfo');
  60. const hashLinks = Array.from(messageTags).map((element) =>
  61. window.location.origin + window.location.pathname + '#' + element.parentNode.id);
  62.  
  63. for (let i = 0; i < messageTags.length; i++) {
  64. const divTag = document.createElement('div');
  65. divTag.style.textAlign = 'right';
  66.  
  67. // Add a link to next post
  68. if (i !== messageTags.length - 1) {
  69. const nextPost = document.createElement('a');
  70. nextPost.title = "Next post";
  71. nextPost.href = hashLinks[i + 1];
  72. nextPost.appendChild(document.createTextNode('▼'));
  73. divTag.appendChild(nextPost);
  74. }
  75.  
  76. // Add a link to current post
  77. if (i === 0 || i === messageTags.length - 1) {
  78. const currentPost = document.createElement('a');
  79. currentPost.title = "Current post";
  80. currentPost.href = hashLinks[i];
  81. currentPost.appendChild(document.createTextNode('◈'));
  82. divTag.appendChild(currentPost);
  83. }
  84.  
  85. // Add a link to prev post
  86. if (i !== 0) {
  87. const prevPost = document.createElement('a');
  88. prevPost.title = "Prev post";
  89. prevPost.href = hashLinks[i - 1];
  90. prevPost.appendChild(document.createTextNode('▲'));
  91. divTag.appendChild(prevPost);
  92. }
  93.  
  94. messageTags[i].insertBefore(divTag, messageTags[i].children[0]);
  95. }
  96. }
  97. })();