-
-
Save masterchop/25deebc57e0b4af9ffe85df283562e54 to your computer and use it in GitHub Desktop.
User script used in TamperMonkey to remove ads in Facebooks' news feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Hide sponsored items | |
// @namespace whatisthis | |
// @version 0.1 | |
// @description try to sanitize Facebook | |
// @author Sy Hung Doan | |
// @match https://www.facebook.com/* | |
// @match https://facebook.com/* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
// src: https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf | |
const throttle = (func, limit) => { | |
let lastFunc; | |
let lastRan; | |
return function () { | |
const context = this; | |
const args = arguments; | |
if (!lastRan) { | |
func.apply(context, args); | |
lastRan = Date.now(); | |
} else { | |
clearTimeout(lastFunc); | |
lastFunc = setTimeout(function () { | |
if (Date.now() - lastRan >= limit) { | |
func.apply(context, args); | |
lastRan = Date.now(); | |
} | |
}, limit - (Date.now() - lastRan)); | |
} | |
}; | |
}; | |
let lastCommitIndex = 0; | |
let unitsContainer = document.querySelector("div[role='feed']"); | |
const callback = () => { | |
unitsContainer = document.querySelector("div[role='feed']"); | |
if (!unitsContainer) { | |
return; | |
} | |
const feedUnits = Array.from( | |
document.querySelectorAll( | |
`div[role='feed'] > div[data-pagelet^='FeedUnit_']:nth-child(n+${lastCommitIndex})` | |
) | |
); | |
if (!feedUnits.length) { | |
return; | |
} | |
Array(feedUnits.length) | |
.fill(0) | |
.forEach((_, i, obj) => { | |
const unit = feedUnits[i]; | |
if (unit.querySelector("[aria-label='Sponsored']")) { | |
unitsContainer.removeChild(unit); | |
obj.splice(i, 1); | |
} else { | |
lastCommitIndex = i; | |
} | |
}); | |
}; | |
const callbackThrottled = throttle(callback, 1000); | |
const observer = new MutationObserver(callbackThrottled); | |
observer.observe(unitsContainer, { childList: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment