11

I use uBlock Origin as my ad blocker. It makes it easy to add custom filters through a picker tool which can identify the HTML id and class attributes of elements.

There are some websites which generate a random id for the ad section each time the page is loaded. What is an effective way to block such ads? Blocking on a particular id is not possible anymore.

Here is an example of such a page. Notice the random id.

http://www.thelocal.fr/20170219/france-denounces-cyberattacks-blamed-on-moscow

enter image description here

To be clear, at this moment I am not much bothered by these ads on this particular website because they are clearly labelled and highlighted, so they are easy to skip when skimming a page. But in the past, this site did not label them, and it was hard to distinguish "sponsored" articles from normal ones. This is what motivated me to try to block them. Should I want to do it again in the future, I would like to know how.

Szabolcs
  • 3,097

2 Answers2

11

First, it’s all about CSS. Because there’s no unique ID, you need to somehow make a query that matches only the unwanted elements. You can test them in your browser’s developer tools using

document.querySelectorAll("...")

Sometimes, these elements have other attributes that uniquely identify them, like inline styles. You can match them using an attribute selector:

div[style="background: ..."]

Sometimes, these elements appear at a fixed point in the DOM tree, like in this case. Sometimes tricky to get right, but very easy here, because there’s always another (already blocked) ad preceding them:

div.ad_container + div[id*=-widget]

Maybe it’s the last element:

div:last-child

You can read more about CSS selectors at Mozilla Developer Network.

However, if done correctly, you can’t do anything without a script. I’ve already seen scripts that would create a tag with a random name (like <uaUZGI>) at a random location in the DOM tree that would then somehow be made to appear at the desired location on-screen.


In addition to regular CSS, uBlock Origin also supports “procedural cosmetic filters”. They are a very powerful extension to regular CSS filters. You can, for example:

  • Match (and thus hide) elements that contain certain text
  • Match elements that have certain styles applied
  • Match elements that contain elements matching a different selector
user219095
  • 65,551
0

Some websites are using browser's localstorage to save "randomized id" ads. If that is your case try blocking cookies/third-party cookies from that webpage in your browser.

For Chrome it would be on the bottom of this page: chrome://settings/cookies

iGRiK
  • 1