Reddit: Hide/Show NSFW posts

Adds dropdown list, allowing to select prefferable way of watching Reddit: Show All Posts, Hide NSFW Posts or Show Only NSFW Posts.

As of 2019-06-12. See the latest version.

  1. // ==UserScript==
  2. // @name Reddit: Hide/Show NSFW posts
  3. // @description Adds dropdown list, allowing to select prefferable way of watching Reddit: Show All Posts, Hide NSFW Posts or Show Only NSFW Posts.
  4. // @include http://*.reddit.com/*
  5. // @include https://*.reddit.com/*
  6. // @version 1.1
  7. // @require http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js
  8. // @run-at document-end
  9. // @author Delmor_S
  10. // @grant none
  11. // @namespace https://greasyfork.org/users/4638
  12. // ==/UserScript==
  13.  
  14. function showAllPosts() {
  15.  
  16. $('div[class*="Post"]').each(function() {
  17.  
  18. $(this).css('display', 'block');
  19. });
  20.  
  21. }
  22.  
  23. function hideNSFWPosts() {
  24.  
  25. $('div[class*="Post"]').each(function() {
  26. if ($(this).text().match('nsfw')) {
  27. $(this).css('display', 'none');
  28. }
  29. });
  30.  
  31. }
  32.  
  33. function showOnlyNSFWposts() {
  34.  
  35. $('div[class*="Post"]').each(function() {
  36. if ($(this).text().match('nsfw')) {
  37. $(this).css('display', 'block');
  38. } else {
  39. $(this).css('display', 'none');
  40. }
  41. });
  42.  
  43. }
  44.  
  45.  
  46.  
  47. function createEl(elementName, id /*optional*/ , attrArr /*optional*/ , parentEl /*optional*/ ) {
  48.  
  49. var el = document.createElement(elementName);
  50. if (id) {
  51. el.id = id;
  52. }
  53. if (attrArr) {
  54. for (var attr in attrArr) {
  55. el.setAttribute(attr, attrArr[attr]);
  56. }
  57. }
  58. if (parentEl) {
  59. parentEl.appendChild(el);
  60. }
  61. return el;
  62. }
  63.  
  64. function createText(txt) {
  65.  
  66. return document.createTextNode(txt);
  67. }
  68.  
  69. function appendCSS(obj) {
  70.  
  71. var cssString = "",
  72. propString = "",
  73. eachSelector = "",
  74. style = createEl("style");
  75. for (var selector in CSS) {
  76. eachSelector = CSS[selector];
  77. propString = "";
  78. for (var property in eachSelector) {
  79. propString += property + ":" + eachSelector[property] + ";";
  80. }
  81. cssString += selector + "{" + propString + "}";
  82. }
  83. style.appendChild(createText(cssString));
  84. document.head.appendChild(style);
  85. }
  86.  
  87.  
  88.  
  89.  
  90. var nsfwSelect = document.createElement('select');
  91. nsfwSelect.id = 'nsfwSelect';
  92. document.body.appendChild(nsfwSelect);
  93.  
  94.  
  95. var nsfwShowAll = document.createElement('option');
  96. nsfwShowAll.value = 'Show All Posts';
  97. nsfwShowAll.innerHTML = 'Show All Posts';
  98. nsfwSelect.appendChild(nsfwShowAll);
  99.  
  100. var nsfwHideNSFW = document.createElement('option');
  101. nsfwHideNSFW.value = 'Hide NSFW Posts';
  102. nsfwHideNSFW.innerHTML = 'Hide NSFW Posts';
  103. nsfwSelect.appendChild(nsfwHideNSFW);
  104.  
  105. var nsfwShowOnlyNSFW = document.createElement('option');
  106. nsfwShowOnlyNSFW.value = 'Show Only NSFW Posts';
  107. nsfwShowOnlyNSFW.innerHTML = 'Show Only NSFW Posts';
  108. nsfwSelect.appendChild(nsfwShowOnlyNSFW);
  109.  
  110.  
  111.  
  112.  
  113. var timer = setInterval(
  114. function() {
  115.  
  116. if (nsfwSelect.options[nsfwSelect.selectedIndex].text == "Hide NSFW Posts") {
  117. hideNSFWPosts();
  118. } else if (nsfwSelect.options[nsfwSelect.selectedIndex].text == "Show Only NSFW Posts") {
  119.  
  120. showOnlyNSFWposts();
  121. } else {
  122. showAllPosts();
  123. }
  124. },
  125. 1000
  126.  
  127. );
  128.  
  129.  
  130.  
  131. var CSS = {
  132. '#nsfwSelect': {
  133. 'position': 'fixed',
  134. 'bottom': '20px',
  135. 'left': '3px',
  136. 'z-index': '999'
  137. }
  138. };
  139.  
  140.  
  141.  
  142. appendCSS(CSS);