IThome Fix

去除「IT之家」博客版信息流广告

当前为 2024-02-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IThome Fix
  3. // @namespace http://your-namespace.com
  4. // @version 3.1
  5. // @description 去除「IT之家」博客版信息流广告
  6. // @author https://blog.tongmingzhi.com
  7. // @match https://www.ithome.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Function to set rounded images with proportional height and a border
  15. function setRoundedImages() {
  16. var images = document.querySelectorAll('img');
  17. images.forEach(function(image) {
  18. if (image.width >= 30) {
  19. image.parentElement.style.border = 'none';
  20. image.style.borderRadius = '12px';
  21. image.style.border = '2px solid #CCC';
  22.  
  23. if (image.height > 200) {
  24. image.style.borderRadius = '12px';
  25. image.style.border = '2px solid #CCC';
  26. image.style.display = 'inline';
  27. image.style.maxWidth = '400px';
  28. image.style.height = 'auto';
  29. image.style.objectFit = 'cover';
  30. image.style.overflow = 'hidden';
  31. }
  32. }
  33. });
  34. }
  35.  
  36. // Function to set rounded corners for comments
  37. function setRounded() {
  38. var roundeds = document.querySelectorAll(
  39. '.comm_list ul.list li.entry ul.reply, ' +
  40. '.content .post_content blockquote, ' +
  41. '.add_comm input#btnComment' +
  42. '.card, span.card'
  43. );
  44. roundeds.forEach(function (rounded) {
  45. rounded.style.borderRadius = '12px';
  46. });
  47.  
  48. var addCommElements = document.querySelectorAll('.add_comm');
  49. addCommElements.forEach(function (addCommElement) {
  50. addCommElement.style.borderRadius = '0px 0px 12px 12px';
  51. });
  52.  
  53. var Cards = document.querySelectorAll('.card, span.card');
  54. Cards.forEach(function (card) {
  55. card.style.transform = 'scale(0.8)';
  56. });
  57. }
  58.  
  59. // Function to remove ads
  60. function removeAds() {
  61. document.querySelectorAll(
  62. 'div.bb.clearfix > div.fl > ul.bl > li'
  63. ).forEach(function(element) {
  64. if (element.querySelector('div.c > div.m:empty')) {
  65. element.remove();
  66. }
  67. });
  68. }
  69.  
  70. // Function to observe DOM changes
  71. function observeDOM() {
  72. var targetNode = document.body;
  73. var config = { childList: true, subtree: true };
  74.  
  75. var callback = function(mutationsList, observer) {
  76. for (var mutation of mutationsList) {
  77. if (mutation.type === 'childList') {
  78. setRoundedImages();
  79. setRounded();
  80. removeAds();
  81. }
  82. }
  83. };
  84.  
  85. var observer = new MutationObserver(callback);
  86. observer.observe(targetNode, config);
  87.  
  88. return observer;
  89. }
  90.  
  91. function checkDOMChanges() {
  92. var observer = observeDOM();
  93.  
  94. var interval = setInterval(function() {
  95. setRoundedImages();
  96. setRounded();
  97. removeAds();
  98. }, 20);
  99.  
  100. return interval;
  101. }
  102.  
  103. window.addEventListener('load', function() {
  104. setRoundedImages();
  105. setRounded();
  106. removeAds();
  107.  
  108. var intervalID = checkDOMChanges();
  109.  
  110. setTimeout(function() {
  111. clearInterval(intervalID);
  112. }, 60000);
  113. });
  114. })();