* Guide to write the extension to block facebook ads in 5pfor everyone who knows how to use facebook on a laptop Withonly about10 lines of code, you can create your own Facebook ad blocking extension
1. Begin
chrome_developers extension – Get familiar with the code
create 1 folder [facebook_block_ads]
Create a manifest.json file in the directory
create a content.js file in the directory
# directory structure
facebook_block_ads
– manifest.json
– content.js
2. What does Facebook_block_ads need?
is as simple as deleting posts with sponsored | sponsored posts
It is also possible to replace the post position with what you want
3. What does the extension need?
1 File manifest.json to declare, 1 File content.js to delete articles with ads
What do we declare in manifest.json
{ "name": "Getting Started Example", "version": "1.0", "description": "Build an Extension!", "content_scripts": [ { "matches": ["https://*.facebook.com/*"], "js": ["content.js"], "run_at": "document_end" }], "manifest_version": 2 }
Explain a bit about content_scripts declaration
– matches: declare the url in which the content.js file can function
– js: declare file content.js 🙂
Idea for writing content.js files
– Retrieve all articles currently displayed on the screen
– find articles with [Sponsored] advertising content
– Delete those posts
What is in the content.js file
window.onscroll = function (e) { // called when the window is scrolled. let elements = document.querySelectorAll("[data-pagelet='FeedUnit_{n}']"); elements.forEach(function (ele, i) { if (elements[i].innerHTML.indexOf("Được tài trợ") !== -1) { elements[i].remove(); } }) }
# explain a bit about the content.js file
let elements = document.querySelectorAll (“[data-pagelet = ‘FeedUnit_ {n}’]”); // find all posts
elements.forEach (function (ele, i) {… // loop to cycle through posts
if (elements [i] .innerHTML.indexOf (“Sponsored”)! == -1) {… // find the post containing the advertisement
elements [i] .remove (); // delete those posts
window.onscroll // detected scrolling
Link: https://github.com/dangtinh-dev/extensions/tree/master/block-ads-facebook?