Skip to content

Instantly share code, notes, and snippets.

@briankung
Last active March 25, 2019 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briankung/06fe835bc73b321be9821fb349354dbf to your computer and use it in GitHub Desktop.
Save briankung/06fe835bc73b321be9821fb349354dbf to your computer and use it in GitHub Desktop.
Bookmarklet to scroll through lobste.rs comments in order
javascript:void(
(function() {
this.distanceFromTop = (a, b) => {
return Math.sign(a.getBoundingClientRect().top - b.getBoundingClientRect().top);
};
this.setUnreadElements = () => {
document.unreadElements = [...document.querySelectorAll('[class*=unread]')].sort(this.distanceFromTop);
};
this.parentComment = (el) => {
return el.closest('.comment');
};
this.focusElement = (el) => {
document.oldBackgroundColor = el.style.backgroundColor;
el.style.backgroundColor = "#FDFF47";
el.scrollIntoView({ block: 'center', behavior: 'smooth' });
};
this.unFocusElement = (el) => {
el.style.backgroundColor = document.oldBackgroundColor;
};
if (document.unreadElements === undefined) {
this.setUnreadElements();
} else if (document.unreadElements.length == 0) {
return;
};
this.unFocusElement(this.parentComment(document.unreadElements[document.unreadElements.length - 1]));
this.focusElement(this.parentComment(document.unreadElements[0]));
document.unreadElements.push(document.unreadElements.shift());
// Just reverse everything to go backwards:
// document.unreadElements.unshift(document.unreadElements.pop());
// this.unFocusElement(this.parentComment(document.unreadElements[0]));
// this.focusElement(this.parentComment(document.unreadElements[document.unreadElements.length - 1]));
})()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment