Skip to content

Instantly share code, notes, and snippets.

@kaylathedev
Last active December 30, 2019 22:02
Show Gist options
  • Save kaylathedev/6e6f8f5f162eeb0c5a39be7f2d4a620d to your computer and use it in GitHub Desktop.
Save kaylathedev/6e6f8f5f162eeb0c5a39be7f2d4a620d to your computer and use it in GitHub Desktop.
javascript:console.log("Version 0.4.4 of Reddit Auto Hider");var str=window.localStorage.getItem("posts_hidden");function startHidingPosts(){pauseHidingPosts(),window.post_hider_interval=setInterval(tick_postHide,200)}function pauseHidingPosts(){window.post_hider_interval&&(clearInterval(window.post_hider_interval),window.post_hider_interval=void 0)}function forgetAllHiddenPosts(){pauseHidingPosts(),window.posts={},window.localStorage.removeItem("posts_hidden"),location.reload()}function tick_postHide(){for(var e=new Date,i=document.getElementsByClassName("rpBJOHq2PR60pnwJlUyP0")[0].children,t=0;t<i.length;t++){var n=i[t];if(""!==n.innerText){if(n.children){var o=n.children[0];if(o){if(!o.children)continue;if(o=o.children[0]){var s=o.id;if(/^t3_[a-zA-Z0-9]+$/g.test(s)){var d=window.posts[s];if(d||(d={id:s,seen:!1,hidden:!1},window.posts[s]=d),d.hidden){n.style.display="none";continue}void 0!==d.seen_end_time&&(d.seen_end_time=new Date(d.seen_end_time));var l=edgesVisible(n);if(d.seen?l.middle||void 0!==d.seen_end_time||(d.seen_end_time=new Date):l.middle&&(d.seen=!0),!l.middle&&d.seen&&d.seen_end_time&&d.seen_end_time.getTime()+5e3<e.getTime()){d.hidden=!0,n.style.display="none",console.log("hidden post");continue}}else n.parentElement.removeChild(n),o.classList.contains("scrollerItem")}}}}else n.parentElement.removeChild(n)}window.localStorage.setItem("posts_hidden",JSON.stringify(window.posts))}function edgesVisible(e){var i=window.visualViewport,t=e.getBoundingClientRect();return{top:0<t.y&&t.top<i.height,bottom:0<t.y&&t.bottom<i.height,left:0<t.x&&t.left<i.width,right:0<t.x&&t.right<i.width,middle:!(t.bottom<0||t.top>i.height)}}window.posts=str?JSON.parse(str):{},startHidingPosts();
// ==UserScript==
// @name Reddit Auto Hider
// @namespace https://kaylathedev.com/
// @version 0.4.4
// @date 2019-12-30
// @author Kayla Colflesh
// @description Automatically hides Reddit posts that have already been seen.
// @homepage https://gist.github.com/kaylathedev/6e6f8f5f162eeb0c5a39be7f2d4a620d
// @match https://www.reddit.com/*
// @grant none
// @updateURL https://gist.githubusercontent.com/kaylathedev/6e6f8f5f162eeb0c5a39be7f2d4a620d/raw/reddit_auto_hider.js
// @downloadURL https://gist.githubusercontent.com/kaylathedev/6e6f8f5f162eeb0c5a39be7f2d4a620d/raw/reddit_auto_hider.js
// ==/UserScript==
console.log('Version 0.4.4 of Reddit Auto Hider');
var str = window.localStorage.getItem('posts_hidden');
if (!str) {
window.posts = {};
} else {
window.posts = JSON.parse(str);
}
startHidingPosts();
function startHidingPosts() {
pauseHidingPosts();
window.post_hider_interval = setInterval(tick_postHide, 200);
}
function pauseHidingPosts() {
if (window.post_hider_interval) {
clearInterval(window.post_hider_interval);
window.post_hider_interval = undefined;
}
}
function forgetAllHiddenPosts() {
pauseHidingPosts();
window.posts = {};
window.localStorage.removeItem('posts_hidden');
location.reload();
}
function tick_postHide() {
var now = new Date();
var elements = document.getElementsByClassName('rpBJOHq2PR60pnwJlUyP0')[0].children;
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.innerText === '') {
element.parentElement.removeChild(element);
continue;
}
if (!element.children) continue;
var child = element.children[0];
if (child) {
if (!child.children) continue;
child = child.children[0];
if (child) {
var id = child.id;
if (/^t3_[a-zA-Z0-9]+$/g.test(id)) {
var post_info = window.posts[id];
if (post_info) {
// no-op
} else {
post_info = {
id: id,
seen: false,
hidden: false,
};
window.posts[id] = post_info;
}
if (post_info.hidden) {
element.style.display = 'none';
continue;
}
// convert date strings into dates if needed
if (post_info.seen_end_time !== undefined) {
post_info.seen_end_time = new Date(post_info.seen_end_time);
}
var edges = edgesVisible(element);
// If post has been viewed already, but is no longer visible, then set the timeout.
if (post_info.seen) {
if (!edges.middle && post_info.seen_end_time === undefined) {
post_info.seen_end_time = new Date();
}
} else {
if (edges.middle) {
post_info.seen = true;
}
}
// Post is already viewed, and has been out of sight for 5 seconds.
// Post will only be hidden if it's not visible again.
if (!edges.middle &&
post_info.seen &&
post_info.seen_end_time &&
post_info.seen_end_time.getTime() + (1000 * 5) < now.getTime()
) {
post_info.hidden = true;
element.style.display = 'none';
console.log('hidden post');
continue;
}
} else {
element.parentElement.removeChild(element);
if (child.classList.contains('scrollerItem')) {
//element.parentElement.removeChild(element);
}
// not a post, might be blank
}
}
}
}
window.localStorage.setItem('posts_hidden', JSON.stringify(window.posts));
}
function edgesVisible(element) {
var viewport = window.visualViewport;
var bounds = element.getBoundingClientRect();
return {
top: bounds.y > 0 && bounds.top < viewport.height,
bottom: bounds.y > 0 && bounds.bottom < viewport.height,
left: bounds.x > 0 && bounds.left < viewport.width,
right: bounds.x > 0 && bounds.right < viewport.width,
middle: !(bounds.bottom < 0 || bounds.top > viewport.height),
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment