Skip to content

Instantly share code, notes, and snippets.

@miglen
Last active July 4, 2022 12:19
Show Gist options
  • Save miglen/4f1bccf15b63944675d34149dff0bc3d to your computer and use it in GitHub Desktop.
Save miglen/4f1bccf15b63944675d34149dff0bc3d to your computer and use it in GitHub Desktop.
News Feed Eradicator for LinkedIn
// ==UserScript==
// @name News Feed Eradicator for LinkedIn
// @namespace http://miglen.com/
// @version 0.5
// @description News Feed Eradicator for LinkedIn
// @author Miglen Evlogiev (hi@miglen.com)
// @match https://www.linkedin.com/*
// @grant none
// @downloadURL https://gist.github.com/miglen/4f1bccf15b63944675d34149dff0bc3d/raw/news-feeds-eradicator-linkedin.user.js#.user.js
// @updateURL https://gist.github.com/miglen/4f1bccf15b63944675d34149dff0bc3d/raw/news-feeds-eradicator-linkedin.user.js#.user.js
// ==/UserScript==
/**
Similiar to the News Feed Eradicator for LinkedIn it's just not
a separate extention but a Grease Monkey Script.
Installation instructions:
1. Install the latest version of Tamper Monkey: https://tampermonkey.net
2. Click on Raw on the current file, your browser should
detect the script and allow you to install it,
otherwise copy the download url above and paste in your browser.
*/
(function() {
'use strict';
// Set the main div containing the news feeds
var core_rail = document.getElementById('main');
function addCss(css) {
var head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
// This is required for IE8 and below.
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}
// Hide a css selector element
function hideElement(css_selector) {
addCss(`${css_selector} {display:none;visibility:hidden;}`);
}
function applicationContentMutation(mutation) {
if (!window.location.pathname.startsWith("/feed")) {
return true;
}
// Placeholder text, change it to whatever you want
var div = `
<div style="padding:30px;">
<h3>Don't "work harder." Instead:</h3>
<br>
<ul>
<li>Cut away distractions. Use social media intentionally.</li>
<li>Stop doing fake work and being busy, focus on the results.</li>
<li>Ask for help & engage your mentors.</li>
<li>Solicit criticism.</li>
<li>Automate what can be automated in tools and processes.</li>
<li>Exercise & Get more sleep. Take care of your self!</li>
</ul>
<br>
<h4>Whatever you do, don't "work harder." It's pretty much never the answer. Work smarter!<h4>
</div>`;
// Replace the news feeds with the placeholder text
core_rail.lastElementChild.innerHTML = div;
}
// Various css selectors of elements to be hidden
var css_selectors = [
'div[data-id^="urn:li:activity:"]', // feed activity
'#feed-nav-item .nav-item__badge--doughnut', // feed notifications
'div.feed-shared-update-v2', // Ads in the feed
'.ad-banner-container', // Ads container
'.feed-shared-navigation-module', // Discover More container
'.feed-shared-navigation-module--v2', // Discover More container
'.feed-follows-module', // Add to your feed container
'.sort-dropdown__dropdown-trigger', // Sort dropdown trigger
'.feed-shared-news-module' //Shared news module
]
// Hide all elements
hideElement(css_selectors.join(', '))
// Set the placeholder text initially
applicationContentMutation();
// Hook into the news feed mutations
const applicationContentObserver = new MutationObserver(function(mutations) {
mutations.forEach(applicationContentMutation);
});
applicationContentObserver.observe(core_rail, {
childList: true
});
})();
@MadManMoon
Copy link

MadManMoon commented Jun 29, 2022

Hey, I can see that the CSS stuff works, but I don't think that the mutationObserver is actually doing anything.

I've slipped the following under the if !window.location:
console.log("no-one here but us chickenz!");

It doesn't fire at all which suggests to me that it's not actually doing anything with elements that come in ... just hiding everything in the CSS (which is great, by the way).

I'm not trying to be on your case, by the way ... I've been struggling to find a way to make mutationObserver do anything on the LinkedIn feed, and I've been losing ... but I'm a real newb.


EDIT

To be clear, I was trying to use your script to enable me to have a feed, but it's just actual activity from contacts that matters (comments, or posts, basically) ... and I couldn't get the mutation observer to fire.

So now that I have this, I'll hopefully find a way to make it much more efficient. Might publish and then ask for help on stack.

Anyway ... this feels really inefficient, but it works ... I'd love to find a way that's a bit more selective.

(function () { 
$("div.scaffold-finite-scroll__content").filter("div a[href^='https://www.linkedin.com/company'][data-control-name='actor_container']").remove();
  
})();

(function () { 
var targetNodes = $( document );
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var myObserver = new MutationObserver(mutationHandler);
var obsConfig = {
  childList: true,
  characterData: true,
  attributes: true,
  subtree: true
};

//--- Add a target node to the observer. Can only add one node at a time.
targetNodes.each(function() {
  myObserver.observe(this, obsConfig);
});

function mutationHandler(mutationRecords) {
  console.info("mutationHandler:");

  mutationRecords.forEach(function(mutation) {
    //console.log("this was a "+mutation.type);
    //console.log("yallseedis: "+mutation);

    if (typeof mutation.addedNodes == "object") {
      $(mutation.addedNodes).filter("[href^='https://www.linkedin.com/company'][data-control-name='actor_container']").closest("div.relative").remove();
      console.log("yooooooooooooo");
    }
  });
}
})();

I couldn't get it to run on #main or the actual container that updates (2 divs in) which is div.scaffold-finite-scroll__content ... plus I also have to ensure that I include the entire minified jquery, because apparently the include doesn't work, and I also have to inject it ... but it works ... no more companies in my feed! (so far)

@miglen do you think there's anything that can be done to trim this? Does it need all the config options that it has? I'm thinking of replacing the add-on which I'm using with this, as it's quite effective on the company part. I could perhaps easily include checks on containing "commented on" or "loves this", etc.

@mthurmond
Copy link

This script inspired me to write a chrome extension that allows you to show or hide your newsfeed, among other things. It's free and open source. Feel free to pull any of the logic into this script, or visa-versa.

https://github.com/mthurmond/distraction-free-for-linkedin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment