Skip to content

Commit

Permalink
major: [bump] fadblock v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
0x48piraj committed Oct 15, 2023
1 parent 4d39c3e commit e622bd4
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 312 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ This blocker is designed to monitor advertisements, automatically seek the ads a

Nevertheless, the extension's underlying logic enables it to fast-forward through the ad content to its conclusion. The entire process is optimized to occur within an extremely brief timeframe, typically **<=50 milliseconds**, ensuring a smooth and uninterrupted user experience.

## Features

This extension effectively eliminates all categories of YouTube advertisements:

**Category 1:** Ads visible on the homepage.

**Category 2:** Ads overlaid atop the suggested video list.

**Category 3:** Ads displayed below the video description.

> Starting on April 6th, 2023, the “Overlay ads” ad format will no longer appear on YouTube to help improve the viewer experience and shift engagement to higher performing ad formats on desktop and mobile devices. Overlay ads are a legacy ad format that only served on desktop and are disruptive for viewers.
**Category 4:** Skippable 5-second-must-be-watched video ads, which may be positioned at the video's start, midway, or at the end.

**Category 5:** Ads similar to Category 4, but with two consecutive video ads instead of one.

**Category 6:** Inescapable 10-second video ads, which must be watched.


## Install

Expand Down
94 changes: 46 additions & 48 deletions src/chrome/js/background.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
function getVideoContainer() {
const videoContainer = Array.from(
document.getElementsByClassName("html5-video-container"),
);
return videoContainer.length > 0 ? videoContainer[0] : null;
}

function getVideoWrapper() {
return getVideoContainer() ? getVideoContainer().parentNode : null;
}

function getVideoPlayer() {
return getVideoContainer() ? getVideoContainer().firstChild : null;
}

function isAdShowing() {
const wrapper = getVideoWrapper();
return wrapper !== null
? wrapper !== undefined && String(wrapper.className).includes("ad-showing")
: null;
}

function getSkipButton() {
const skipAdButton = Array.from(
document.getElementsByClassName("ytp-ad-skip-button ytp-button"),
);
return skipAdButton.length > 0 ? skipAdButton[0] : null;
}

function waitForPlayer() {
if (getVideoPlayer()) {
hookVideoPlayer();
} else {
setTimeout(() => {
waitForPlayer();
}, 200);
// RELOAD ALL YOUTUBE TABS WHEN THE EXTENSION IS INSTALLED OR UPDATED
chrome.runtime.onInstalled.addListener((details) => {
switch (details.reason) {
case "install":
console.info("EXTENSION INSTALLED");
break;
case "update":
console.info("EXTENSION UPDATED");
break;
case "chrome_update":
case "shared_module_update":
default:
console.info("BROWSER UPDATED");
break;
}
}

function hookVideoPlayer() {
const videoPlayer = getVideoPlayer();
videoPlayer.addEventListener("timeupdate", () => {
getSkipButton()?.click();
chrome.tabs.query({}, (tabs) => {
tabs
.filter((tab) => tab.url.startsWith("https://www.youtube.com/"))
.forEach(({ id }) => {
chrome.tabs.reload(id);
});
});
});

if (isAdShowing()) {
videoPlayer.currentTime = videoPlayer.duration - 1;
videoPlayer.pause();
videoPlayer.play();
}
}
const taimuRipu = async () => {
await new Promise((resolve, _reject) => {
const videoContainer = document.getElementById("movie_player");

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

if (isAd && skipLock) {
const videoPlayer = document.getElementsByClassName("video-stream")[0];
videoPlayer.currentTime = videoPlayer.duration - 0.1;
// CLICK ON THE SKIP AD BTN
document.querySelector(".ytp-ad-skip-button")?.click();
}

resolve();
};

// RUN IT ONLY AFTER 100 MILLISECONDS
setTimeout(setTimeoutHandler, 100);
});

taimuRipu();
};

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
Expand All @@ -57,7 +55,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
) {
chrome.scripting.executeScript({
target: { tabId: tabId },
function: waitForPlayer,
function: taimuRipu,
});
}
});
99 changes: 45 additions & 54 deletions src/chrome/js/content.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,47 @@
function getVideoContainer() {
const videoContainer = Array.from(
document.getElementsByClassName("html5-video-container"),
);
return videoContainer.length > 0 ? videoContainer[0] : null;
}

function getVideoWrapper() {
return getVideoContainer() ? getVideoContainer().parentNode : null;
}

function getVideoPlayer() {
return getVideoContainer() ? getVideoContainer().firstChild : null;
}

function isAdShowing() {
const wrapper = getVideoWrapper();
return wrapper !== null
? wrapper !== undefined && String(wrapper.className).includes("ad-showing")
: null;
}

function getSkipButton() {
const skipAdButton = Array.from(
document.getElementsByClassName("ytp-ad-skip-button ytp-button"),
);
return skipAdButton.length > 0 ? skipAdButton[0] : null;
}

function waitForPlayer() {
if (getVideoPlayer()) {
hookVideoPlayer();
} else {
setTimeout(() => {
waitForPlayer();
}, 200);
}
}

