Facebook Adblocker

Block all ads in Facebook News Feed.

目前為 2020-12-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            Facebook Adblocker
// @name:vi         Facebook Adblocker
// @namespace       https://lelinhtinh.github.io
// @description     Block all ads in Facebook News Feed.
// @description:vi  Chặn quảng cáo được tài trợ trên trang chủ Facebook.
// @version         1.3.0
// @icon            https://i.imgur.com/F8ai0jB.png
// @author          lelinhtinh
// @oujs:author     baivong
// @license         MIT; https://baivong.mit-license.org/license.txt
// @match           https://facebook.com/*
// @match           https://*.facebook.com/*
// @noframes
// @supportURL      https://github.com/lelinhtinh/Userscript/issues
// @run-at          document-idle
// @grant           none
// ==/UserScript==

(function () {
  'use strict';

  let adsCount = 0;
  let labelStore = [];
  let observerLabel;
  let observerStory;
  let observerHead;
  let isWatch;

  const findAds = (wrapper) => {
    function removeAds() {
      if (!labelStore.length) return;
      const labelId = labelStore.pop();

      const adsLabel = wrapper.querySelector('span[aria-labelledby="' + labelId + '"][class]');
      if (adsLabel === null) return;

      const adsWrap = adsLabel.closest(isWatch ? 'div:not([class*=" "])' : '[data-pagelet^="FeedUnit"]');
      adsWrap.remove();
      // adsWrap.style.opacity = 0.1;

      console.log(++adsCount, 'adsCount');
      removeAds();
    }
    removeAds();

    const watchLabel = (labelHidden) => {
      if (observerLabel) return;
      observerLabel = new MutationObserver((mutationsList) => {
        for (let mutation of mutationsList) {
          if (
            mutation.type === 'attributes' &&
            mutation.attributeName === 'id' &&
            /(Được\s+tài\s+trợ|Sponsored)/i.test(mutation.target.textContent.trim())
          ) {
            labelStore.push(mutation.target.id);
            removeAds();
          }
        }
      });
      observerLabel.observe(labelHidden, {
        attributes: true,
        attributeFilter: ['id'],
        subtree: true,
      });
    };

    const labelHidden = document.querySelector('[hidden="true"]');
    if (labelHidden === null) {
      if (observerLabel) {
        observerLabel.disconnect();
        observerLabel = null;
      }
    } else {
      watchLabel(labelHidden);
    }
  };

  const init = () => {
    isWatch = location.pathname === '/watch';
    const newsFeed = document.querySelector('[role="feed"], [data-pagelet="MainFeed"]');
    if (newsFeed === null) return;

    if (observerStory) observerStory.disconnect();
    observerStory = new MutationObserver((mutationsList) => {
      for (let mutation of mutationsList) {
        findAds(mutation.target);
      }
    });
    observerStory.observe(newsFeed, {
      attributes: false,
      childList: true,
      subtree: true,
    });

    findAds(document);
  };

  init();

  if (observerHead) observerHead.disconnect();
  observerHead = new MutationObserver(init);
  observerHead.observe(document.head, {
    attributes: true,
    childList: true,
    subtree: true,
  });

  (function (old) {
    window.history.pushState = function () {
      old.apply(window.history, arguments);
      init();
    };
  })(window.history.pushState);
})();