I am trying to make a chrome extension that will provide ctrl+left arrow
and ctrl+right arrow
to backward or forward the YouTube video by 2 seconds. Here is my script :
const player = document.querySelector('ytd-player').getPlayer();
const seconds = 2;
window.addEventListener('keydown', function (e) {
if (e.ctrlKey) {
if (e.keyCode === 37) {
player.seekToStreamTime(player.getCurrentTime() - seconds)
}
else if (e.keyCode === 39) {
player.seekToStreamTime(player.getCurrentTime() + seconds)
}
}
})
console.log('youtube-seconds extension loaded.');
The script is working if I run directly from the chrome devTools but if I make an extension with the same code it doesn't work.
After investigating the code I noticed the issue comes from getPlayer
function.
The document.querySelector('ytd-player')
works well if I console.log
but getPlayer
returns nothing from the script.
How would one explain that ? and how can I make this work ? am I missing a permission or something ?