Skip to content

Commit

Permalink
✨ Add APF Captions support to watcher mode
Browse files Browse the repository at this point in the history
  • Loading branch information
richardfrost committed Aug 19, 2022
1 parent 0d22831 commit 5ed54fb
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/script/webAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@ export default class WebAudio {
const data: WatcherData = { initialCall: true, textResults: [] };
let captions;

// Always hide captions if using APF Captions
if (rule.apfCaptions && rule.displaySelector) instance.hideSubtitles(rule);

if (rule.parentSelector) { // Tested on: Amazon
captions = document.querySelector(rule.parentSelector) as HTMLElement;
if (captions && captions.textContent && captions.textContent.trim()) {
Expand All @@ -1087,9 +1090,29 @@ export default class WebAudio {

if (data.skipped) { return false; }

// Hide/show caption text
// Hide/show caption/subtitle text
const shouldBeShown = instance.subtitlesShouldBeShown(rule, data.filtered);
shouldBeShown ? instance.showSubtitles(rule) : instance.hideSubtitles(rule);

if (rule.apfCaptions) {
let container;
if (rule.apfCaptionsSelector) container = getElement(rule.apfCaptionsSelector);
if (!container) container = video.parentElement;

// Clean up old APF Caption lines
const oldLines = getElement('div.APF-subtitles');
if (oldLines) oldLines.remove();

// Show APF Caption lines if they should be shown
if (shouldBeShown && data.textResults.length) {
const captionLines = data.textResults.map((result) => rule.filterSubtitles && result.modified ? result.filtered : result.original);
const apfLines = captionLines.map((text) => instance.apfCaptionLine(rule, text));
const apfCaptions = instance.apfCaptionLines(rule, apfLines);
container.appendChild(apfCaptions);
}
} else {
shouldBeShown ? instance.showSubtitles(rule) : instance.hideSubtitles(rule);
}

if (data.filtered) { instance.filter.updateCounterBadge(); }
} else {
if (rule.ignoreMutations) { instance.filter.startObserving(); } // Start observing when video is not playing
Expand Down

0 comments on commit 5ed54fb

Please sign in to comment.