Skip to content

Commit

Permalink
Add "Ad Skipper" JS plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Natanel-Shitrit committed Oct 18, 2024
1 parent ee9ad05 commit 21aaed4
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/app/plugins/client/ad-skipper.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export const meta = {
name: "Ad Skipper"
}

export const afterInit = () => {
const PLAYER_TYPE_AD = 2;
const SKIP_AD_POOLING_RATE_MS = 10;

window.domUtils.ensureDomLoaded(() => {
const playerApi = window.domUtils.playerApi();
playerApi.addEventListener("onVideoDataChange", ev => {
if (ev.playertype !== PLAYER_TYPE_AD) return;
console.log("Found AD, skipping...", ev);
var keepProbing = true;

const onAd = async () => {
await new Promise((resolve, _reject) => {
const videoContainer = document.getElementById("movie_player");

const skipAdHandler = () => {
const isAd = videoContainer?.classList.contains("ad-interrupting") || videoContainer?.classList.contains("ad-showing");
const skipLock = document.querySelector(".ytp-ad-preview-text-modern")?.innerText;

if (isAd && skipLock) {
const videoPlayer = document.getElementsByClassName("video-stream")[0];
videoPlayer.muted = true;
videoPlayer.currentTime = videoPlayer.duration - 0.01;
videoPlayer.paused && videoPlayer.play()
document.querySelector(".ytp-ad-skip-button")?.click();
document.querySelector(".ytp-ad-skip-button-modern")?.click();
keepProbing = false;
}

resolve();
};

setTimeout(skipAdHandler, SKIP_AD_POOLING_RATE_MS);
});

if (keepProbing) onAd();
};

onAd();
})
})
};

0 comments on commit 21aaed4

Please sign in to comment.