-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |