Skip to content

Commit

Permalink
Merge pull request #119 from richardfrost/youtube-auto-gen-subs
Browse files Browse the repository at this point in the history
Support for YouTube auto-generated subtitles
  • Loading branch information
richardfrost authored Feb 14, 2019
2 parents 24a0a1a + 8d5fc8d commit 7db933d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "advancedprofanityfilter",
"version": "2.0.1",
"version": "2.0.2",
"description": "A browser extension to filter profanity from webpages.",
"main": "filter.js",
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion src/script/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ class Popup {
// Initial summary data request
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {popup: true}, function(response) {
popup.populateSummary(response);
if (!chrome.runtime.lastError) {
popup.populateSummary(response);
}
});
});

Expand Down
36 changes: 33 additions & 3 deletions src/script/webAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class WebAudio {
'www.youtube.com': 'span.caption-visual-line'
}

static clean(filter, subtitleContainer, subSelector) {
static clean(filter, subtitleContainer, subSelector): void {
let filtered = false;
let subtitles = subtitleContainer.querySelectorAll(subSelector);

Expand All @@ -31,7 +31,21 @@ export default class WebAudio {
if (filtered) { filter.updateCounterBadge(); } // Update if modified
}

static mute(filter) {
static cleanYouTubeAutoSubs(filter, node): void {
let filtered = false;
let result = filter.replaceTextResult(node.textContent);
if (result.modified) {
filtered = true;
node.textContent = result.filtered;
WebAudio.mute(filter);
} else {
WebAudio.unmute(filter);
}

if (filtered) { filter.updateCounterBadge(); } // Update if modified
}

static mute(filter): void {
if (!filter.muted) {
filter.muted = true;

Expand Down Expand Up @@ -79,7 +93,7 @@ export default class WebAudio {
return Object.keys(WebAudio.subtitleSelectors);
}

static unmute(filter) {
static unmute(filter): void {
if (filter.muted) {
filter.muted = false;

Expand All @@ -97,4 +111,20 @@ export default class WebAudio {
}
}
}

static youTubeAutoSubsNodeIsSubtitleText(node): boolean {
let captionWindow = document.querySelectorAll('div.caption-window')[0]; // YouTube Auto-gen subs
return !!(captionWindow && captionWindow.contains(node));
}

static youTubeAutoSubsPresent(filter): boolean {
return !!(filter.hostname == 'www.youtube.com' && document.querySelectorAll('div.ytp-caption-window-rollup')[0]);
}

static youTubeAutoSubsSupportedNode(hostname: string, node: any): boolean {
if (hostname == 'www.youtube.com' && node.nodeName == '#text' && node.textContent != '') {
return !!(WebAudio.youTubeAutoSubsNodeIsSubtitleText(node));
}
return false;
}
}
8 changes: 7 additions & 1 deletion src/script/webFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ export default class WebFilter extends Filter {
mutation.addedNodes.forEach(node => {
if (!Page.isForbiddenNode(node)) {
// console.log('Added node(s):', node); // DEBUG - Mutation - addedNodes
if (filter.mutePage && WebAudio.supportedNode(filter.hostname, node)) {
if (filter.mutePage && WebAudio.youTubeAutoSubsPresent(filter)) { // YouTube Auto subs
if (WebAudio.youTubeAutoSubsSupportedNode(filter.hostname, node)) {
WebAudio.cleanYouTubeAutoSubs(filter, node); // Clean Auto subs
} else if (!WebAudio.youTubeAutoSubsNodeIsSubtitleText(node)) {
filter.cleanNode(node); // Clean the rest of the page
}
} else if (filter.mutePage && WebAudio.supportedNode(filter.hostname, node)) {
WebAudio.clean(filter, node, filter.subtitleSelector);
} else {
// console.log('Added node to filter', node); // DEBUG - Mutation addedNodes
Expand Down
2 changes: 1 addition & 1 deletion src/static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"short_name": "Profanity Filter",
"author": "Richard Frost",
"manifest_version": 2,
"version": "2.0.1",
"version": "2.0.2",
"description": "Advanced Profanity Filter helps to clean up bad language on the websites you and your family visit.",
"icons": {
"16": "img/icon16.png",
Expand Down

0 comments on commit 7db933d

Please sign in to comment.