Greasy Fork

来自缓存

Greasy Fork is available in English.

Twitter AD Filter 推特广告过滤

Hide ads in tweets. 隐藏推特中的广告。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name                  Twitter AD Filter 推特广告过滤
// @name:zh-CN            Twitter AD Filter 推特广告过滤
// @namespace             http://tampermonkey.net/
// @version               0.2
// @description           Hide ads in tweets. 隐藏推特中的广告。
// @description:zh-CN     Hide ads in tweets. 隐藏推特中的广告。
// @icon                  https://about.twitter.com/etc/designs/about2-twitter/public/img/favicon-32x32.png
// @author                gabe
// @license               MIT
// @match                 https://twitter.com/*
// @grant                 none
// ==/UserScript==

(function () {
  "use strict";

  function log() {
    return console.info("[Twitter AD Filter]", ...arguments);
  }

  let i = 0;
  function hideAd(node) {
    try {
      if (
        !node ||
        node.nodeName !== "DIV" ||
        node.getAttribute("data-testid") !== "cellInnerDiv"
      ) {
        return;
      }

      const el = node.querySelector(
        "div[data-testid='placementTracking'] > article"
      );
      if (!el) {
        return;
      }

      const userName = el.querySelector("div[data-testid='User-Name']");
      log("hide ad:", ++i, userName && userName.innerText);

      node.style.cssText += "display: none;";
    } catch (err) {
      log("got err:", err.message);
    }
  }

  const pageObserver = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      mutation.addedNodes.forEach(hideAd);
    });
  });

  pageObserver.observe(document.body, {
    childList: true,
    subtree: true,
  });

  document.querySelectorAll("div[data-testid='cellInnerDiv']").forEach(hideAd);

  log("--- start ---");
})();