How to uBlock all elements which `class` is not `X`?
Apr 19, 1:37pm

To block all elements that do NOT have a specific class (e.g., X) using uBlock Origin, you can use a cosmetic filter with the :not() CSS selector.

Solution

Use the following filter in uBlock Origin's dashboard under the Custom filters tab (or in the Cosmetic filters section for a specific site):

plaintext
example.com##:not(.X)

Explanation:

  • example.com → Replace with the domain(s) you want to apply the filter to. Use * for all sites.
  • ## → Indicates a cosmetic (element-hiding) filter.
  • :not(.X) → Selects all elements except those with the class X.

Examples:

  1. Block all elements except those with class X on all sites:
    plaintext
    *##:not(.X)
  2. Block all elements except those with class X on example.com:
    plaintext
    example.com##:not(.X)
  3. Block all <div> elements except those with class X:
    plaintext
    *##div:not(.X)

How to Add the Filter:

  1. Open uBlock Origin dashboard.
  2. Go to the "Custom filters" tab.
  3. Add your filter (e.g., example.com##:not(.X)).
  4. Click "Apply changes".

Notes:

  • This works for cosmetic filtering (hiding elements visually).
  • If you want to block network requests (e.g., scripts, images) from elements without class X, you need a different approach (e.g., using :matches-css() or script injection, which is more advanced).
  • Test the filter on the site to ensure it works as expected.
Apr 19, 1:37pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Le Chat can make mistakes. Check answers. Learn more