Skip to content

Instantly share code, notes, and snippets.

@nvictor
Last active November 6, 2023 22:04
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 nvictor/6065ce8ef3b0b0acb0f122fe69312e66 to your computer and use it in GitHub Desktop.
Save nvictor/6065ce8ef3b0b0acb0f122fe69312e66 to your computer and use it in GitHub Desktop.
yt.js
const mastHeadHeight = document.querySelector("ytd-masthead").clientHeight;
let nextYtdRichGridRow = null;
function scrollToNextYtdRichGridRow() {
if (nextYtdRichGridRow === null) {
nextYtdRichGridRow = document.querySelector('ytd-rich-grid-row');
} else {
nextYtdRichGridRow = document.evaluate('following-sibling::ytd-rich-grid-row', nextYtdRichGridRow, null, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue;
}
// Scroll to the top of the next <ytd-rich-grid-row> element
if (nextYtdRichGridRow) {
const scrollToY = window.scrollY - mastHeadHeight + nextYtdRichGridRow.getBoundingClientRect().top;
window.scrollTo({ top: scrollToY, behavior: 'smooth' });
}
}
// Event listener for the space bar key press
document.addEventListener('keydown', function(event) {
// Check if the pressed key is the space bar (key code 32)
if (event.keyCode === 32) {
// Prevent the default space bar behavior (e.g., scrolling the page)
event.preventDefault();
// Call the function to scroll to the next <ytd-rich-grid-row> element
scrollToNextYtdRichGridRow();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment