facebook-adblock / facebook_adblock

An open-source Ad Blocker for Facebook™
GNU General Public License v3.0
215 stars 42 forks source link

New way to find sponsored element #132

Open arye321 opened 2 years ago

arye321 commented 2 years ago

Hi, I wanted to make a pull request but the code is too complicated for me so I'll suggest code improvement for finding sponsored elemnts

run this in facebook webpage console:

document.querySelectorAll("use").forEach(element=>{
  const link = element.getAttribute("xlink:href").substring(1)
  if (document.querySelector(`text[id=${link}]`).innerHTML === "Sponsored"){
      element.closest('div[role]').style.borderStyle = 'solid'
      element.closest('div[role]').style.borderColor = 'red'
  }
})

this will mark all current ads in english interface .

the new Sponsor label is inside an SVG with a <use> element, inside the use element theres a shadow root (closed) element.

image

you cant extract that so you have to follow the xlink:href of the use, which is always a #gid(number) , this leads to a <text> element

image

the innerHTML of the text element is the text inside the original element. if the innerHTML is 'Sponsored' you mark it.

hope it helps

Sledmine commented 2 years ago

This was not working for me, as it selects the entire div for where all the other posts live as well, I edited your selector to get the parent div the Sponsor post, it got dirty but it does the job, probably someone else can simplify it and integrate it with the extension:

document.querySelectorAll("use").forEach(element=>{
  const link = element.getAttribute("xlink:href").substring(1)
  if (document.querySelector(`text[id=${link}]`).innerHTML === "Sponsored"){
      element.closest('div > div:not([class]) > div[class] > div:not([class]) > div[class]').style.borderStyle = 'solid'
      element.closest('div > div:not([class]) > div[class] > div:not([class]) > div[class]').style.borderColor = 'red'
  }
})

It manages to highlight just the Sponsored post (remember editing name of the text element depending on your language). image

arye321 commented 2 years ago

This was not working for me, as it selects the entire div for where all the other posts live as well, I edited your selector to get the parent div the Sponsor post, it got dirty but it does the job, probably someone else can simplify it and integrate it with the extension:

Right, they changed it a couple of days later, you can see my new method here:

https://github.com/arye321/fbab/blob/master/main.js

    useElements[len].closest(`div[class=""]`).hidden = true