Forked from fgrsnau/Reddit_Comment_Highlighter.user.js
Created
April 8, 2020 19:48
-
-
Save btamayo/414c268e573948b820831f302da21892 to your computer and use it in GitHub Desktop.
Reddit Comment Highlighter
This file contains hidden or 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
// ==UserScript== | |
// @name Reddit Comment Highlighter | |
// @namespace https://gist.github.com/fgrsnau | |
// @include http://www.reddit.com/r/*/comments/* | |
// @include https://www.reddit.com/r/*/comments/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
var id = $('.content .sitetable .thing:first').attr('data-fullname'); | |
var cookie = 'lastvisit_' + id; | |
var btnReset = $('<a href="#" class="title-button"></a>'); | |
var btnAdjust = $('<a href="#" class="title-button">(adjust last visit)</a>'); | |
var dateOfLoad = new Date(); | |
$('.panestack-title').append(btnReset, btnAdjust); | |
function highlight(date) { | |
console.log('Highlighting comments newer than ' + date + '.'); | |
var filterFunc = function() { | |
str = $('.live-timestamp', this).attr('datetime'); | |
d = new Date(str); | |
return d > date; | |
}; | |
var entries = $('.commentarea .entry'); | |
entries.not(filterFunc).removeClass('new-comment'); | |
entries = entries.filter(filterFunc); | |
entries.addClass('new-comment'); | |
btnReset.text(entries.length + ' new comments!'); | |
} | |
function load() { | |
var lastVisit = parseInt(localStorage.getItem(cookie)); | |
if (isNaN(lastVisit)) { | |
lastVisit = 0; | |
} | |
return new Date(lastVisit); | |
} | |
function store(date) { | |
localStorage.setItem(cookie, date.valueOf()); | |
} | |
btnReset.on('click', function(event) { | |
event.preventDefault(); | |
store(dateOfLoad); | |
highlight(dateOfLoad); | |
}); | |
btnAdjust.on('click', function(event) { | |
event.preventDefault(); | |
var str = prompt('Last visit was [xx] hours ago?', '1'); | |
var h = parseInt(str); | |
if (! isNaN(h)) { | |
var d = new Date(dateOfLoad - h * 60 * 60 * 1000); | |
store(d); | |
highlight(d); | |
} | |
}); | |
highlight(load()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment