Twitter Ad Remover

Remove promoted tweets and ads from Twitter feed

当前为 2024-12-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter Ad Remover
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Remove promoted tweets and ads from Twitter feed
  6. // @author aspen138
  7. // @match *://twitter.com/*
  8. // @match *://x.com/*
  9. // @grant none
  10. // @license MIT
  11. // @icon https://about.twitter.com/etc/designs/about2-twitter/public/img/favicon-32x32.png
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. function hideAd(node) {
  16. if (
  17. !node ||
  18. node.nodeName !== "DIV" ||
  19. node.getAttribute("data-testid") !== "cellInnerDiv"
  20. ) {
  21. return;
  22. }
  23.  
  24. const adArticle = node.querySelector("div[data-testid='placementTracking'] > article");
  25. if (!adArticle) {
  26. return;
  27. }
  28.  
  29. node.style.cssText += "display: none;";
  30. }
  31.  
  32. // Observe for newly added nodes and hide ads in them
  33. new MutationObserver(function(mutations) {
  34. mutations.forEach(function(mutation) {
  35. mutation.addedNodes.forEach(hideAd);
  36. });
  37.  
  38. // Hide the sidebar ad
  39. const sidebarAd = document.querySelector("#react-root > div > div > div.css-175oi2r.r-1f2l425.r-13qz1uu.r-417010.r-18u37iz > main > div > div > div > div.css-175oi2r.r-aqfbo4.r-10f7w94.r-1hycxz > div > div.css-175oi2r.r-1hycxz.r-gtdqiz > div > div > div > div:nth-child(3) > div > aside");
  40. if (sidebarAd) {
  41. sidebarAd.style.display = 'none';
  42. }
  43. }).observe(document.body, {
  44. childList: true,
  45. subtree: true
  46. });
  47.  
  48. // Initial pass to hide existing ads
  49. document.querySelectorAll("div[data-testid='cellInnerDiv']").forEach(hideAd);
  50.  
  51. function removePromotedTweets(node) {
  52. node = node || document.body;
  53. const tweets = node.querySelectorAll('article[data-testid="tweet"]');
  54.  
  55. tweets.forEach((tweet) => {
  56. const adLabel = tweet.querySelector('div[dir="ltr"] > span');
  57. if (adLabel && (adLabel.textContent === 'Promoted' || adLabel.textContent === 'Ad')) {
  58. tweet.remove();
  59. }
  60. });
  61. }
  62.  
  63. // Initial removal of promoted tweets
  64. removePromotedTweets();
  65.  
  66. // Observe the DOM for changes and remove newly added promoted tweets
  67. new MutationObserver(function(mutations) {
  68. mutations.forEach(function(mutation) {
  69. mutation.addedNodes.forEach(function(node) {
  70. if (node.nodeType === Node.ELEMENT_NODE) {
  71. removePromotedTweets(node);
  72. }
  73. });
  74. });
  75. }).observe(document.body, {
  76. childList: true,
  77. subtree: true
  78. });
  79. })();

QingJ © 2025

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