correct parsing for timestamp-links in current video

This commit is contained in:
robertkleinschuster 2023-05-25 20:43:11 +02:00 committed by robertkleinschuster
parent 3dfdf8806f
commit 66f3584895
4 changed files with 56 additions and 33 deletions

View file

@ -51,11 +51,12 @@ export default {
if (this.getPreferenceBoolean("searchHistory", false)) if (this.getPreferenceBoolean("searchHistory", false))
this.searchSuggestions = JSON.parse(localStorage.getItem("search_history")) ?? []; this.searchSuggestions = JSON.parse(localStorage.getItem("search_history")) ?? [];
} else { } else {
this.searchSuggestions = ( this.searchSuggestions =
await this.fetchJson(this.apiUrl() + "/opensearch/suggestions", { (
query: this.searchText, await this.fetchJson(this.apiUrl() + "/opensearch/suggestions", {
}) query: this.searchText,
)?.[1]; })
)?.[1] ?? [];
} }
this.searchSuggestions.unshift(this.searchText); this.searchSuggestions.unshift(this.searchText);
this.setSelected(0); this.setSelected(0);

View file

@ -23,6 +23,7 @@
<script> <script>
import "shaka-player/dist/controls.css"; import "shaka-player/dist/controls.css";
import { parseTimeParam } from "@/utils/Misc";
const shaka = import("shaka-player/dist/shaka-player.ui.js"); const shaka = import("shaka-player/dist/shaka-player.ui.js");
if (!window.muxjs) { if (!window.muxjs) {
import("mux.js").then(muxjs => { import("mux.js").then(muxjs => {
@ -231,24 +232,7 @@ export default {
const time = this.$route.query.t ?? this.$route.query.start; const time = this.$route.query.t ?? this.$route.query.start;
if (time) { if (time) {
let start = 0; videoEl.currentTime = parseTimeParam(time);
if (/^[\d]*$/g.test(time)) {
start = time;
} else {
const hours = /([\d]*)h/gi.exec(time)?.[1];
const minutes = /([\d]*)m/gi.exec(time)?.[1];
const seconds = /([\d]*)s/gi.exec(time)?.[1];
if (hours) {
start += parseInt(hours) * 60 * 60;
}
if (minutes) {
start += parseInt(minutes) * 60;
}
if (seconds) {
start += parseInt(seconds);
}
}
videoEl.currentTime = start;
this.initialSeekComplete = true; this.initialSeekComplete = true;
} else if (window.db && this.getPreferenceBoolean("watchHistory", false)) { } else if (window.db && this.getPreferenceBoolean("watchHistory", false)) {
var tx = window.db.transaction("watch_history", "readonly"); var tx = window.db.transaction("watch_history", "readonly");

View file

@ -236,6 +236,7 @@ import PlaylistVideos from "./PlaylistVideos.vue";
import WatchOnYouTubeButton from "./WatchOnYouTubeButton.vue"; import WatchOnYouTubeButton from "./WatchOnYouTubeButton.vue";
import LoadingIndicatorPage from "./LoadingIndicatorPage.vue"; import LoadingIndicatorPage from "./LoadingIndicatorPage.vue";
import ToastComponent from "./ToastComponent.vue"; import ToastComponent from "./ToastComponent.vue";
import { parseTimeParam } from "@/utils/Misc";
export default { export default {
name: "App", name: "App",
@ -540,16 +541,32 @@ export default {
}, },
handleClick(event) { handleClick(event) {
if (!event || !event.target) return; if (!event || !event.target) return;
var target = event.target; if (!event.target.matches("a[href]")) return;
if ( const target = event.target;
!target.nodeName == "A" || if (!target.getAttribute("href")) return;
!target.getAttribute("href") || if (this.handleTimestampLinks(target)) {
!target.innerText.match(/(?:[\d]{1,2}:)?(?:[\d]{1,2}):(?:[\d]{1,2})/) event.preventDefault();
) }
return; },
const time = parseInt(target.getAttribute("href").match(/(?<=t=)\d+/)[0]); handleTimestampLinks(target) {
this.navigate(time); try {
event.preventDefault(); const url = new URL(target.getAttribute("href"), document.baseURI);
if (
url.searchParams.size > 2 ||
url.searchParams.get("v") !== this.getVideoId() ||
!url.searchParams.has("t")
) {
return false;
}
const time = parseTimeParam(url.searchParams.get("t"));
if (time) {
this.navigate(time);
}
return true;
} catch (e) {
console.error(e);
}
return false;
}, },
handleScroll() { handleScroll() {
if (this.loading || !this.comments || !this.comments.nextpage) return; if (this.loading || !this.comments || !this.comments.nextpage) return;

View file

@ -6,3 +6,24 @@ export const isEmail = input => {
); );
return result; return result;
}; };
export const parseTimeParam = time => {
let start = 0;
if (/^[\d]*$/g.test(time)) {
start = time;
} else {
const hours = /([\d]*)h/gi.exec(time)?.[1];
const minutes = /([\d]*)m/gi.exec(time)?.[1];
const seconds = /([\d]*)s/gi.exec(time)?.[1];
if (hours) {
start += parseInt(hours) * 60 * 60;
}
if (minutes) {
start += parseInt(minutes) * 60;
}
if (seconds) {
start += parseInt(seconds);
}
}
return start;
};