Skip to content

Instantly share code, notes, and snippets.

@mottosso
Last active January 3, 2024 10:07
Show Gist options
  • Save mottosso/9d4eb6a075d5a0edd18f437584b1e44d to your computer and use it in GitHub Desktop.
Save mottosso/9d4eb6a075d5a0edd18f437584b1e44d to your computer and use it in GitHub Desktop.
LinkedIn Ad-blocker

On LinkedIn, some of the posts in your "Feed" are ads. They carry a little indicator (by law) to tell you as much. This script looks for those indicators and hides it for you. It works by installing an event listener for when you scroll, and looks for newly promoted posts to hide.

Usage

Copy/paste the above script into your Console when visiting LinkedIn. You can use an extension to run the above script whenever visiting LinkedIn, such as "Custom JavaScript for Websites 2"


Changes

2023.10.31

Updated to handle "Promoted by ..." junk

image

/**
* Hide ads on LinkedIn
*
* This finds any mention of "Promoted" and hides the parent card
* whenever the user scrolls. Effectively hiding every possible ad.
*
*/
// Avoid triggering too frequently
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Document/scroll_event
let ticking = false;
function hidePromoted() {
var spans = document.querySelectorAll('span[aria-hidden="true"]');
for (var i=0; i<spans.length; i++) {
var span = spans[i];
if (span.textContent.startsWith("Promoted")) {
// The post itself exists 10 levels above this "Promoted" indicator
for (var j=0; j < 10; j++) span = span.parentElement;
console.log("Hiding garbage Promoted material");
span.remove()
}
}
}
document.addEventListener("scroll", (event) => {
if (!ticking) {
window.requestAnimationFrame(() => {
hidePromoted();
ticking = false;
});
ticking = true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment