Skip to content

Instantly share code, notes, and snippets.

@LiamKarlMitchell
Created October 14, 2023 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LiamKarlMitchell/55f280276bdba31db167bc0324921947 to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/55f280276bdba31db167bc0324921947 to your computer and use it in GitHub Desktop.
Attempt to block youtube ads, English NZ
// https://www.youtube.com/watch?v=LcmT38ZPTjg
var _STAGE_ = 0;
var _FAIL_COUNT_ = 0;
var __INTERVAL__ = setInterval(function() {
let iframe = null;
let blockAdBtn = null;
let continueElement = null;
let closeBtn = null;
let infoBtn = null;
if (_STAGE_ > 0) {
console.log(`Stage: ${_STAGE_} Fail Counter: ${_FAIL_COUNT_}`);
}
if (_STAGE_ > 0 && _FAIL_COUNT_ > 50) {
_STAGE_ --;
_FAIL_COUNT_ = 0;
console.log('Stepping back due to fail count being too high.')
}
switch (_STAGE_) {
case 0:
// var infoBtn = document.getElementById('ad-info-hover-text-button:h');
let infoBtns = document.querySelectorAll('button[aria-label="My Ad Center"]');
if (infoBtns.length > 0) {
infoBtn = infoBtns[0];
}
if (infoBtn == null || infoBtn.checkVisibility() === false) {
_FAIL_COUNT_++;
return;
}
console.log('Info Button Found');
infoBtn.click();
_STAGE_ = 1;
_FAIL_COUNT_ = 0;
break;
case 1:
// Go into iFrame?
// iframe #iframe classes style-scope yt-about-this-ad-renderer
// TODO: Other languages, other selectors.
blockAdBtn = null;
iframe = document.getElementById('iframe');
if (iframe) {
blockAdBtn = iframe.contentWindow.document.querySelector('gm3-text-button[label="Block ad"]');
}
if (blockAdBtn == null || blockAdBtn.checkVisibility() === false) {
_FAIL_COUNT_++;
return;
}
console.log('Found Block Ad Btn');
blockAdBtn.click();
_STAGE_ = 2;
_FAIL_COUNT_ = 0;
break;
case 2:
iframe = document.getElementById('iframe');
allFound = null;
if (iframe) {
allFound = iframe.contentWindow.document.querySelectorAll('div > span > span');
}
if (allFound === null || allFound.length === 0) {
_FAIL_COUNT_++;
return;
}
continueElement = null;
for (const element of allFound) {
if (element.textContent.includes('CONTINUE') && element.checkVisibility() === true) {
continueElement = element;
break; // Exit the loop once you've found the element
}
}
if (continueElement) {
console.log('continue element found');
// Do something with the "CONTINUE" element, for example, clicking it:
continueElement.parentElement.click();
_STAGE_ = 3;
_FAIL_COUNT_ = 0;
return;
} else {
console.log('No element with the text "CONTINUE" found.');
_FAIL_COUNT_++;
}
break;
case 3:
console.log('Looking for Close Dialog');
iframe = document.getElementById('iframe');
closeBtn = null;
if (iframe) {
closeBtn = iframe.contentWindow.document.querySelector('button[aria-label="Close"]');
}
if (closeBtn == null || closeBtn.checkVisibility() === false) {
_FAIL_COUNT_++;
return;
}
console.log('Closing My Ad Center dialog presumably.');
closeBtn.click();
_STAGE_ = 0;
_FAIL_COUNT_ = 0;
break;
}
}, 300);
// I found 300ms to be ok, but want to make a further improvement where it runs faster for other steps but lazy.
function stopBlockTest() {
clearInterval(__INTERVAL__);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment