Cnbeta Remove AD

原版存在性能问题

  1. // ==UserScript==
  2. // @name Cnbeta Remove AD
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 原版存在性能问题
  6. // @author yyy
  7. // @match https://www.cnbeta.com/articles/*.htm
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13. function addMutationObserver(selector, callback) {
  14. let timer;
  15. var watch = document.querySelector(selector);
  16. if (!watch) return;
  17.  
  18. var observer = new MutationObserver(function (mutations) {
  19. mutations.forEach(function (m) {
  20. if (m.addedNodes.length > 0) {
  21. clearTimeout(timer)
  22. timer = setTimeout(callback, 100)
  23. }
  24. });
  25. });
  26. observer.observe(watch, { childList: true, subtree: true });
  27. }
  28.  
  29. var clearAd = function clearAd() {
  30. var adNode = document.querySelector('a[href$="/articles/3.htm"]');
  31. if (adNode) {
  32. var adContainer = adNode.parentNode.parentNode.parentNode;
  33. var mainContainer = adContainer.parentNode;
  34. mainContainer.removeChild(adContainer);
  35. document.body.style = "";
  36. }
  37. };
  38.  
  39. addMutationObserver('body', () => {
  40. // console.log(111)
  41. clearAd()
  42. })
  43. })();