Block Instagram Suggested Posts/Sponsored

Block Instagram posts with "Follow", "Suggested for you", or "Suggested posts" or "Sponsored"

  1. // ==UserScript==
  2. // @name Block Instagram Suggested Posts/Sponsored
  3. // @namespace https://github.com/rowhanm
  4. // @version 0.0.1
  5. // @description Block Instagram posts with "Follow", "Suggested for you", or "Suggested posts" or "Sponsored"
  6. // @author Rohan M
  7. // @match https://www.instagram.com/*
  8. // @grant none
  9. // @license MIT
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. window.addEventListener('load', function() {
  14. // Function to hide posts that contain "Follow", "Suggested for you", or "Suggested posts"
  15. function hidePostsWithFollowOrSuggested() {
  16. const posts = document.querySelectorAll('article');
  17.  
  18. posts.forEach(post => {
  19. // Check for "Follow" in divs
  20. const followDivs = post.querySelectorAll('div');
  21. let followFound = false;
  22. followDivs.forEach(div => {
  23. if (div.innerText.trim().toLowerCase() === 'follow' || (div.innerText.trim().toLowerCase() === 'sponsored')) {
  24. followFound = true;
  25. }
  26. });
  27.  
  28. // Check for "Suggested for you" or "Suggested posts" in spans, divs, sections, and headers
  29. let suggestedFound = false;
  30. const textElements = post.querySelectorAll('span, div, section, h2');
  31. textElements.forEach(el => {
  32. const text = el.innerText.trim().toLowerCase();
  33. if (text.includes('suggested for you') || text.includes('suggested posts')) {
  34. suggestedFound = true;
  35. }
  36. });
  37.  
  38. // Hide the post but keep the layout intact to avoid scroll issues
  39. if (followFound || suggestedFound) {
  40. post.style.visibility = 'hidden'; // Makes the post invisible
  41. post.style.height = '0px'; // Reduces the height to avoid layout disruption
  42. post.style.overflow = 'hidden'; // Prevents any overflow content from being visible
  43. }
  44. });
  45. }
  46.  
  47. // MutationObserver to track changes in the feed (for infinite scroll)
  48. function initializeMutationObserver() {
  49. // Select the main content area (usually where Instagram loads posts)
  50. const mainContent = document.querySelector('main');
  51.  
  52. if (mainContent) {
  53. const observer = new MutationObserver(mutations => {
  54. mutations.forEach(mutation => {
  55. mutation.addedNodes.forEach(node => {
  56. console.log('New node detected:', node); // Log new nodes
  57. if (node.nodeType === 1 && node.tagName === 'ARTICLE') {
  58. console.log('New article added:', node); // Log when a new post is added
  59. hidePostsWithFollowOrSuggested(); // Check newly added content
  60. }
  61. });
  62. });
  63. });
  64.  
  65. // Start observing the main content area for dynamically loaded posts
  66. observer.observe(mainContent, {
  67. childList: true, // Track when new child nodes are added
  68. subtree: true // Check the entire subtree (for infinite scroll)
  69. });
  70. } else {
  71. console.log('Main content area not found.');
  72. }
  73. }
  74.  
  75. // Initial run to hide already existing posts once the page fully loads
  76. hidePostsWithFollowOrSuggested();
  77. initializeMutationObserver();
  78.  
  79. });