Facebook Adblocker

Block all ads in Facebook News Feed.

21.12.2020 itibariyledir. En son verisyonu görün.

  1. // ==UserScript==
  2. // @name Facebook Adblocker
  3. // @name:vi Facebook Adblocker
  4. // @namespace https://lelinhtinh.github.io
  5. // @description Block all ads in Facebook News Feed.
  6. // @description:vi Chặn quảng cáo được tài trợ trên trang chủ Facebook.
  7. // @version 1.3.1
  8. // @icon https://i.imgur.com/F8ai0jB.png
  9. // @author lelinhtinh
  10. // @oujs:author baivong
  11. // @license MIT; https://baivong.mit-license.org/license.txt
  12. // @match https://facebook.com/*
  13. // @match https://*.facebook.com/*
  14. // @noframes
  15. // @supportURL https://github.com/lelinhtinh/Userscript/issues
  16. // @run-at document-idle
  17. // @grant none
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. let adsCount = 0;
  24. let labelStore = null;
  25. let observerLabel;
  26. let observerStory;
  27. let observerHead;
  28. let isWatch;
  29.  
  30. const removeAd = (adsLabel) => {
  31. const adsWrap = adsLabel.closest(isWatch ? 'div:not([class*=" "])' : '[data-pagelet^="FeedUnit"]');
  32. // adsWrap.style.opacity = 0.1;
  33. adsWrap.remove();
  34. console.log(++adsCount, 'adsCount');
  35. };
  36.  
  37. const findAds = (wrapper) => {
  38. function pickAds() {
  39. if (labelStore instanceof Array) {
  40. if (!labelStore.length) return;
  41. const labelId = labelStore.pop();
  42.  
  43. const adsLabel = wrapper.querySelector('span[aria-labelledby="' + labelId + '"][class]');
  44. if (adsLabel === null) return;
  45.  
  46. removeAd(adsLabel);
  47. pickAds();
  48. } else {
  49. const adsLabels = wrapper.querySelectorAll('a[aria-label="Sponsored"], a[aria-label="Được tài trợ"]');
  50. if (!adsLabels.length) return;
  51.  
  52. adsLabels.forEach(removeAd);
  53. }
  54. }
  55. pickAds();
  56.  
  57. const watchLabel = (labelHidden) => {
  58. if (observerLabel) return;
  59. observerLabel = new MutationObserver((mutationsList) => {
  60. for (let mutation of mutationsList) {
  61. if (
  62. mutation.type === 'attributes' &&
  63. mutation.attributeName === 'id' &&
  64. /(Được\s+tài\s+trợ|Sponsored)/i.test(mutation.target.textContent.trim())
  65. ) {
  66. labelStore.push(mutation.target.id);
  67. pickAds();
  68. }
  69. }
  70. });
  71. observerLabel.observe(labelHidden, {
  72. attributes: true,
  73. attributeFilter: ['id'],
  74. subtree: true,
  75. });
  76. };
  77.  
  78. const labelHidden = document.querySelector('[hidden="true"]');
  79. if (labelHidden === null) {
  80. labelStore = null;
  81. if (observerLabel) {
  82. observerLabel.disconnect();
  83. observerLabel = null;
  84. }
  85. } else {
  86. labelStore = [];
  87. watchLabel(labelHidden);
  88. }
  89. };
  90.  
  91. const init = () => {
  92. isWatch = location.pathname === '/watch';
  93. const newsFeed = document.querySelector('[role="feed"], [data-pagelet="MainFeed"]');
  94. if (newsFeed === null) return;
  95.  
  96. if (observerStory) observerStory.disconnect();
  97. observerStory = new MutationObserver((mutationsList) => {
  98. for (let mutation of mutationsList) {
  99. findAds(mutation.target);
  100. }
  101. });
  102. observerStory.observe(newsFeed, {
  103. attributes: false,
  104. childList: true,
  105. subtree: true,
  106. });
  107.  
  108. findAds(document);
  109. };
  110.  
  111. init();
  112.  
  113. if (observerHead) observerHead.disconnect();
  114. observerHead = new MutationObserver(init);
  115. observerHead.observe(document.head, {
  116. attributes: true,
  117. childList: true,
  118. subtree: true,
  119. });
  120.  
  121. (function (old) {
  122. window.history.pushState = function () {
  123. old.apply(window.history, arguments);
  124. init();
  125. };
  126. })(window.history.pushState);
  127. })();