Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
glebov21 authored Oct 22, 2024
1 parent 2c22701 commit f241ea3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"manifest_version": 2,
"name": "RedditViewsHidder",
"version": "1.0",

"description": "Hide viewed reddit posts",

"icons": {
"48": "icon.png"
},

"content_scripts": [
{
"matches": ["*://*.reddit.com/*"],
"js": ["redditviewshidder.js"]
}
]
}

71 changes: 71 additions & 0 deletions redditviewshidder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let maxStoreCount = 10000;
let articlesContainer = document.getElementsByTagName('shreddit-feed')[0];
let localStorageSaveKey = "viewedRedditPosts";
let viewedPostsById = new Set();

function LoadSavedViews(){
let savedJson = localStorage.getItem(localStorageSaveKey);
if(savedJson){
const arrFromJson = JSON.parse(savedJson);
viewedPostsById = new Set(arrFromJson);
//trim size
if(viewedPostsById.size > maxStoreCount){
let arrToTrim = Array.from(viewedPostsById);
arrToTrim.splice(0, viewedPostsById.size - maxStoreCount)
viewedPostsById = new Set(arrToTrim);
}
}
}

function SaveViews(){
localStorage.setItem(localStorageSaveKey, JSON.stringify(Array.from(viewedPostsById)));
}

function ForeachPosts(callback){
let articles = articlesContainer.getElementsByTagName('article');
for(let article of articles){
let post = article.getElementsByTagName('shreddit-post')[0];
callback(article, post, post.id);
}
}

function RemoveViewedPosts(){
ForeachPosts((articleEl, postEl, postId)=>{
if(viewedPostsById.has(postId))
{
articleEl.nextSibling.remove(); //remove hr
articleEl.remove();
}
});
}

//START

LoadSavedViews();

//Hide viewedPosts
let newpostObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.addedNodes.length > 0){
RemoveViewedPosts();
}
});
});

newpostObserver.observe(articlesContainer, {childList: true, subtree: true, characterData: false});

RemoveViewedPosts();

//Catch viewed posts
addEventListener("scroll", (event) => {
ForeachPosts((articleEl, postEl, postId)=>{
let bounds = articleEl.getBoundingClientRect();
if(bounds.bottom < 0) {
viewedPostsById.add(postId);
}
});
});

addEventListener("beforeunload", (event) => {
SaveViews();
});

0 comments on commit f241ea3

Please sign in to comment.