去除rule34.xxx广告

自动从网页中删除指定的元素

  1. // ==UserScript==
  2. // @name 去除rule34.xxx广告
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 自动从网页中删除指定的元素
  6. // @author 通义千问2.5
  7. // @match https://rule34.xxx/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // 定义要删除的元素的选择器
  15. const selectorsToRemove = [
  16. 'div.a_list#lmid',
  17. 'div[style="display: inline-flex; vertical-align: top;"]',
  18. 'div.horizontalFlexWithMargins[style="justify-content: center"][data-nosnippet]',
  19. 'div.exo-native-widget-outer-container',
  20. 'span[data-nosnippet]'
  21. ];
  22.  
  23. // 删除匹配选择器的元素
  24. function removeElements(selectors) {
  25. selectors.forEach(selector => {
  26. const elements = document.querySelectorAll(selector);
  27. elements.forEach(element => {
  28. console.log(`删除了元素:`, element);
  29. element.remove();
  30. });
  31. });
  32. }
  33.  
  34. // 初始化脚本
  35. function init() {
  36. console.log('脚本已启动');
  37.  
  38. // 首次运行,处理现有元素
  39. removeElements(selectorsToRemove);
  40.  
  41. // 监听DOM变化,以便动态添加的元素也能被处理
  42. const observer = new MutationObserver((mutations) => {
  43. mutations.forEach(mutation => {
  44. mutation.addedNodes.forEach(node => {
  45. if (node.nodeType === Node.ELEMENT_NODE) {
  46. removeElements(selectorsToRemove);
  47. }
  48. });
  49. });
  50. });
  51.  
  52. observer.observe(document.body, { childList: true, subtree: true });
  53.  
  54. console.log('DOM观察器已启动');
  55. }
  56.  
  57. // 确保在页面加载完成后初始化
  58. if (document.readyState === 'complete' || document.readyState === 'interactive') {
  59. init();
  60. } else {
  61. window.addEventListener('load', init);
  62. }
  63. })();