DEV Community

wimdenherder
wimdenherder

Posted on

Youtube adblocker does not work? Solve it with 6 lines of code (December 2023)

I used to have a chrome extension Adblock to block YouTube video's but somwhere november 2023 it stopped working. YouTube noticed its activity and blocked the full experience whatsoever.

So I tried to code it myself and it turned out to be just 6 lines of code that does the trick:

setInterval(async () => {
    if(!document.querySelector('.ad-showing')) return;
    document.querySelector('video').currentTime = 1000;
    await (async () => new Promise(r => setTimeout(r, 100)))();
    document.querySelector('.ytp-ad-skip-button-slot')?.click();
}, 300);
Enter fullscreen mode Exit fullscreen mode

This is what it does

  • checks if an ad is displayed
  • skips advertisement to 1000 seconds
  • waits 0,1 sec
  • clicks skip button

Around this you will find the setInterval that executes the function every 0,3 sec

Create a Chrome Extension

This is the ChatGPT prompt:

Create a chrome extension that executes the following script on a youtube page. No pop up. No background script. Manifest version 3.
setInterval(async () => {
if(!document.querySelector('.ad-showing')) return;
document.querySelector('video').currentTime = 1000;
await (async () => new Promise(r => setTimeout(r, 100)))();"

Click this link to find ChatGPT's instruction how to create and install the Chrome Extension. It's really simple!

Top comments (0)