Last active
November 6, 2023 22:04
-
-
Save nvictor/6065ce8ef3b0b0acb0f122fe69312e66 to your computer and use it in GitHub Desktop.
yt.js
This file contains 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
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