hacker news - scroll to next most outer post

14/02/2025, 4:56:16 pm

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        hacker news - scroll to next most outer post
// @namespace   Violentmonkey Scripts
// @match       https://news.ycombinator.com/item*
// @grant       none
// @version     1.0
// @license MIT
// @author      -
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
// @description 14/02/2025, 4:56:16 pm
// ==/UserScript==

const { register } = VM.shortcut;

const viewportHeight = window.innerHeight
const outerMostCommentElements = Array.from(document.querySelectorAll(".comment-tree>tbody>tr:has(td[indent='0'])"))

VM.shortcut.register('n', () => {
  if(document.activeElement.type === 'textarea'){
    // Is this hackey? maybe.
    document.activeElement.value+="n"
    return
  }


  const scrollY = window.scrollY || window.pageYOffset;
  const buffer = 50; // Buffer for invisible padding

    // Find the next element below viewport
  const nextElement = outerMostCommentElements.find(elem => {
    const elemTop = elem.getBoundingClientRect().top;
    return elemTop >= (viewportHeight - buffer);
  });

  if (nextElement) {
    nextElement.scrollIntoView({ behavior: "smooth", block: "center" });
  }

  // else {
  //   // If no next element found, scroll to the last element
  //   const lastElement = outerMostCommentElements[outerMostCommentElements.length - 1];
  //   if (lastElement && lastElement.getBoundingClientRect().top < (viewportHeight - buffer)) {
  //     lastElement.scrollIntoView({ behavior: "smooth", block: "center" });
  //   }
  // }

//   for (const elem of outerMostCommentElements){
//     const elemYPosition = elem.getBoundingClientRect().top
//     // -50 to give it a bit of a buffer as there is invisible padding and stuff.
//     const elemIsBelowBottomViewport = elemYPosition >= (document.body.scrollTop + (viewportHeight - 50))

//     console.log(elemIsBelowBottomViewport)
//     console.log(elem.getAttribute("id"))

//     if(elemIsBelowBottomViewport){
//       elem.scrollIntoView({behavior:"smooth", block:"center"})
//       break
//     }
//   }

});