mirror of https://github.com/shuang854/Turtle
added netflix syncing in browser extension
parent
ee0a83e19d
commit
60aa771572
@ -0,0 +1,39 @@
|
||||
const videoPlayer = netflix.appContext.state.playerApp.getAPI().videoPlayer;
|
||||
const playerSessionId = videoPlayer.getAllPlayerSessionIds()[0];
|
||||
const netflixPlayer = videoPlayer.getVideoPlayerBySessionId(playerSessionId);
|
||||
const player = document.getElementsByTagName('video')[0];
|
||||
window.parent.postMessage('video ready', '*');
|
||||
|
||||
// Subscribe listeners on client-side actions
|
||||
player.addEventListener('play', (ev) => {
|
||||
window.parent.postMessage({ type: 'play', time: player.currentTime }, '*');
|
||||
});
|
||||
|
||||
player.addEventListener('pause', (ev) => {
|
||||
window.parent.postMessage({ type: 'pause', time: player.currentTime }, '*');
|
||||
});
|
||||
|
||||
// Subscribe listener for syncing requests
|
||||
window.addEventListener('message', (ev) => {
|
||||
let type = ev.data.type;
|
||||
|
||||
// Handle status request event
|
||||
if (ev.data.toString() === 'fetch current status') {
|
||||
ev.ports[0].postMessage({ isPlaying: player.paused, time: player.currentTime });
|
||||
}
|
||||
|
||||
// Handle seeking event
|
||||
if (type === 'seek') {
|
||||
netflixPlayer.seek(Number(ev.data.currentTime * 1000)); // Netflix Player object is needed to seek
|
||||
}
|
||||
|
||||
// Handle playing event
|
||||
if (type === 'playing') {
|
||||
if (ev.data.playing && player.paused) {
|
||||
player.play();
|
||||
}
|
||||
if (!ev.data.playing && !player.paused) {
|
||||
player.pause();
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,14 @@
|
||||
var playerReady = false;
|
||||
const NETFLIX_VID_URL = /https?:\/\/www\.netflix\.com\/watch\/([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
|
||||
|
||||
document.addEventListener('click', (e) => {
|
||||
if (!playerReady && window.location.toString().match(NETFLIX_VID_URL)) {
|
||||
let s = document.createElement('script');
|
||||
s.src = chrome.runtime.getURL('netflix.js');
|
||||
(document.head || document.documentElement).appendChild(s);
|
||||
playerReady = true;
|
||||
} else if (playerReady && !window.location.toString().match(NETFLIX_VID_URL)) {
|
||||
playerReady = false;
|
||||
window.parent.postMessage('video not ready', '*');
|
||||
}
|
||||
});
|
||||
Loading…
Reference in New Issue