LinkedIn unsponsored

Block sponsored posts in the LinkedIn feed

  1. // ==UserScript==
  2. // @name LinkedIn unsponsored
  3. // @namespace https://jacobbundgaard.dk
  4. // @version 1.3
  5. // @description Block sponsored posts in the LinkedIn feed
  6. // @match https://www.linkedin.com/feed/*
  7. // @grant none
  8. // @inject-into content
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Selectors
  15. const storySelector = '.feed-shared-update-v2';
  16. const descriptionSelector = '.feed-shared-actor__description, .feed-shared-actor__sub-description';
  17.  
  18. // Search strings
  19. const searchStrings = {
  20. 'da': ['Promoveret'],
  21. 'en': ['Promoted'],
  22. 'es': ['Promocionado'],
  23. 'fr': ['Post sponsorisé']
  24. };
  25. const language = searchStrings.hasOwnProperty(document.documentElement.lang) ? document.documentElement.lang : 'en';
  26.  
  27. function blockSponsoredPosts() {
  28. const stories = document.querySelectorAll(storySelector);
  29. for (const story of stories) {
  30. if (story.style.display == 'none') {
  31. continue;
  32. }
  33.  
  34. const descriptions = story.querySelectorAll(descriptionSelector);
  35. for (const description of descriptions) {
  36.  
  37. const descriptionContent = description.innerText.trim();
  38. if (searchStrings[language].find(searchString => searchString == descriptionContent)) {
  39.  
  40. console.debug('Blocked sponsored story', story);
  41. story.style.display = 'none';
  42. }
  43. }
  44. }
  45. }
  46.  
  47. const observer = new MutationObserver(blockSponsoredPosts);
  48. observer.observe(document.body, {
  49. 'childList': true,
  50. 'subtree': true
  51. });
  52. blockSponsoredPosts();
  53. })();