Greasy Fork is available in English.

Facebook Adblocker

Block all ads in Facebook News Feed

Verze ze dne 26. 09. 2019. Zobrazit nejnovější verzi.

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