Links to Next and Previous Posts

Add links to next and previous posts to each post

目前为 2020-08-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Links to Next and Previous Posts
  3. // @namespace https://greasyfork.org/en/users/163551-vannius
  4. // @version 1.70
  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. if (window.location.hostname == 'forum.questionablequesting.com') {
  33. const messageTags = document.querySelectorAll('li.message');
  34. const ids = [...messageTags].map(x => x.id);
  35.  
  36. for (let i = 0; i < messageTags.length; i++) {
  37. const ulTag = document.createElement('ul');
  38. ulTag.style.listStyle = 'none';
  39. ulTag.style.display = 'flex';
  40.  
  41. // Add a link to current post
  42. if (i === 0) {
  43. const currentPost = makeLink(ids[i], "Current post", '◇');
  44. ulTag.appendChild(currentPost);
  45. }
  46.  
  47. // Add a link to prev post
  48. if (i !== 0) {
  49. const prevPost = makeLink(ids[i - 1], "Prev post", '▲');
  50. ulTag.appendChild(prevPost);
  51. }
  52.  
  53. // Add a link to next post
  54. if (i !== messageTags.length - 1) {
  55. const nextPost = makeLink(ids[i + 1], "Next post", '▼');
  56. ulTag.appendChild(nextPost);
  57. }
  58.  
  59. // Add a link to current post
  60. if (i === messageTags.length - 1) {
  61. const currentPost = makeLink(ids[i], "Current post", '◇');
  62. ulTag.appendChild(currentPost);
  63. }
  64.  
  65. const userBlockTag = messageTags[i].querySelector('.userText');
  66. userBlockTag.appendChild(ulTag);
  67. }
  68. } else {
  69. // Get message ids from messageTags and make in hashLinks
  70. const messageTags = [...document.querySelectorAll('.message-attribution-opposite--list > li')]
  71. .filter(x => /^#[\d,]+$/.test(x.textContent.trim()));
  72. const ids = messageTags.map(x => x.closest('article').id);
  73.  
  74. for (let i = 0; i < messageTags.length; i++) {
  75. // Add a link to next post
  76. if (i !== messageTags.length - 1) {
  77. const nextPost = makeLink(ids[i + 1], "Next post", '▼');
  78. messageTags[i].parentElement.appendChild(nextPost);
  79. }
  80.  
  81. // Add a link to prev post
  82. if (i !== 0) {
  83. const prevPost = makeLink(ids[i - 1], "Prev post", '▲');
  84. messageTags[i].parentElement.insertBefore(prevPost, messageTags[i]);
  85. }
  86.  
  87. // Add a link to current post
  88. if (i === 0 || i === messageTags.length - 1) {
  89. const currentPost = makeLink(ids[i], "Current post", '◇');
  90. if (i === 0) {
  91. messageTags[i].parentElement.insertBefore(currentPost, messageTags[i]);
  92. } else if (i === messageTags.length - 1) {
  93. messageTags[i].parentElement.appendChild(currentPost);
  94. }
  95. }
  96. }
  97. }
  98. })();