Last active
March 25, 2019 22:03
-
-
Save briankung/06fe835bc73b321be9821fb349354dbf to your computer and use it in GitHub Desktop.
Bookmarklet to scroll through lobste.rs comments in order
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
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