Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to prioritize unwatched videos when autoplaying #2109

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/components/PreferencesPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@
@change="onChange($event)"
/>
</label>
<label v-if="autoPlayVideo" class="pref" for="chkAutoPlayPreferUnwatched">
<strong v-t="'actions.autoplay_prefer_unwatched'" />
<input
id="chkAutoPlayPreferUnwatched"
v-model="autoPlayPreferUnwatched"
class="checkbox"
type="checkbox"
@change="onChange($event)"
/>
</label>
<label v-if="autoPlayVideo" class="pref" for="chkAutoPlayPreferSameAuthor">
<strong v-t="'actions.autoplay_prefer_same_author'" />
<input
id="chkAutoPlayPreferSameAuthor"
v-model="autoPlayPreferSameAuthor"
class="checkbox"
type="checkbox"
@change="onChange($event)"
/>
</label>
<label class="pref" for="chkAudioOnly">
<strong v-t="'actions.audio_only'" />
<input id="chkAudioOnly" v-model="listen" class="checkbox" type="checkbox" @change="onChange($event)" />
Expand Down Expand Up @@ -346,6 +366,8 @@ export default {
minSegmentLength: 0,
selectedTheme: "dark",
autoPlayVideo: true,
autoPlayPreferUnwatched: true,
autoPlayPreferSameAuthor: false,
listen: false,
resolutions: [144, 240, 360, 480, 720, 1080, 1440, 2160, 4320],
defaultQuality: 0,
Expand Down Expand Up @@ -461,6 +483,8 @@ export default {
this.minSegmentLength = Math.max(this.getPreferenceNumber("minSegmentLength", 0), 0);
this.selectedTheme = this.getPreferenceString("theme", "dark");
this.autoPlayVideo = this.getPreferenceBoolean("playerAutoPlay", true);
this.autoPlayPreferUnwatched = this.getPreferenceBoolean("playerAutoPlayUnwatched", true);
this.autoPlayPreferSameAuthor = this.getPreferenceBoolean("playerAutoPlaySameAuthor", false);
this.listen = this.getPreferenceBoolean("listen", false);
this.defaultQuality = Number(localStorage.getItem("quality"));
this.bufferingGoal = Math.max(Number(localStorage.getItem("bufferGoal")), 10);
Expand Down Expand Up @@ -515,6 +539,8 @@ export default {
localStorage.setItem("minSegmentLength", this.minSegmentLength);
localStorage.setItem("theme", this.selectedTheme);
localStorage.setItem("playerAutoPlay", this.autoPlayVideo);
localStorage.setItem("playerAutoPlayUnwatched", this.autoPlayPreferUnwatched);
localStorage.setItem("playerAutoPlaySameAuthor", this.autoPlayPreferSameAuthor);
localStorage.setItem("listen", this.listen);
localStorage.setItem("quality", this.defaultQuality);
localStorage.setItem("bufferGoal", this.bufferingGoal);
Expand Down
9 changes: 8 additions & 1 deletion src/components/VideoPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default {
return {};
},
},
nextVideo: {
type: Object,
default: () => {
return {};
},
},
FireMasterK marked this conversation as resolved.
Show resolved Hide resolved
playlist: {
type: Object,
default: null,
Expand Down Expand Up @@ -611,7 +617,8 @@ export default {
},
navigateNext() {
const params = this.$route.query;
let url = this.playlist?.relatedStreams?.[this.index]?.url ?? this.video.relatedStreams[0].url;
const relatedUrl = this.nextVideo?.url ?? this.video.relatedStreams[0].url;
let url = this.playlist?.relatedStreams?.[this.index]?.url ?? relatedUrl;
const searchParams = new URLSearchParams();
for (var param in params)
switch (param) {
Expand Down
58 changes: 56 additions & 2 deletions src/components/WatchVideo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<VideoPlayer
ref="videoPlayer"
:video="video"
:next-video="nextVideo"
:sponsors="sponsors"
:playlist="playlist"
:index="index"
Expand Down Expand Up @@ -206,8 +207,12 @@
/>
<hr v-show="showRecs" />
<div v-show="showRecs">
<div v-if="nextVideo" class="">
<p>Next up:</p>
ai25 marked this conversation as resolved.
Show resolved Hide resolved
<VideoItem :key="nextVideo.url" :item="nextVideo" height="94" width="168" />
</div>
<ContentItem
v-for="related in video.relatedStreams"
v-for="related in filteredRelatedStreams"
:key="related.url"
:item="related"
height="94"
Expand All @@ -230,6 +235,7 @@ import PlaylistAddModal from "./PlaylistAddModal.vue";
import ShareModal from "./ShareModal.vue";
import PlaylistVideos from "./PlaylistVideos.vue";
import WatchOnYouTubeButton from "./WatchOnYouTubeButton.vue";
import VideoItem from "./VideoItem.vue";

export default {
name: "App",
Expand All @@ -243,13 +249,15 @@ export default {
ShareModal,
PlaylistVideos,
WatchOnYouTubeButton,
VideoItem,
},
data() {
const smallViewQuery = window.matchMedia("(max-width: 640px)");
return {
video: {
title: "Loading...",
},
nextVideo: null,
playlistId: null,
playlist: null,
index: null,
Expand Down Expand Up @@ -291,6 +299,15 @@ export default {
year: "numeric",
});
},
filteredRelatedStreams: _this => {
return _this.video.relatedStreams?.filter(video => video.url !== _this.nextVideo?.url);
},
preferUnwatched: _this => {
return _this.getPreferenceBoolean("playerAutoPlayUnwatched", true) && !_this.isEmbed;
},
preferAuthor: _this => {
return _this.getPreferenceBoolean("playerAutoPlaySameAuthor", false) && !_this.isEmbed;
},
},
mounted() {
// check screen size
Expand Down Expand Up @@ -425,7 +442,13 @@ export default {
});
xmlDoc.querySelectorAll("br").forEach(elem => (elem.outerHTML = "\n"));
this.video.description = this.rewriteDescription(xmlDoc.querySelector("body").innerHTML);
this.updateWatched(this.video.relatedStreams);
this.updateWatched(this.video.relatedStreams).then(() => {
if (!this.isEmbed) {
if (!this.playlistId) {
this.setNextVideo();
}
}
});
}
});
},
Expand Down Expand Up @@ -560,6 +583,37 @@ export default {
onTimeUpdate(time) {
this.currentTime = time;
},
setNextVideo() {
if (!this.preferUnwatched && !this.preferAuthor) {
this.nextVideo = this.video.relatedStreams[0];
return;
}
if (!this.preferUnwatched) {
this.nextVideo = this.video.relatedStreams.find(video => video.uploaderUrl === this.video.uploaderUrl);
if (!this.nextVideo) this.nextVideo = this.video.relatedStreams[0];
return;
}
for (let i = 0; i < this.video.relatedStreams.length; i++) {
let foundAuthorMatch = false;
const video = this.video.relatedStreams[i];
if (!video.watched) {
if (!this.preferAuthor) {
this.nextVideo = video;
break;
}
if (video.uploaderUrl === this.video.uploaderUrl) {
this.nextVideo = video;
foundAuthorMatch = true;
break;
} else if (!foundAuthorMatch) {
this.nextVideo = video;
}
}
if (!this.nextVideo) {
this.nextVideo = this.video.relatedStreams[0];
}
}
},
},
};
</script>
2 changes: 2 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"dark": "Dark",
"light": "Light",
"autoplay_video": "Autoplay Video",
"autoplay_prefer_unwatched": "Prefer unwatched videos",
"autoplay_prefer_same_author": "Prefer videos from the same author",
"audio_only": "Audio Only",
"default_quality": "Default Quality",
"buffering_goal": "Buffering Goal (in seconds)",
Expand Down
27 changes: 18 additions & 9 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,24 @@ const mixin = {
if (window.db && this.getPreferenceBoolean("watchHistory", false)) {
var tx = window.db.transaction("watch_history", "readonly");
var store = tx.objectStore("watch_history");
videos.map(async video => {
var request = store.get(video.url.substr(-11));
request.onsuccess = function (event) {
if (event.target.result) {
video.watched = true;
video.currentTime = event.target.result.currentTime;
}
};
});
return Promise.all(
videos.map(
video =>
new Promise(resolve => {
var request = store.get(video.url.substr(-11));
request.onsuccess = function (event) {
if (event.target.result) {
video.watched = true;
video.currentTime = event.target.result.currentTime;
}
resolve();
};
request.onerror = function () {
resolve();
};
}),
),
);
}
},
getLocalSubscriptions() {
Expand Down