DEV Community

Parth Chokshi
Parth Chokshi

Posted on • Originally published at Medium on

Remove all advertisers and targeted ads from facebook in 2 minutes

Photo by Thought Catalog on Unsplash

User – Hey Facebook, what do you do for living?

Facebook – We curate people’s News Feed based on what they like, what information they share with us in their profile, what websites they visit – where that website gives us money to follow the users 24 X 7, and more.

User – How can I stop that? Can I remove all that targeted ads and advertisers?

Facebook – Yes, now you can. Just remove all advertisers from your profile — one by one.

User – But I have them in thousands.

Facebook – I can’t hear you.

Yes, some things are made difficult deliberately to discourage you from doing it. You can’t delete all the advertisers once in for all by just clicking one button. But we can make that happen by running just one line of code.

Steps below work for both sections:

1) Who use a contact list added to Facebook.

2) Whose website or app you’ve used

Here’s how to do it:

Step 1: Go to Ads section in your profile:

Step 2: Click on Advertisers tab

Step 3: Right click on first Advertiser and click inspect element.

If you want to remove all the advertisers, you have to bring them in the browser view. For that, keep clicking See more until all the advertisers have loaded and then you can continue the following steps. In case, you just want to give it a shot. You don’t have to do anything.

If you are lazy like me and don’t even want to keep clicking “See more” hundred times, here’s the code for that too.

This will invoke click on “See more” for 50 times. If you want to trigger click more than 50 times, replace 50 with some bigger number.

[...new Array(50)].forEach(() => document.querySelector('#interacted > div > div._2qor._2pic > div:nth-child(1) > div._dfo > div._45yq._5946 > div > div').click());

Step 4: Clicking inspect will open a section at the bottom or on the side of your browser.

Now head over to console section in that menu. You will see a big STOP sign. Ignore it (Trust me 😃). Just execute this code:

document.querySelectorAll(
    '#interacted > div > div._2qor._2pic > div:nth-child(1) > div._dfo > div._45ys > div._2b2e > div._3viq > div._2b2h > button > div._2b2n'
  )
  .forEach(advSection => advSection.click())

This should remove all advertisers. This may freeze your screen for sometime.

Here is the code to add them back again:

document.querySelectorAll(
    '#interacted > div > div._2qor._2pic > div:nth-child(1) > div._dfo > div._45ys > div._2b2e > div._3viq > div._2b2h > button > div._2b2m'
  )
  .forEach(advSection => advSection.click())

The above code should work. If you are curious to know how we formed that code and just in case the above doesn’t work for you. Keep following to know the steps to form that code.

If the above code doesn’t work then may be it is because of the class names, it seems it could be different for you. So, you need to write code specific to your machine. Notice that whole Advertisers sections follows same pattern of naming (<div class="_2b2e">) each section. I have tagged that as Parent Element.

Toggle the Parent Element section to hide the code and you should see something like below. As you can see, all individual advertiser section have same code, only the data in it is different.

NOTE: You may have different class value(_2b2e in my case) from me probably. But don’t worry, follow the steps with whatever values you have. It is all good until all the sections have same class values.

Now, the main part. We need to trigger click on that remove button which only appears when we hover our mouse on each Advertiser section. Let’s locate that in code. You can click the first Parent element and keep opening the code until you find that remove button.

Let’s do the coding now. Right click on the first Parent element and go to Copy and then Copy Selector. After clicking console tab, you might see a big STOP sign with warning. Ignore that. (Trust me!). Now if you paste the code, it will look something like this. The values starting with dot(.) could be different for you.

#interacted > div > div._2qor._2pic > div:nth-child(1) > div._dfo > div._45ys > div:nth-child(1)

Replace the last part of code which says — ‘:nth-child(1)’ with the class value of ‘ Parent Element ’. In my case, it is _2b2e. So I would replace it with ‘._2b2e’. Don’t forget to add a dot(.) before. It will look something like this:

#interacted > div > div._2qor._2pic > div:nth-child(1) > div._dfo > div._45ys > div._2b2e

This gives us code to reach to the Parent element but we need to reach to the Remove button and hit click on that. For that we need to go down each level and get the class value of all the elements to form the code to execute. All target values are marked with red arrows.

Let’s form the code. If you have different class names, code accordingly. After the parent element (div._2b2e), keep adding angular bracket and the class values with ‘div.’ prepended to it. And put that value in document.querySelectorAll() and add a for loop to trigger click on all. Something like this:

document.querySelectorAll(
    '#interacted > div > div._2qor._2pic > div:nth-child(1) > div._dfo > div._45ys > div._2b2e > div._3viq > div._2b2h > button > div._2b2n'
).forEach(advSection => advSection.click());

That’s it. It should remove all the Advertisers.

Wait, what if I want to add them back again. Locate the remove button again.

You can see the value _2b2n is replaced with _2b2m. Just replace the class in the above code and execute it again.

document.querySelectorAll(
'#interacted > div > div._2qor._2pic > div:nth-child(1) > div._dfo > div._45ys > div._2b2e > div._3viq > div._2b2h > button > div._2b2m'
).forEach(advSection => advSection.click());

This should add the advertisers back.

Moreover, you can got to Ad Settings and choose ‘Not allowed’ in all sections to further stop the curation and targeted ads.

I am curious to know if you have different class names. Let me know in the comment section if they are and also if you are facing any issues. Have fun!

Check out my another blog to unlike all facebook pages at once.

Unlike all pages on facebook in 2 minutes

Top comments (0)