Skip to content

Commit

Permalink
Reimport chapters if they are found to have changed
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayyy committed Oct 11, 2022
1 parent 85e3d3b commit 4729268
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ function createPreviewBar(): void {

if (el) {
const chapterVote = new ChapterVote(voteAsync);
previewBar = new PreviewBar(el, onMobileYouTube, onInvidious, chapterVote);
previewBar = new PreviewBar(el, onMobileYouTube, onInvidious, chapterVote, () => importExistingChapters(false));

updatePreviewBar();

Expand Down
11 changes: 10 additions & 1 deletion src/js-components/previewBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class PreviewBar {
segments: PreviewBarSegment[] = [];
existingChapters: PreviewBarSegment[] = [];
videoDuration = 0;
updateExistingChapters: () => void;
lastChapterUpdate = 0;

// For chapter bar
hoveredSection: HTMLElement;
Expand All @@ -58,7 +60,7 @@ class PreviewBar {
unfilteredChapterGroups: ChapterGroup[];
chapterGroups: ChapterGroup[];

constructor(parent: HTMLElement, onMobileYouTube: boolean, onInvidious: boolean, chapterVote: ChapterVote, test=false) {
constructor(parent: HTMLElement, onMobileYouTube: boolean, onInvidious: boolean, chapterVote: ChapterVote, updateExistingChapters: () => void, test=false) {
if (test) return;
this.container = document.createElement('ul');
this.container.id = 'previewbar';
Expand All @@ -67,6 +69,7 @@ class PreviewBar {
this.onMobileYouTube = onMobileYouTube;
this.onInvidious = onInvidious;
this.chapterVote = chapterVote;
this.updateExistingChapters = updateExistingChapters;

this.updatePageElements();
this.createElement(parent);
Expand Down Expand Up @@ -647,6 +650,12 @@ class PreviewBar {

cursor += sectionWidthDecimal;
}

if (sections.length !== 0 && sections.length !== this.existingChapters?.length
&& Date.now() - this.lastChapterUpdate > 3000) {
this.lastChapterUpdate = Date.now();
this.updateExistingChapters();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/previewBar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PreviewBar, { PreviewBarSegment } from "../src/js-components/previewBar";
describe("createChapterRenderGroups", () => {
let previewBar: PreviewBar;
beforeEach(() => {
previewBar = new PreviewBar(null, null, null, null, true);
previewBar = new PreviewBar(null, null, null, null, null, true);
})

it("Two unrelated times", () => {
Expand Down

0 comments on commit 4729268

Please sign in to comment.