Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adblocker): add new option AdSpeedup #2235

Merged
merged 12 commits into from
Jul 18, 2024
4 changes: 4 additions & 0 deletions src/i18n/resources/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@
},
"name": "Ad Blocker"
},
"ad-speedup": {
"name": "Ad Speedup",
"description": "If an ad play it mutes the audio and sets playback speed to 16x"
},
"album-actions": {
"description": "Adds Undislike, Dislike, Like, and Unlike buttons to apply this to all songs in a playlist or album",
"name": "Album Actions"
Expand Down
56 changes: 56 additions & 0 deletions src/plugins/adblocker/adSpeedup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function skipAd(target: Element) {
const skipButton = target.querySelector<HTMLButtonElement>('button.ytp-ad-skip-button-modern');
if (skipButton) {
skipButton.click();
}
}

function speedUpAndMute(player: Element, isAdShowing: boolean) {
const video = player.querySelector<HTMLVideoElement>('video');
if (!video) return;
if (isAdShowing) {
video.playbackRate = 16;
video.muted = true;
} else if (!isAdShowing) {
video.playbackRate = 1;
video.muted = false;
}
}

export const loadAdSpeedup = async () => {
const player = document.querySelector<HTMLVideoElement>('#movie_player');
if (!player) return;

new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (
mutation.type === 'attributes' &&
mutation.attributeName === 'class'
) {
const target = mutation.target as HTMLElement;

const isAdShowing =
target.classList.contains('ad-showing') ||
target.classList.contains('ad-interrupting');
speedUpAndMute(target, isAdShowing);
}
if (
mutation.type === 'childList' &&
mutation.addedNodes.length &&
mutation.target instanceof HTMLElement
) {
skipAd(mutation.target);
}
}
}).observe(player, {
attributes: true,
childList: true,
subtree: true,
});

const isAdShowing =
player.classList.contains('ad-showing') ||
player.classList.contains('ad-interrupting');
speedUpAndMute(player, isAdShowing);
skipAd(player);
}
9 changes: 9 additions & 0 deletions src/plugins/adblocker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { inject, isInjected } from './injectors/inject';
import { t } from '@/i18n';

import type { BrowserWindow } from 'electron';
import { loadAdSpeedup } from './adSpeedup';

interface AdblockerConfig {
/**
Expand Down Expand Up @@ -72,6 +73,14 @@ export default createPlugin({
},
];
},
renderer: {
async onPlayerApiReady(_, {getConfig}) {
const config = await getConfig();
if (config.blocker === blockers.AdSpeedup) {
await loadAdSpeedup();
}
}
},
backend: {
mainWindow: null as BrowserWindow | null,
async start({ getConfig, window }) {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/adblocker/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const blockers = {
WithBlocklists: 'With blocklists',
InPlayer: 'In player',
AdSpeedup: 'Ad speedup',
} as const;
Loading