Skip to content

Instantly share code, notes, and snippets.

@pjpscriv
Last active December 21, 2024 19:02
Show Gist options
  • Save pjpscriv/d67bf0088bd6e94da4a3c2a043a0efd7 to your computer and use it in GitHub Desktop.
Save pjpscriv/d67bf0088bd6e94da4a3c2a043a0efd7 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name LinkedIn Feed Ads Remover
// @namespace https://pjpscriv.com/
// @version 2024-21
// @description Remove Promoted and Suggested posts on LinkedIn.
// @author @pjpscriv
// @match https://www.linkedin.com/*
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/LinkedIn_icon.svg/72px-LinkedIn_icon.svg.png
// @grant none
// ==/UserScript==
(function() {
'use strict';
const INTERVAL = 100;
const ADD_REPLACEMENTS = false;
const SELECTOR_1 = 'span.text-body-xsmall.t-black--light.update-components-actor__description span[aria-hidden]'; // Description
const SELECTOR_2 = 'span.text-body-xsmall.t-black--light.update-components-actor__sub-description span[aria-hidden]'; // Sub-description
const SELECTOR_3 = 'div.update-components-header__text-wrapper > span.update-components-header__text-view'; // Header text
function getReplacementHtml(message) {
return `
<div class="relative">
<div class="ember-view occludable-update">
<div class="full-height">
<div class="full-height">
<div class="feed-shared-update-v2 feed-shared-update-v2--minimal-padding full-height relative artdeco-card">
<div style="padding: 0.5em 1em;" class="t-black--light">${message}</div>
</div>
</div>
</div>
</div>
</div>`;
}
// Remove the post a node belongs to
function removePost(node, message = "") {
var temp = node;
var atNames = Array.from(temp.attributes).map(attr => attr.name);
while (!atNames.includes('data-id')) {
temp = temp.parentNode;
atNames = Array.from(temp.attributes).map(attr => attr.name);
}
if (message === "") {
temp.parentNode.remove();
} else {
temp.innerHTML = getReplacementHtml(message);
}
}
// Run Loop
setInterval(() => {
var promoNodes = Array.from(
document.querySelectorAll(`${SELECTOR_1}, ${SELECTOR_2}`).values()
.filter(x => x.innerText === "Promoted" || x.innerText.startsWith("Promoted by"))
);
if (promoNodes.length > 0) {
console.log(`Found ${promoNodes.length} Promoted posts to delete`);
promoNodes.forEach(n => removePost(n, ADD_REPLACEMENTS ? "Promoted post removed ✨" : ""));
}
var suggNodes = Array.from(
document.querySelectorAll(SELECTOR_3).values()
.filter(x => x.innerText === "Suggested")
);
if (suggNodes.length > 0) {
console.log(`Found ${suggNodes.length} Suggested posts to delete`);
suggNodes.forEach(n => removePost(n, ADD_REPLACEMENTS ? "Suggested post removed ☺️" : ""));
}
}, INTERVAL)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment