Reddit Post Hider

Provides an option to hide visited Reddit Posts. Supoports RES. Classic theme only

Click here to install Browse More Scripts
// ==UserScript==
// @name         Reddit Post Hider
// @namespace    58aa7af8b5e105fb9100415644e551a77f848a1d
// @version      0.9
// @description  Provides an option to hide visited Reddit Posts. Supoports RES. Classic theme only
// @author       /u/AyrA_ch
// @match        https://*.reddit.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

// Version history
// 0.9 - Changed "action" to "data-event-action"
// 0.8 - Remove key press delay
// 0.7 - Allow ALT+DEL to quickly delete past posts
// 0.6 - New RES update
// 0.5 - Remove unnecessary logging
// 0.4 - Switch to CTRL instead of SHIFT to mass hide items
// 0.3 - Enable mass hiding of items with SHIFT
// 0.2 - Scroll up on removal of items to prevent the next page from triggering
// 0.1 - Initial Version with RES Support

(function ($, $$) {
	"use strict";
	//Hides visited links
	var hideVisited = function (hideAll) {
		if (hideAll) {
			var stop = hideAll === true ? $$(".res-selected")[0] : hideAll;
			if (stop) {
				var ee = $$(".thing");
				if (ee.length > 0) {
					window.scrollTo(0, 0);
					for (var i = 0; i < ee.length && ee[i] !== stop; i++) {
						var lnk = ee[i].querySelector("a[data-event-action=hide]");
						if (lnk) {
							lnk.click();
						}
					}
				} else {
					console.log("No elements before", ee);
				}
				stop.querySelector("a[data-event-action=hide]").click();
			} else {
				alert("Select the item from which you want all previous elements to be cleared (RES only)");
			}
		} else {
			var ee = $$(".visited a[data-event-action=hide],:visited a[data-event-action=hide]");
			if (ee.length > 0) {
				window.scrollTo(0, 0);
				//Reddit actually collects the IDs and makes only one request,
				//so we are safe to click as many links in a batch as we want
				ee.forEach(function (v) {
					v.click();
				});
			} else {
				alert("No elements found to hide. CTRL + Click (or ALT+DEL) to hide all elements up to the selected one.");
			}
		}
	}

	//Adds the hide options to RES and the reddit menu
	var addOption = function () {
		//RES specific setup
		var RES = $(".res-floater-list");
		if (RES) {
			if (!$("#RESHideAll")) {
				var e = document.createElement("li");
				e.id = "RESHideAll";
				e.textContent = '❌';
				e.title = "Hide all read posts and restart from top";
				e.setAttribute("style", "cursor:pointer;color:#F00;");
				e.onclick = function (e) {
					e.preventDefault();
					hideVisited(e.ctrlKey || e.ctrl);
				};
				RES.appendChild(e);
				console.log("RES Hide option added");
			}
		} else {
			//RES not loaded yet. Don't try for longer than 10 seconds though
			if (addOption.count++ < 10) {
				setTimeout(addOption, 1000);
			}
		}

		//Classic reddit setup
		if (!$("#hideallposts") && $("a#mail")) {
			var spacer = $("a#mail").previousSibling;
			var newSpacer = spacer.cloneNode();
			spacer.parentNode.insertBefore(newSpacer, spacer);

			var a = document.createElement("a");
			a.id = "hideallposts";
			a.textContent = '❌';
			a.href = '#';
			a.title = "Hide all read posts and restart from top";
			a.setAttribute("style", "color:#F00;");
			a.onclick = function (e) {
				e.preventDefault();
				hideVisited(e.ctrlKey || e.ctrl);
			};
			spacer.parentNode.insertBefore(a, spacer);
			console.log("Header Hide option added");
		}
	}
	addOption.count = 0;
	addOption();

	//Ability to delete using ALT+DEL (Hold for 1 second)
	var reset = false;
	document.addEventListener("keydown", function (e) {
		//Listen for ALT+DEL
		if (!reset && e.altKey && e.keyCode === 46) {
			reset = $$(".res-selected")[0];
			if (reset) {
				window.scrollTo(0, 0);
				e.preventDefault();
			}
		}
	});
	document.addEventListener("keyup", function (e) {
		if (reset && e.keyCode === 46) {
			hideVisited(reset);
			reset = false;
		}
	});
})(document.querySelector.bind(document), document.querySelectorAll.bind(document));

/*
LICENSE:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
The full license text can be found here: http://creativecommons.org/licenses/by-nc-sa/4.0/
The link has an easy to understand version of the license and the full license text.

DISCLAIMER:
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/

Copyright © 2018 by Kevin Gut 📧 | More services | Generated for 98.80.143.34