Is a Macro That Handles YouTube Ads Possible?

I will read your post very carefully (tomorrow.) It sounds very interesting. I've come very close to solving all of the Youtube ad problems, but your assistance could possibly get us a complete solution. (I'm pretty useless at Javascript.)

Is it possible to use Javascript or AppleScript to return the coordinates of the video playback window in the current browser tab? Without this, it has taken me dozens of complicated KM actions to calculate this frame's location.

Running the remove() command every second seems to work fine. No testing (using queryselector) is necessary. I also discovered that the "SKIP ADS" button becomes pressable about a half second BEFORE the "SKIP ADS" button is rendered on screen. Which is odd, but doesn't let you avoid the ads. I have some additional ideas that I may ask you about once I've used your latest ideas.

Yeah that's pretty easy using this JavaScript Method:

document.querySelector('div#container.ytd-player').getBoundingClientRect()

This returns an object containing the height and width of the element in pixels, alongside the distances of the top, bottom, left and right edges of the element, relative to the top left corner of the browser window (excluding the URL bar and tab area). That function (well technically a method in strict JS terms) returns an object like this:

{
"width": 1676, Width of element
"height": 779, Height of element
"top": 56, Distance from window top edge to top of element
"right": 1676, Distance from window left side to elements right edge
"bottom": 835, Distance from top of window to bottom edge of element
"left": 0 Distance from window left edge to element left edge
}

These numbers can also go be negative, this occurs when the element has been scrolled past.

I'm not sure how to interact with JSON data in KBM (I'm still pretty new) but we could just return each number individually like this:

document.querySelector('div#container.ytd-player').getBoundingClientRect().right
document.querySelector('div#container.ytd-player').getBoundingClientRect().bottom

I've just put this macro together which seems to work with my small amount of testing:

The -50 on the right position is to avoid clicking the scroll bar in instances like mine where the scroll bar takes up no space on the screen until hovered. The -30 is to account for the top portion of the browser window (tabs, URL etc.) which in my case is 70 pixels, then minus a further 100 to rise the click position up to the skip button position.

As long as the lower half of the video is in view, this should click any skip button that's in that location. Unfortunately, this won't work with the mini player due to the distance of the skip button from the bottom edge being smaller. I tried to fiddle with the number but there's just no overlap. To account for both cases, there would have to be additional work done. Maybe an additional bit of JavaScript that checks for the presence of the mini player. Then just store the click adjustments in variables and depending on the presence of the mini player, use click adjustment variables that correspond to the type of video player.

I know that the banner style ad can come in different sizes but if they have a common html class for the outer container, the same technique could be used there too. Instead of getting the bottom and right positions, we'd get the top and right, then adjust the click position to be slightly left and down of that position.

Not only that, but they also change size depending upon the Zoom level. Anyway, I'm too tired to talk now. Cya tomorrow.

Together I'm sure we can defeat Youtube.

Yeah zoom also breaks the Skip Ad that I've just created. Luckily, I never zoom the page on YouTube and zoom levels can get very messy with it having different effects in different browsers. I mean, just removing the html for the banner like I showed earlier does the trick so that's good enough for that purpose as far as I can see.

Well at least your name is accurate, bye for now!

I have lots of experience trying to do the impossible by closing all Youtube ads/pop-ups, but never had a solution good/clean enough to publish on this forum. With the tips you've provided, I think we can write something that people will love. I'm going to give it another try today, but if you want the glory, I'll let you do it instead.

Which browser did you test that on? It always causes Safari on an M1 Mac Mini to actually crash. ("Termination Reason: Namespace SIGNAL, Code 11 Segmentation fault: 11")

I can process the numbers easily enough if we can stop the problem with crashing. There are several ways to access the numbers; your Javascript idea isn't necessary, but thanks for the idea.

I'm a little busy today but if I get some time, I'll try and come up with something that's all encompassing regarding the different types of ads.

I've just attempted the getBoundingClientRect method in Safari using the JS browser console and it seems to work fine as it's not a browser specific thing, just vanilla JavaScript. I'm also on an M1 Mac Mini. I did get an error when trying to run the macro though, but it was related to a safari setting which needed to be turned on. I didn't experience any crashes though. After turning this setting on, the macro worked in Safari. Safari required being quit and restarted after the setting was changed.

CleanShot 2022-03-01 at 12.17.13

Edit: No need for a plugin, I found how to speed up videos with JavaScript instead. Just run this in the browser once the ad starts:

document.querySelector('#movie_player video.video-stream.html5-main-video').playbackRate = 16

16 seems to be the maximum playback rate that can be set. Any higher results in a JS error. The great thing about this is that it will only effect the ad and once it ends, the video you were trying to watch will remain at normal speed. Just make sure that if you use it, the ad is currently on-screen or it will target the original video instead.


Original post:

Side note, but related to skipping YouTube ads. There is a Chrome extension which I've been using in Brave that is meant to help with controlling YouTube video speed.

Essentially you can set up hot keys that correspond with user-defined speed increases/decreases. I originally started using it because I felt that the built-in < and > hotkeys which adjusted speed by 25% were too big of a jump.

A welcome side-effect which I found, was that the speed increase also effected ads. The extension allows you to set up specific speeds and bind them to hotkeys, alongside the speed increment keys.
CleanShot 2022-03-01 at 12.43.06

As you can see from my screenshot, I have set up the Q key to set video speed to 10x. Of course, this could be even higher if desired. When an ad begins, I hit that key and the ad is dramatically sped up and yes, the ad timer is sped up too. This is amazing for speeding through longer un-skippable ads. After hitting a key like Q in my case, to set a specific speed, hitting the same key again will return the speed back to what it was prior to hitting Q the first time.

I know it's an external factor and not universal to all browsers, but incorporating the power of this into an ad skipping macro could be a great option.

Your skills are excellent and so is your creativity. I'm sure you can handle this without me. But I want to help. I have lots of experience in this topic.

As for Chrome, I refuse to use that product for reasons I won't get into here. If you come up with a solution that works better in Chrome, that's fine. But my goal is a browser-independent solution.

Can you create the Javascript to detect if the small frame is on the screen that contains the letters "Your video will". This would be a great tool.

A few months ago I was getting many popups below the video frame asking me if I want to pay for Youtube Premium. But for some odd reason those popups have not reappeared lately.

Many people may not realize it, but Youtube's interface does go through small changes every few months. Any solution we come up with may have to be updated every few months to take the changes into account.

This amazing, simple macro will get rid of the two main types of Ads in Youtube (well, what it actually does is press "SKIP ADS" and presses the "X" on the pop-up windows.) I'm planning to add more powerful features to this macro, but I'm still looking for an alternative to Bevelle's "document.querySelector('div#container.ytd-player').getBoundingClientRect()" which causes Safari to crash.

Youtube Ad Skipper Macro (v10.0.2)

Youtube Ad Skipper.kmmacros (3.6 KB)