Skip to content

Instantly share code, notes, and snippets.

@funkyremi
Last active October 14, 2024 15:05
Show Gist options
  • Save funkyremi/1ecf4dba9308063eca70b40dde3755db to your computer and use it in GitHub Desktop.
Save funkyremi/1ecf4dba9308063eca70b40dde3755db to your computer and use it in GitHub Desktop.
Display Netflix Captions in PiP mode
// Hit refresh when playing a Netflix video and paste the following code in your console.
// This script will replace the native captions by WebVTT captions that will work in PiP mode.
// Use the code here to trigger PiP in safari: https://gist.github.com/funkyremi/9d7b70285282d532c3f8c964810e7bf9
const lang = 'fr';
const hideNetflixCaptions = (() => {
const style = document.createElement("style");
style.type = "text/css";
const hideCc = ".player-timedtext {display: none !important;}";
if (style.styleSheet) {
style.styleSheet.cssText = hideCc;
} else {
style.appendChild(document.createTextNode(hideCc));
}
document.getElementsByTagName("head")[0].appendChild(style);
})();
const displayVtt = data => {
console.log("detail", data);
const textTrack = data.timedtexttracks.find(cc => cc.language === lang);
const textTrackUrls =
textTrack.ttDownloadables["webvtt-lssdh-ios8"].downloadUrls;
const url = textTrackUrls[Object.keys(textTrackUrls)[0]];
// Wait for the video element to add new text track
const interval = setInterval(() => {
const video = document.querySelector("video");
if (video) {
clearInterval(interval);
const track = document.createElement("track");
track.kind = "captions";
track.label = "Subtitle";
track.src = url;
track.addEventListener("load", function() {
video.textTracks[0].mode = "showing";
});
video.appendChild(track);
}
}, 100);
};
const hijackJson = ((parse, stringify) => {
JSON.parse = function(text) {
const data = parse(text);
if (
data &&
data.result &&
data.result.timedtexttracks &&
data.result.movieId
) {
displayVtt(data.result);
}
return data;
};
JSON.stringify = function(data) {
if (data && data.params && data.params.profiles) {
data.params.profiles.unshift("webvtt-lssdh-ios8");
}
return stringify(data);
};
})(JSON.parse, JSON.stringify);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment