Skip to content

[FEATURE] Block YouTube ads in videos, Shorts and on the site - #31

Merged
ignaciojuarez merged 1 commit into
mainfrom
feature/youtube-ad-blocking
Nov 11, 2026
Merged

ignaciojuarez merged 1 commit into
mainfrom
feature/youtube-ad-blocking

Conversation

@ignaciojuarez

Copy link
Copy Markdown
Owner

What

Built-in YouTube ad blocking for WebKit and Chromium tabs:

  • In-video and Shorts ads are removed before the player reads them.
  • Sponsored slots on home, search and watch pages are hidden.
  • Playback stall recovery: when YouTube holds a video back for about the length of the ads it planned, Cobble reloads it with a request context that has no ads. Without this, removing ads costs a 5–17 s black screen.

It follows each profile's content blocking switch and its site exceptions: allowing https://www.youtube.com through the blocker shows ads again. Settings explains this under the Enable rules switch.

How

  • Engine/Shared/YouTubeAdBlock.swift: one page-world script, run at document start. It only acts on YouTube player hosts (not Studio or account pages). It ports uBlock Origin's current YouTube filters (GPL-3.0, credited in THIRD_PARTY_NOTICES.md).
  • WebKit: WebKitPage.applyContentRules() installs the script in all frames and updates it when blocking or exceptions change. WebKit cannot remove a single user script, so the others are reinstalled unchanged.
  • Chromium: Cobble's rules extension now also carries the script in the main world. Its YouTube site access is granted after install. A failed grant is reported, and request blocking keeps working.
  • Docs: FEATURES, DECISIONS, ARCHITECTURE, RESEARCH and CHANGELOG.

Testing

  • ./Scripts/test.sh: 656 tests, 0 failures (3 skipped). New tests cover ad removal, site-slot hiding, exceptions and other sites, and user-script swapping that keeps other scripts.
  • swift test: 13 Chromium adapter tests pass, including the rules-extension manifest.
  • Live YouTube, logged out, with isolated data (local only, not in CI):
Engine Blocking off Blocking on
WebKit, 4 videos ads on 4/4, video at 19–20 s (one still in a 30 s ad at 25 s) 0 ads, video at 4–5 s; comments load
WebKit, in-app navigation — 0 ads, video at 2–3 s
Chromium (Full), 2 videos 15 s and 15 + 18 s ads; video at 19 s 0 ads, video at 4–8 s
Search page 10 sponsored slots visible 0 visible

Limits

  • YouTube changes its ad delivery often, so this script will need refreshes from uBlock Origin's filters.
  • Chromium private windows are not covered yet: the rules extension stays out of incognito.
  • Not checked: signed-in and Premium accounts, mid-roll ads on long videos, and server-inserted ads (handled by seeking past them, but not observed in testing).

🤖 Generated with Claude Code

YouTube's in-video ads come inside its own player data, so network rules
cannot block them. A page-world script, shared by WebKit and Chromium,
removes ad placements before the player reads them, recovers from the
playback stall YouTube imposes when its ads do not play, and hides
sponsored slots. It follows each profile's content-blocking switch and
site exceptions. The techniques port uBlock Origin's current YouTube filters.

Live checks (logged out): without blocking, 15-33 s of ads before each
video; with blocking, no ads and playback within 2-8 s in both engines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ignaciojuarez
ignaciojuarez merged commit c736d67 into main Nov 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant