For those trying to control Netflix's video player from their extensions: I have found a simple solution.
Current time and pause status: can be easily obtained using
document.querySelector('video').currentTime
document.querySelector('video').paused
(Simply accessing this data doesn't redirect to the error page)
Resume/Pause: Here we have two options:
Simply execute
document.querySelector('video').click();
this pauses/unpaused the video. You can use paused status fetching described above to make sure that you don't unpause video that's already paused.
Add script, which itself adds elements to the document with click event listeners:
const actualCode = `
const pauseElem = document.createElement('div');
pauseElem.id = 'my_pause_elem';
pauseElem.addEventListener('click', () => {
const videoPlayer = window.netflix.appContext.state.playerApp.getAPI().videoPlayer;
const player = videoPlayer.getVideoPlayerBySessionId(videoPlayer.getAllPlayerSessionIds()[0]);
player.pause();
});
const resumeElem = document.createElement('div');
resumeElem.id = 'my_resume_elem';
resumeElem.addEventListener('click', () => {
const videoPlayer = window.netflix.appContext.state.playerApp.getAPI().videoPlayer;
const player = videoPlayer.getVideoPlayerBySessionId(videoPlayer.getAllPlayerSessionIds()[0]);
player.play();
});
document.body.appendChild(pauseElem);
document.body.appendChild(resumeElem);
`;
const script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();
Now all you need to do is
document.getElementById('my_pause_elem').click();
document.getElementById('my_resume_elem').click();
to pause/unpause.
Seek: This has the same solution as above: add an element with an event listener, which uses spotify API to seek. Special thing here is that we need to pass a timestamp. This can be done by setting an attribute value to our element:
const actualCode = `
const seekElem = document.createElement('div');
seekElem.id = "my_seek_elem";
seekElem.addEventListener('click', () => {
const newTime = Number(document.getElementById('my_seek_elem').getAttribute('timestamp')) * 1000;
const videoPlayer = window.netflix.appContext.state.playerApp.getAPI().videoPlayer;
const player = videoPlayer.getVideoPlayerBySessionId(videoPlayer.getAllPlayerSessionIds()[0]);
player.seek(newTime);
});
(document.body).appendChild(seekElem);
`;
const script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();
Now all we need to do is trigger this event after setting the timestamp:
document.getElementById('my_seek_elem').setAttribute('timestamp', '' + timestamp);
document.getElementById('my_seek_elem').click();
That's all!