Tumblr notes filtering, autoloading & counts

Allows filtering of Tumblr notes by type (with counts) & autoloading of all notes

  1. // ==UserScript==
  2. // @name Tumblr notes filtering, autoloading & counts
  3. // @include http://*tumblr.com/post/*
  4. // @description Allows filtering of Tumblr notes by type (with counts) & autoloading of all notes
  5. // @version 1.3
  6. // @author JesseW
  7. // @contributor wOxxOm
  8. // @license MIT License
  9. // @run-at document-start
  10. // @grant none
  11. // @namespace https://greasyfork.org/users/2423
  12. // ==/UserScript==
  13.  
  14. // The original code for autoloading came from wOxxOm's script,
  15. //available at: https://greasyfork.org/en/scripts/8157-tumblr-notes-autoload
  16.  
  17. window.addEventListener('DOMContentLoaded', function () {
  18. filterWhenLoadingMoreNotes();
  19. document.querySelector('.notes') .insertAdjacentHTML(
  20. 'beforebegin',
  21. '<div style="border: 2px black ridge">'
  22. + '<input checked id="with_commentary_checkbox" type="checkbox"/>Reblogs with commentary (<span id="with_commentary_count"></span>)'
  23. + '<br/><input checked id="reply_checkbox" type="checkbox"/>Replies (<span id="reply_count"></span>)'
  24. + '<br/><input checked id="reblog_checkbox" type="checkbox"/>Reblogs without commentary (<span id="reblog_count"></span>)'
  25. + '<br/><input checked id="like_checkbox" type="checkbox">Likes (<span id="like_count"></span>)'
  26. + '<br/><button id="loadallBtn">Load all notes</button></div>')
  27. document.getElementById('loadallBtn').addEventListener('click', clickMore);
  28. document.getElementById('with_commentary_checkbox').addEventListener('change', filterNotes);
  29. document.getElementById('reply_checkbox').addEventListener('change', filterNotes);
  30. document.getElementById('reblog_checkbox').addEventListener('change', filterNotes);
  31. document.getElementById('like_checkbox').addEventListener('change', filterNotes);
  32. });
  33.  
  34. function filterWhenLoadingMoreNotes() {
  35. var more = document.querySelector('.more_notes_link');
  36. more.addEventListener('click', filterWhenLoadingMoreNotes);
  37. if (more) {
  38. var ob = new MutationObserver(function (mutations) {
  39. ob.disconnect();
  40. filterNotes();
  41. filterWhenLoadingMoreNotes()
  42. });
  43. ob.observe(more.parentNode.parentNode, {
  44. subtree: true,
  45. childList: true
  46. });
  47. }
  48. }
  49.  
  50. function shouldFilter(note) {
  51. if (note.classList.contains('more_notes_link_container') ||
  52. (document.getElementById('with_commentary_checkbox').checked &&
  53. note.classList.contains('with_commentary')) ||
  54. (document.getElementById('reply_checkbox').checked &&
  55. note.classList.contains('reply')) ||
  56. (document.getElementById('reblog_checkbox').checked &&
  57. note.classList.contains('reblog') &&
  58. note.classList.contains('without_commentary')) ||
  59. (document.getElementById('like_checkbox').checked &&
  60. note.classList.contains('like'))) {
  61. return false;
  62. }
  63. return true;
  64. }
  65.  
  66. function filterNotes() {
  67. var notes = document.querySelectorAll('.note');
  68. var counts = {'commentary': 0, 'reply': 0, 'reblog': 0, 'like': 0};
  69. for (var i = 0; i < notes.length; ++i) {
  70. notes[i].hidden = shouldFilter(notes[i]);
  71. if (notes[i].classList.contains('with_commentary')) {
  72. counts['commentary'] += 1
  73. } else if (notes[i].classList.contains('reply')) {
  74. counts['reply'] += 1
  75. } else if (notes[i].classList.contains('reblog') &&
  76. notes[i].classList.contains('without_commentary')) {
  77. counts['reblog'] += 1
  78. } else if (notes[i].classList.contains('like')) {
  79. counts['like'] += 1
  80. }
  81. }
  82. document.getElementById('with_commentary_count').textContent = counts['commentary'];
  83. document.getElementById('reply_count').textContent = counts['reply'];
  84. document.getElementById('reblog_count').textContent = counts['reblog'];
  85. document.getElementById('like_count').textContent = counts['like'];
  86. }
  87.  
  88. function clickMore() {
  89. filterNotes();
  90. var more = document.querySelector('.more_notes_link');
  91. if (more) {
  92. more.click();
  93. var ob = new MutationObserver(function (mutations) {
  94. ob.disconnect();
  95. setTimeout(function () {
  96. clickMore()
  97. }, 200);
  98. });
  99. ob.observe(more.parentNode.parentNode, {
  100. subtree: true,
  101. childList: true
  102. });
  103. }
  104. }