Help With Clicking “Skip Ads” Link on YouTube

I'm trying to create a macro that will click the "Skip Ads" link on YouTube.

This is what I am using, but it is not working (I may have the class wrong):

Execute JavaScript in Safari:

document.getElementsByClassName('video-ads ytp-ad-module')[0].click();

This is what I am trying to click:

Skip

Any advice is appreciated, including ideas on how to track down the class of the element.

just curious, why don't you use adblocker extensions?

I do run one (AdBlock Pro), but it stopped blocking YouTube ads a bit ago. Otherwise, it works well.

Do you recommend a better adblocker?

I usually use Chrome for YouTube. Let google products go with google products. :grinning:
"ADGUARD" works well on Chrome.

I tried AdGuard, and I don't like that an app needs to run in the background/on the menu bar.

Going back to 1Blocker, which seems to be much better at YouTube ad blocking than it used to be.

I use the Chrome extension AdGuard, not the app.
image

Personally, I’ve found the best result with Ublock Origin. It works great for YouTube.

The ad blockers I tried, did remove the YouTube ads, but I still had to wait until the ad's play period was up.

I solved this by making a script to click the Skip Ads button, like I originally planned.

Since YouTube rapidly changes the page after the ad ends, I had trouble locating the class name that I needed. I ended up taking a quick screenshot of the page's code before it vanished. That helped immensely.

Here is the AppleScript code that I use. I still get aa smile on my face every time I trigger it:

to clickClassName(theClassName, elementnum)
	
	tell application "Safari"
		
		do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
		
	end tell
	
end clickClassName

clickClassName("ytp-ad-skip-button", 0)
5 Likes

Jim, how do you trigger this script?

A simple keystroke (it is not automated).

Thank you, kind man! Based on your script, I created my own for clicking (Like button on Youtube for Safari), thank you very much. It may be useful to someone. :slight_smile:
I use Automator/ Apple Script/ Quick action + Keyboard shortcut (⌥L)

to clickClassName(theClassName, elementnum)
	tell application "Safari"
		do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1	
	end tell
end clickClassName

clickClassName("yt-spec-button-shape-next yt-spec-button-shape-next--tonal yt-spec-button-shape-next--mono yt-spec-button-shape-next--size-m yt-spec-button-shape-next--icon-leading yt-spec-button-shape-next--segmented-start", 0)