Skip to content

Commit

Permalink
New skipping method and Cleaned up code
Browse files Browse the repository at this point in the history
Basically added a way of skipping ads using the adCenter. 
Removed the josn paths thing as it was causing the video player block.

I've tested and been using this version since yesterday and had no problems with seeing ads and have not been blocked.
  • Loading branch information
TheRealJoelmatic committed Dec 9, 2023
1 parent 9a7ee2d commit 23f26f1
Showing 1 changed file with 43 additions and 75 deletions.
118 changes: 43 additions & 75 deletions Youtube-Ad-blocker-Reminder-Remover.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Remove Adblock Thing
// @namespace http://tampermonkey.net/
// @version 2.4
// @version 2.5
// @description Removes Adblock Thing
// @author JoelMatic
// @match https://www.youtube.com/*
Expand All @@ -24,31 +24,21 @@
const removePopup = true;

// Enable debug messages into the console
const debug = true;
const debugMessages = true;

//
// CODE
//
// If you have any suggestions, bug reports,
// or want to contribute to this userscript,
// feel free to create issues or pull requests in the GitHub repository.
//
// GITHUB: https://github.com/TheRealJoelmatic/RemoveAdblockThing

// Specify domains and JSON paths to remove
const domainsToRemove = [
'*.youtube-nocookie.com/*'
];
const jsonPathsToRemove = [
'playerResponse.adPlacements',
'playerResponse.playerAds',
'adPlacements',
'playerAds',
'playerConfig',
'auxiliaryUi.messageRenderers.enforcementMessageViewModel'
];

// Observe config
const observerConfig = {
childList: true,
subtree: true
};

//
// Varables used for the Popup Remover
//
const keyEvent = new KeyboardEvent("keydown", {
key: "k",
code: "KeyK",
Expand All @@ -68,24 +58,20 @@
//This is used to check if the video has been unpaused already
let unpausedAfterSkip = 0;

if (debug) console.log("Remove Adblock Thing: Remove Adblock Thing: Script started");
// Old variable but could work in some cases
window.__ytplayer_adblockDetected = false;
//Set everything up here
if (debugMessages) console.log("Remove Adblock Thing: Script started");

if(adblocker) addblocker();
if(removePopup) popupRemover();
if(removePopup) observer.observe(document.body, observerConfig);

// Remove Them pesski popups
function popupRemover() {
removeJsonPaths(domainsToRemove, jsonPathsToRemove);
setInterval(() => {

const fullScreenButton = document.querySelector(".ytp-fullscreen-button");
const modalOverlay = document.querySelector("tp-yt-iron-overlay-backdrop");
const popup = document.querySelector(".style-scope ytd-enforcement-message-view-model");
const popupButton = document.getElementById("dismiss-button");
// const popupButton2 = document.getElementById("ytp-play-button ytp-button");

const video1 = document.querySelector("#movie_player > video.html5-main-video");
const video2 = document.querySelector("#movie_player > .html5-video-container > video");
Expand All @@ -100,20 +86,20 @@
}

if (popup) {
if (debug) console.log("Remove Adblock Thing: Popup detected, removing...");
if (debugMessages) console.log("Remove Adblock Thing: Popup detected, removing...");

if(popupButton) popupButton.click();
// if(popupButton2) popupButton2.click();

popup.remove();
unpausedAfterSkip = 2;

fullScreenButton.dispatchEvent(mouseEvent);

setTimeout(() => {
fullScreenButton.dispatchEvent(mouseEvent);
}, 500);

if (debug) console.log("Remove Adblock Thing: Popup removed");
if (debugMessages) console.log("Remove Adblock Thing: Popup removed");
}

// Check if the video is paused after removing the popup
Expand All @@ -128,10 +114,10 @@
// undetected adblocker method
function addblocker()
{
setInterval(() =>
{
const skipBtn = document.querySelector('.videoAdUiSkipButton,.ytp-ad-skip-button');
setInterval(() =>{
const ad = [...document.querySelectorAll('.ad-showing')][0];

const skipBtn = document.querySelector('.videoAdUiSkipButton,.ytp-ad-skip-button');
const sidAd = document.querySelector('ytd-action-companion-ad-renderer');
const displayAd = document.querySelector('div#root.style-scope.ytd-display-ad-renderer.yt-simple-endpoint');
const sparklesContainer = document.querySelector('div#sparkles-container.style-scope.ytd-promoted-sparkles-web-renderer');
Expand All @@ -145,11 +131,24 @@

if (ad)
{
if (debugMessages) console.log("Remove Adblock Thing: Found Ad");

const video = document.querySelector('video');
video.playbackRate = 10;
video.volume = 0;
video.currentTime = video.duration;
skipBtn?.click();
const openAdCenterButton = document.querySelector('.ytp-ad-button-icon');
const blockAdButton = document.querySelector('[label="Block ad"]');
const blockAdButtonConfirm = document.querySelector('.Eddif [label="CONTINUE"] button');

if (video) video.playbackRate = 10;
if (video) video.volume = 0;
if (video) video.currentTime = video.duration || 0;

if (video) skipBtn?.click();

openAdCenterButton?.click();
blockAdButton?.click();
blockAdButtonConfirm?.click();

if (debugMessages) console.log("Remove Adblock Thing: skipped Ad (✔️)");
}

sidAd?.remove();
Expand All @@ -162,13 +161,14 @@
sponsor?.forEach((element) => {
if (element.getAttribute("id") === "panels") {
element.childNodes?.forEach((childElement) => {
if (childElement.data.targetId && childElement.data.targetId !=="engagement-panel-macro-markers-description-chapters")
if (childElement.data.targetId && childElement.data.targetId !=="engagement-panel-macro-markers-description-chapters"){
//Skipping the Chapters section
childElement.remove();
});
} else {
element.remove();
}
}
});
} else {
element.remove();
}
});
nonVid?.click();
}, 50)
Expand All @@ -181,39 +181,7 @@
// Simulate pressing the "k" key to unpause the video
document.dispatchEvent(keyEvent);
unpausedAfterSkip = 0;
if (debug) console.log("Remove Adblock Thing: Unpaused video using 'k' key");
if (debugMessages) console.log("Remove Adblock Thing: Unpaused video using 'k' key");
} else if (unpausedAfterSkip > 0) unpausedAfterSkip--;
}
function removeJsonPaths(domains, jsonPaths)
{
const currentDomain = window.location.hostname;
if (!domains.includes(currentDomain)) return;

jsonPaths.forEach(jsonPath => {
const pathParts = jsonPath.split('.');
let obj = window;
let previousObj = null;
let partToSetUndefined = null;

for (const part of pathParts) {
if (obj.hasOwnProperty(part)) {
previousObj = obj; // Keep track of the parent object.
partToSetUndefined = part; // Update the part that we may set to undefined.
obj = obj[part];
} else {
break; // Stop when we reach a non-existing part.
}
}

// If we've identified a valid part to set to undefined, do so.
if (previousObj && partToSetUndefined !== null) {
previousObj[partToSetUndefined] = undefined;
}
});
}
// Observe and remove ads when new content is loaded dynamically
const observer = new MutationObserver(() =>
{
removeJsonPaths(domainsToRemove, jsonPathsToRemove);
});
})();

0 comments on commit 23f26f1

Please sign in to comment.