Facebook Adblocker

Block all ads in Facebook News Feed

Od 16.10.2019.. Pogledajte najnovija verzija.

  1. // ==UserScript==
  2. // @name Facebook Adblocker
  3. // @namespace https://lelinhtinh.github.io
  4. // @description Block all ads in Facebook News Feed
  5. // @version 1.1.1
  6. // @icon https://i.imgur.com/F8ai0jB.png
  7. // @author lelinhtinh
  8. // @oujs:author baivong
  9. // @license MIT; https://baivong.mit-license.org/license.txt
  10. // @match https://facebook.com/*
  11. // @match https://*.facebook.com/*
  12. // @noframes
  13. // @supportURL https://github.com/lelinhtinh/Userscript/issues
  14. // @run-at document-idle
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. /**
  22. * Logging level
  23. * @type {Number}
  24. */
  25. const DEBUG = 0;
  26.  
  27.  
  28. /* === DO NOT CHANGE === */
  29. let countAds = 0;
  30.  
  31. const config = {
  32. attributes: false,
  33. childList: true,
  34. subtree: true
  35. };
  36.  
  37. const removeAds = wrap => {
  38. if (DEBUG >= 2) console.log(wrap, 'wrapNode');
  39.  
  40. const subtiltes = wrap.querySelectorAll('[data-testid*="story"]:not([role]) a');
  41. if (!subtiltes.length) return;
  42.  
  43. Array.from(subtiltes).forEach(v => {
  44. if (v.textContent.trim().search(/Được tài trợ|Bài viết được đề xuất|Sponsor|Suggest|Recommend/i) !== -1) {
  45. v = v.closest('[data-testid="fbfeed_story"]');
  46. if (DEBUG) console.log(++countAds, 'countAds');
  47. if (DEBUG >= 3) console.log(v.innerHTML, 'htmlAds');
  48. v.remove();
  49. }
  50. });
  51. };
  52.  
  53. let observerStory;
  54. let observerHead;
  55.  
  56. const init = () => {
  57. if (DEBUG) console.log('Facebook Adblocker');
  58. countAds = 0;
  59.  
  60. const newsFeed = document.querySelector('[data-testid="newsFeedStream"]');
  61. if (DEBUG >= 2) console.log(newsFeed, 'newsFeedNode');
  62. if (!newsFeed) return;
  63.  
  64. if (observerStory) observerStory.disconnect();
  65. observerStory = new MutationObserver(mutationsList => {
  66. for (let mutation of mutationsList) {
  67. removeAds(mutation.target);
  68. }
  69. });
  70. observerStory.observe(newsFeed, config);
  71.  
  72. removeAds(document);
  73. };
  74.  
  75. init();
  76.  
  77. if (observerHead) observerHead.disconnect();
  78. observerHead = new MutationObserver(init);
  79. observerHead.observe(document.head, config);
  80.  
  81. }());