function hookVideoPlayer() {
const videoPlayer = getVideoPlayer();
videoPlayer.addEventListener("timeupdate", () => {
getSkipButton()?.click();
const taimuRipu = async () => {
await new Promise((resolve, _reject) => {
const videoContainer = document.getElementById("movie_player");

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

if (isAd && skipLock) {
const videoPlayer = document.getElementsByClassName("video-stream")[0];
videoPlayer.currentTime = videoPlayer.duration - 0.1;
// CLICK ON THE SKIP AD BTN
document.querySelector(".ytp-ad-skip-button")?.click();
}

const staticAds = [".ytd-companion-slot-renderer", ".ytd-action-companion-ad-renderer", // in-feed video ads
".ytd-watch-next-secondary-results-renderer.sparkles-light-cta", ".ytd-unlimited-offer-module-renderer", // similar components
".ytp-ad-overlay-image", ".ytp-ad-text-overlay", // deprecated overlay ads (04-06-2023)
".ytd-display-ad-renderer", ".ytd-statement-banner-renderer", // homepage ads
".ytd-banner-promo-renderer", ".ytd-video-masthead-ad-v3-renderer", ".ytd-primetime-promo-renderer" // subscribe for premium & youtube tv ads
];

staticAds.forEach((ad) => {
document.hideElementsBySelector(ad);
});

resolve();
};

// RUN IT ONLY AFTER 100 MILLISECONDS
setTimeout(setTimeoutHandler, 100);
});

if (isAdShowing()) {
videoPlayer.currentTime = videoPlayer.duration - 1;
videoPlayer.pause();
videoPlayer.play();
}
}

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log(message)
return true
});
taimuRipu();
};


const init = async () => {
Document.prototype.hideElementsBySelector = (selector) =>
[...document.querySelectorAll(selector)].forEach(
(el) => (el.style.display = "none")
);

taimuRipu();
};

init();
2 changes: 1 addition & 1 deletion src/chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "fadblock",
"version": "1.0",
"version": "1.1",
"description": "A fast, lightweight, and undetectable YouTube Ads Blocker for Chrome.",
"icons": {
"16": "assets/icons/icon-16x16.png",
Expand Down
94 changes: 46 additions & 48 deletions src/firefox/js/background.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
function getVideoContainer() {
const videoContainer = Array.from(
document.getElementsByClassName("html5-video-container"),
);
return videoContainer.length > 0 ? videoContainer[0] : null;
}

function getVideoWrapper() {
return getVideoContainer() ? getVideoContainer().parentNode : null;
}

function getVideoPlayer() {
return getVideoContainer() ? getVideoContainer().firstChild : null;
}

function isAdShowing() {
const wrapper = getVideoWrapper();
return wrapper !== null
? wrapper !== undefined && String(wrapper.className).includes("ad-showing")
: null;
}

function getSkipButton() {
const skipAdButton = Array.from(
document.getElementsByClassName("ytp-ad-skip-button ytp-button"),
);
return skipAdButton.length > 0 ? skipAdButton[0] : null;
}

function waitForPlayer() {
if (getVideoPlayer()) {
hookVideoPlayer();
} else {
setTimeout(() => {
waitForPlayer();
}, 200);
// RELOAD ALL YOUTUBE TABS WHEN THE EXTENSION IS INSTALLED OR UPDATED
browser.runtime.onInstalled.addListener((details) => {
switch (details.reason) {
case "install":
console.info("EXTENSION INSTALLED");
break;
case "update":
console.info("EXTENSION UPDATED");
break;
case "browser_update":
case "shared_module_update":
default:
console.info("BROWSER UPDATED");
break;
}
}

function hookVideoPlayer() {
const videoPlayer = getVideoPlayer();
videoPlayer.addEventListener("timeupdate", () => {
getSkipButton()?.click();
browser.tabs.query({}, (tabs) => {
tabs
.filter((tab) => tab.url.startsWith("https://www.youtube.com/"))
.forEach(({ id }) => {
browser.tabs.reload(id);
});
});
});

if (isAdShowing()) {
videoPlayer.currentTime = videoPlayer.duration - 1;
videoPlayer.pause();
videoPlayer.play();
}
}
const taimuRipu = async () => {
await new Promise((resolve, _reject) => {
const videoContainer = document.getElementById("movie_player");

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

if (isAd && skipLock) {
const videoPlayer = document.getElementsByClassName("video-stream")[0];
videoPlayer.currentTime = videoPlayer.duration - 0.1;
// CLICK ON THE SKIP AD BTN
document.querySelector(".ytp-ad-skip-button")?.click();
}

resolve();
};

// RUN IT ONLY AFTER 100 MILLISECONDS
setTimeout(setTimeoutHandler, 100);
});

taimuRipu();
};

browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
Expand All @@ -57,7 +55,7 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
) {
browser.scripting.executeScript({
target: { tabId: tabId },
function: waitForPlayer,
func: taimuRipu,
});
}
});
Loading

0 comments on commit e622bd4

Please sign in to comment.