feat: reinstate 'collapse next comment' bindings

reimplements functionality to collapse the next visible comment on
Reddit/HN without the jQuery dependency
This commit is contained in:
Maddison Hellstrom 2018-08-28 13:11:37 -07:00
parent 0a9fab729f
commit 047cc75e89
2 changed files with 31 additions and 4 deletions

23
conf.js
View File

@ -1,4 +1,5 @@
const completions = require("./completions") const completions = require("./completions")
const { isElementInViewport } = require("./util")
// Unmap undesired defaults // Unmap undesired defaults
const unmaps = [ const unmaps = [
@ -260,10 +261,17 @@ mapsitekeys("twitter.com", [
["g", "Goto user", Hint(".js-user-profile-link")], ["g", "Goto user", Hint(".js-user-profile-link")],
]) ])
const redditCollapseNextComment = () => {
const vis = Array.from(document.querySelectorAll(".noncollapsed.comment"))
.filter(e => isElementInViewport(e))
if (vis.length > 0) {
vis[0].querySelector(".expand").click()
}
}
mapsitekeys("reddit.com", [ mapsitekeys("reddit.com", [
["x", "Collapse comment", Hint(".expand")], ["x", "Collapse comment", Hint(".expand")],
// Not supported by the QuerySelector API ["X", "Collapse next comment", redditCollapseNextComment],
// ["X", "Collapse next comment", Hint(".expand:visible:not(:contains('[+]')):nth(0)")],
["s", "Upvote", Hint(".arrow.up")], ["s", "Upvote", Hint(".arrow.up")],
["S", "Downvote", Hint(".arrow.down")], ["S", "Downvote", Hint(".arrow.down")],
["e", "Expand expando", Hint(".expando-button")], ["e", "Expand expando", Hint(".expando-button")],
@ -279,10 +287,17 @@ const hnGoParent = () => {
window.location.assign(par.href) window.location.assign(par.href)
} }
const hnCollapseNextComment = () => {
const vis = Array.from(document.querySelectorAll("a.togg"))
.filter(e => e.innerText === "[-]" && isElementInViewport(e))
if (vis.length > 0) {
vis[0].click()
}
}
mapsitekeys("news.ycombinator.com", [ mapsitekeys("news.ycombinator.com", [
["x", "Collapse comment", Hint(".togg")], ["x", "Collapse comment", Hint(".togg")],
// Not supported by the QuerySelector API ["X", "Collapse next comment", hnCollapseNextComment],
// ["X", "Collapse next comment", Hint(".togg:visible:contains('[-]'):nth(0)")],
["s", "Upvote", Hint(".votearrow[title='upvote']")], ["s", "Upvote", Hint(".votearrow[title='upvote']")],
["S", "Downvote", Hint(".votearrow[title='downvote']")], ["S", "Downvote", Hint(".votearrow[title='downvote']")],
["a", "View post (link)", Hint(".storylink")], ["a", "View post (link)", Hint(".storylink")],

12
util.js Normal file
View File

@ -0,0 +1,12 @@
const isRectVisibleInViewport = rect => (
rect.height > 0 &&
rect.width > 0 &&
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
)
const isElementInViewport = e => isRectVisibleInViewport(e.getBoundingClientRect())
module.exports = { isRectVisibleInViewport, isElementInViewport }