Twitter AD Filter 推特广告过滤

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

  1. // ==UserScript==
  2. // @name Twitter AD Filter 推特广告过滤
  3. // @name:zh-CN Twitter AD Filter 推特广告过滤
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.2
  6. // @description Hide ads in tweets. 隐藏推特中的广告。
  7. // @description:zh-CN Hide ads in tweets. 隐藏推特中的广告。
  8. // @icon https://about.twitter.com/etc/designs/about2-twitter/public/img/favicon-32x32.png
  9. // @author gabe
  10. // @license MIT
  11. // @match https://twitter.com/*
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. "use strict";
  17.  
  18. function log() {
  19. return console.info("[Twitter AD Filter]", ...arguments);
  20. }
  21.  
  22. let i = 0;
  23. function hideAd(node) {
  24. try {
  25. if (
  26. !node ||
  27. node.nodeName !== "DIV" ||
  28. node.getAttribute("data-testid") !== "cellInnerDiv"
  29. ) {
  30. return;
  31. }
  32.  
  33. const el = node.querySelector(
  34. "div[data-testid='placementTracking'] > article"
  35. );
  36. if (!el) {
  37. return;
  38. }
  39.  
  40. const userName = el.querySelector("div[data-testid='User-Name']");
  41. log("hide ad:", ++i, userName && userName.innerText);
  42.  
  43. node.style.cssText += "display: none;";
  44. } catch (err) {
  45. log("got err:", err.message);
  46. }
  47. }
  48.  
  49. const pageObserver = new MutationObserver(function (mutations) {
  50. mutations.forEach(function (mutation) {
  51. mutation.addedNodes.forEach(hideAd);
  52. });
  53. });
  54.  
  55. pageObserver.observe(document.body, {
  56. childList: true,
  57. subtree: true,
  58. });
  59.  
  60. document.querySelectorAll("div[data-testid='cellInnerDiv']").forEach(hideAd);
  61.  
  62. log("--- start ---");
  63. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址