2 const comments
= document
.querySelectorAll("tr.comtr");
3 const items
= document
.querySelectorAll("table.itemlist tr.athing");
4 const morelink
= document
.querySelectorAll("a.morelink");
8 ...Array
.from(comments
),
9 ...Array
.from(morelink
),
12 let elementsIndex
= 0;
13 let selectedElement
= elements
[elementsIndex
];
14 selectedElement
.style
.outline
= '1px dashed black';
16 const visible
= (element
) => {
17 let bounds
= element
.getBoundingClientRect();
18 return Math
.abs(bounds
.bottom
- bounds
.top
) > window
.innerHeight
|| bounds
.top
>= 0 && bounds
.bottom
<= window
.innerHeight
;
21 const changeWithVisibleCallback
= (comment
, callback
) => {
22 if (comment
== null) {
25 selectedElement
.style
.outline
= '';
26 selectedElement
= comment
;
27 selectedElement
.style
.outline
= '1px dashed black';
28 while (!visible(comment
)) {
33 const change
= (comment
) => {
34 elementsIndex
= elements
.indexOf(comment
);
35 changeWithVisibleCallback(comment
, () => {});
38 // Curry callback for moving downpage
39 const changeDownpage
= () => {
40 elementsIndex
= Math
.min(elementsIndex
+ 1, elements
.length
- 1);
41 changeWithVisibleCallback(elements
[elementsIndex
], () => {window
.scrollTo(0, window
.scrollY
+ selectedElement
.offsetHeight
)});
44 // Curry callback for moving up page
45 const changeUppage
= () => {
46 elementsIndex
= Math
.max(elementsIndex
- 1, 0);
47 changeWithVisibleCallback(elements
[elementsIndex
], () => {window
.scrollTo(0, window
.scrollY
- selectedElement
.offsetHeight
)});
50 document
.addEventListener("click", (e
) => {
51 change(e
.target
.closest("tr.athing"));
53 document
.addEventListener("keydown", (e
) => {
54 if (e
.target
.type
=== 'textarea' || e
.isComposing
) {
61 } while (selectedElement
.classList
.contains("noshow"));
66 } while (selectedElement
.classList
.contains("noshow"));
70 let togg
= selectedElement
.querySelector(".togg");
74 else if (elementsIndex
== elements
.length
- 1) {
75 selectedElement
.click();
79 window
.location
.href
= "https://news.ycombinator.com/item?id=" + selectedElement
.id
;
81 while (selectedElement
.querySelector("td.ind").firstElementChild
.width
!= 0) {