+
{% endblock content %}
diff --git a/tubearchivist/static/cast-videos.js b/tubearchivist/static/cast-videos.js
index 6b98058..0a30af4 100644
--- a/tubearchivist/static/cast-videos.js
+++ b/tubearchivist/static/cast-videos.js
@@ -13,6 +13,16 @@ function initializeCastApi() {
castConnectionChange(player)
}
);
+ playerController.addEventListener(
+ cast.framework.RemotePlayerEventType.CURRENT_TIME_CHANGED, function() {
+ castVideoProgress(player)
+ }
+ );
+ playerController.addEventListener(
+ cast.framework.RemotePlayerEventType.IS_PAUSED_CHANGED, function() {
+ castVideoPaused(player)
+ }
+ );
}
@@ -26,32 +36,64 @@ function castConnectionChange(player) {
}
}
+function castVideoProgress(player) {
+ var videoId = getVideoPlayerVideoId();
+ if (player.mediaInfo.contentId.includes(videoId)) {
+ var currentTime = player.currentTime;
+ var duration = player.duration;
+ if ((currentTime % 10) <= 1.0 && currentTime != 0 && duration != 0) { // Check progress every 10 seconds or else progress is checked a few times a second
+ postVideoProgress(videoId, currentTime);
+ if (!getVideoPlayerWatchStatus()) { // Check if video is already marked as watched
+ if (watchedThreshold(currentTime, duration)) {
+ isWatched(videoId);
+ }
+ }
+ }
+ }
+}
+
+function castVideoPaused(player) {
+ var videoId = getVideoPlayerVideoId();
+ var currentTime = player.currentTime;
+ var duration = player.duration;
+ if (player.mediaInfo != null) {
+ if (player.mediaInfo.contentId.includes(videoId)) {
+ if (currentTime != 0 && duration != 0) {
+ postVideoProgress(videoId, currentTime);
+ }
+ }
+ }
+}
+
function castStart() {
var castSession = cast.framework.CastContext.getInstance().getCurrentSession();
-
// Check if there is already media playing on the cast target to prevent recasting on page reload or switching to another video page
if (!castSession.getMediaSession()) {
- contentId = document.getElementById("video-source").src; // Get video URL
- contentTitle = document.getElementById('video-title').innerHTML; // Get video title
- contentImage = document.getElementById("video-item").poster; // Get video thumbnail URL
+ var videoId = getVideoPlayerVideoId();
+ var videoData = getVideoData(videoId);
+ var contentId = getURL() + videoData.data.media_url;
+ var contentTitle = videoData.data.title;
+ var contentImage = getURL() + videoData.data.vid_thumb_url;
+
contentType = 'video/mp4'; // Set content type, only videos right now so it is hard coded
- contentCurrentTime = document.getElementById("video-item").currentTime; // Get video's current position
+ contentCurrentTime = getVideoPlayerCurrentTime(); // Get video's current position
contentActiveSubtitle = [];
// Check if a subtitle is turned on.
- for (var i = 0; i < document.getElementById("video-item").textTracks.length; i++) {
- if (document.getElementById("video-item").textTracks[i].mode == "showing") {
+ for (var i = 0; i < getVideoPlayer().textTracks.length; i++) {
+ if (getVideoPlayer().textTracks[i].mode == "showing") {
contentActiveSubtitle =[i + 1];
}
}
contentSubtitles = [];
- for (var i = 0; i < document.getElementById("video-item").children.length; i++) {
- if (document.getElementById("video-item").children[i].tagName == "TRACK") {
+ var videoSubtitles = videoData.data.subtitles; // Array of subtitles
+ if (typeof(videoSubtitles) != 'undefined' && videoData.config.downloads.subtitle) {
+ for (var i = 0; i < videoSubtitles.length; i++) {
subtitle = new chrome.cast.media.Track(i, chrome.cast.media.TrackType.TEXT);
- subtitle.trackContentId = document.getElementById("video-item").children[i].src;
+ subtitle.trackContentId = videoSubtitles[i].media_url;
subtitle.trackContentType = 'text/vtt';
subtitle.subtype = chrome.cast.media.TextTrackType.SUBTITLES;
- subtitle.name = document.getElementById("video-item").children[i].label;
- subtitle.language = document.getElementById("video-item").children[i].srclang;
+ subtitle.name = videoSubtitles[i].name;
+ subtitle.language = videoSubtitles[i].lang;
subtitle.customData = null;
contentSubtitles.push(subtitle);
}
@@ -91,7 +133,7 @@ function shiftCurrentTime(contentCurrentTime) { // Shift media back 3 seconds to
function castSuccessful() {
// console.log('Cast Successful.');
- document.getElementById("video-item").pause(); // Pause browser video on successful cast
+ getVideoPlayer().pause(); // Pause browser video on successful cast
}
function castFailed(errorCode) {
diff --git a/tubearchivist/static/script.js b/tubearchivist/static/script.js
index ec3aa40..162e5ab 100644
--- a/tubearchivist/static/script.js
+++ b/tubearchivist/static/script.js
@@ -9,7 +9,7 @@ function sortChange(sortValue) {
}
function isWatched(youtube_id) {
- // sendVideoProgress(youtube_id, 0); // Reset video progress on watched;
+ postVideoProgress(youtube_id, 0); // Reset video progress on watched;
var payload = JSON.stringify({'watched': youtube_id});
sendPost(payload);
var seenIcon = document.createElement('img');
@@ -34,7 +34,7 @@ function isWatchedButton(button) {
}
function isUnwatched(youtube_id) {
- // sendVideoProgress(youtube_id, 0); // Reset video progress on unwatched;
+ postVideoProgress(youtube_id, 0); // Reset video progress on unwatched;
var payload = JSON.stringify({'un_watched': youtube_id});
sendPost(payload);
var unseenIcon = document.createElement('img');
@@ -298,20 +298,12 @@ function cancelDelete() {
function createPlayer(button) {
var videoId = button.getAttribute('data-id');
var videoData = getVideoData(videoId);
- var videoUrl = videoData.media_url;
- var videoThumbUrl = videoData.vid_thumb_url;
- var videoName = videoData.title;
+ var videoName = videoData.data.title;
- var subtitles = '';
- var videoSubtitles = videoData.subtitles; // Array of subtitles
- if (typeof(videoSubtitles) != 'undefined') {
- for (var i = 0; i < videoSubtitles.length; i++) {
- subtitles += `