diff options
author | Ajay <[email protected]> | 2022-02-22 21:22:30 -0500 |
---|---|---|
committer | Ajay <[email protected]> | 2022-02-22 21:22:30 -0500 |
commit | 2ebc5489cddc12652bbd66a07a50fe051256a1e1 (patch) | |
tree | 911c608226b3308588947d4e051ff165423d6a47 /src/utils/pageUtils.ts | |
parent | cf3b3c5c48b60e9495f7989d7a9af6896bad8acc (diff) | |
download | SponsorBlock-2ebc5489cddc12652bbd66a07a50fe051256a1e1.tar.gz SponsorBlock-2ebc5489cddc12652bbd66a07a50fe051256a1e1.zip |
Load existing chapters
Diffstat (limited to 'src/utils/pageUtils.ts')
-rw-r--r-- | src/utils/pageUtils.ts | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/utils/pageUtils.ts b/src/utils/pageUtils.ts index 8f484d73..da3a2f00 100644 --- a/src/utils/pageUtils.ts +++ b/src/utils/pageUtils.ts @@ -1,4 +1,7 @@ -export function getControls(): HTMLElement | false { +import { ActionType, Category, SponsorSourceType, SponsorTime, VideoID } from "../types"; +import { GenericUtils } from "./genericUtils"; + +export function getControls(): HTMLElement { const controlsSelectors = [ // YouTube ".ytp-right-controls", @@ -16,7 +19,7 @@ export function getControls(): HTMLElement | false { } } - return false; + return null; } export function isVisible(element: HTMLElement): boolean { @@ -61,4 +64,42 @@ export function getHashParams(): Record<string, unknown> { } return {}; +} + +export function getExistingChapters(currentVideoID: VideoID, duration: number): SponsorTime[] { + const chaptersBox = document.querySelector("ytd-macro-markers-list-renderer"); + + const chapters: SponsorTime[] = []; + if (chaptersBox) { + let lastSegment: SponsorTime = null; + const links = chaptersBox.querySelectorAll("ytd-macro-markers-list-item-renderer > a"); + for (const link of links) { + const timeElement = link.querySelector("#time") as HTMLElement; + const description = link.querySelector("#details h4") as HTMLElement; + if (timeElement && description?.innerText?.length > 0 && link.getAttribute("href")?.includes(currentVideoID)) { + const time = GenericUtils.getFormattedTimeToSeconds(timeElement.innerText); + + if (lastSegment) { + lastSegment.segment[1] = time; + chapters.push(lastSegment); + } + + lastSegment = { + segment: [time, null], + category: "chapter" as Category, + actionType: ActionType.Chapter, + description: description.innerText, + source: SponsorSourceType.YouTube, + UUID: null + }; + } + } + + if (lastSegment) { + lastSegment.segment[1] = duration; + chapters.push(lastSegment); + } + } + + return chapters; }
\ No newline at end of file |