I'm writing a chrome extension with ReactJS that lets users insert subtitles on Youtube. I've been trying to insert subtitles on Youtube with the track element but it's not working.
Youtube has the following element:
<video tabindex="-1" class="video-stream html5-main-video"
controlslist="nodownload" style="width: 760px; height: 428px; left:
0px; top: 0px;" src="blob:https://www.youtube.com/eb4c349e-ac8a-465d-
b6b1-53acfcb66aa8">
</video>
I managed to inject a track element with this function:
insertSubtitle() {
var track = document.createElement("track");
track.kind = "subtitles";
track.label = "YTSUBS";
track.srclang = "en";
track.mode = "showing";
track.src = "subs.vtt"
var videoContainer = document.querySelector("video.video-stream.html5-main-video");
if(videoContainer){
videoContainer.prepend(track);
console.log("loaded subs");
} else {
console.log("video container not found");
}
}
The subs.vtt
file is located in the same folder as the javascript file with the code above. When the code loads on Youtube I get the "loaded subs" message but the subtitles won't appear.
I tried changing the track.src URL to gist, tried to start a local server with
python -m SimpleHTTPServer
as shown here, and suggested here so that I could change the track.src URL to http://localhost:8000/subtitle.vtt. Both solutions didn't work.
Does anyone have an idea why it isn't working?