Fix starting segments when there is a t=? in the url

This commit is contained in:
Ajay Ramachandran
2021-10-02 17:13:30 -04:00
parent 5f34a777f1
commit 8605e23fbb
3 changed files with 59 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ import * as Chat from "./js-components/chat";
import { getCategoryActionType } from "./utils/categoryUtils"; import { getCategoryActionType } from "./utils/categoryUtils";
import { SkipButtonControlBar } from "./js-components/skipButtonControlBar"; import { SkipButtonControlBar } from "./js-components/skipButtonControlBar";
import { Tooltip } from "./render/Tooltip"; import { Tooltip } from "./render/Tooltip";
import { getStartTimeFromUrl } from "./utils/urlParser";
// Hack to get the CSS loaded on permission-based sites (Invidious) // Hack to get the CSS loaded on permission-based sites (Invidious)
utils.wait(() => Config.config !== null, 5000, 10).then(addCSS); utils.wait(() => Config.config !== null, 5000, 10).then(addCSS);
@@ -775,22 +776,25 @@ function retryFetch(): void {
function startSkipScheduleCheckingForStartSponsors() { function startSkipScheduleCheckingForStartSponsors() {
if (!switchingVideos && sponsorTimes) { if (!switchingVideos && sponsorTimes) {
// See if there are any starting sponsors // See if there are any starting sponsors
let startingSegmentTime = -1; let startingSegmentTime = getStartTimeFromUrl(document.URL) || -1;
let found = false;
let startingSegment: SponsorTime = null; let startingSegment: SponsorTime = null;
for (const time of sponsorTimes) { for (const time of sponsorTimes) {
if (time.segment[0] <= video.currentTime && time.segment[0] > startingSegmentTime && time.segment[1] > video.currentTime if (time.segment[0] <= video.currentTime && time.segment[0] > startingSegmentTime && time.segment[1] > video.currentTime
&& getCategoryActionType(time.category) === CategoryActionType.Skippable) { && getCategoryActionType(time.category) === CategoryActionType.Skippable) {
startingSegmentTime = time.segment[0]; startingSegmentTime = time.segment[0];
startingSegment = time; startingSegment = time;
found = true;
break; break;
} }
} }
if (startingSegmentTime === -1) { if (!found) {
for (const time of sponsorTimesSubmitting) { for (const time of sponsorTimesSubmitting) {
if (time.segment[0] <= video.currentTime && time.segment[0] > startingSegmentTime && time.segment[1] > video.currentTime if (time.segment[0] <= video.currentTime && time.segment[0] > startingSegmentTime && time.segment[1] > video.currentTime
&& getCategoryActionType(time.category) === CategoryActionType.Skippable) { && getCategoryActionType(time.category) === CategoryActionType.Skippable) {
startingSegmentTime = time.segment[0]; startingSegmentTime = time.segment[0];
startingSegment = time; startingSegment = time;
found = true;
break; break;
} }
} }

26
src/utils/urlParser.ts Normal file
View File

@@ -0,0 +1,26 @@
export function getStartTimeFromUrl(url: string): number {
const urlParams = new URLSearchParams(url);
const time = urlParams?.get('t') || urlParams?.get('time_continue');
return urlTimeToSeconds(time);
}
export function urlTimeToSeconds(time: string): number {
if (!time) {
return 0;
}
const re = /(?:(?<hours>\d{1,3})h)?(?:(?<minutes>\d{1,2})m)?(?<seconds>\d+)s?/;
const match = re.exec(time);
if (match) {
const hours = parseInt(match.groups.hours ?? '0', 10);
const minutes = parseInt(match.groups.minutes ?? '0', 10);
const seconds = parseInt(match.groups.seconds ?? '0', 10);
return hours * 3600 + minutes * 60 + seconds;
} else if (/\d+/.test(time)) {
return parseInt(time, 10);
}
}

27
test/urlParser.test.ts Normal file
View File

@@ -0,0 +1,27 @@
import { getStartTimeFromUrl } from '../src/utils/urlParser';
describe("getStartTimeFromUrl", () => {
it("parses with a number", () => {
expect(getStartTimeFromUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=123")).toBe(123);
});
it("parses with a seconds", () => {
expect(getStartTimeFromUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=123s")).toBe(123);
});
it("parses with a minutes", () => {
expect(getStartTimeFromUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=23m3s")).toBe(23 * 60 + 3);
});
it("parses with a hours", () => {
expect(getStartTimeFromUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=1h2m3s")).toBe(1 * 60 * 60 + 2 * 60 + 3);
});
it("works with time_continue", () => {
expect(getStartTimeFromUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ&time_continue=123")).toBe(123);
});
it("works with no time", () => {
expect(getStartTimeFromUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ")).toBe(0);
});
});