HideImage

隐藏所有图片,直到你想看到她

当前为 2024-10-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name HideImage
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @description 隐藏所有图片,直到你想看到她
  6. // @author idealy
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 配置项:最小宽度和高度
  16. const minWidth = 100;
  17. const minHeight = 100;
  18.  
  19. // 字典管理特定域名下的特定类名
  20. const excludeClasses = {
  21. 'linux.do': ['mfp-img'],
  22. 'recaptcha.net':['rc-imageselect-checkbox']
  23. };
  24.  
  25. function shouldExcludeImage(img) {
  26. const domain = window.location.hostname;
  27. if (excludeClasses[domain]) {
  28. return excludeClasses[domain].some(className => img.classList.contains(className));
  29. }
  30. return false;
  31. }
  32.  
  33. function replaceImagesWithText(img) {
  34. if (img.style.display !== 'none' && img.width >= minWidth && img.height >= minHeight && !shouldExcludeImage(img)) {
  35. const width = img.width;
  36. const height = img.height;
  37. const text = document.createElement('span');
  38. text.textContent = `图片 ${width}*${height}`;
  39. text.style.cursor = 'pointer';
  40. text.addEventListener('click', () => {
  41. img.style.display = '';
  42. text.style.display = 'none';
  43. img.addEventListener('click', () => {
  44. img.style.display = 'none';
  45. text.style.display = '';
  46. }, { once: true });
  47. });
  48. img.parentNode.insertBefore(text, img);
  49. img.style.display = 'none';
  50. console.log('已隐藏图片:', img.src);
  51. }
  52. }
  53.  
  54. function handleImage(img) {
  55. if (img.dataset.processed) return;
  56. img.dataset.processed = 'true';
  57.  
  58. console.log('检测到图片:', img.src);
  59.  
  60. if (img.complete) {
  61. replaceImagesWithText(img);
  62. } else {
  63. img.addEventListener('load', () => replaceImagesWithText(img));
  64. img.addEventListener('error', () => replaceImagesWithText(img));
  65. }
  66. }
  67.  
  68. function handleExistingImages() {
  69. const images = document.querySelectorAll('img');
  70. images.forEach(handleImage);
  71. }
  72.  
  73. function observeDOMChanges() {
  74. const observer = new MutationObserver((mutations) => {
  75. mutations.forEach(mutation => {
  76. if (mutation.type === 'childList') {
  77. mutation.addedNodes.forEach(node => {
  78. if (node.nodeType === Node.ELEMENT_NODE) {
  79. if (node.tagName === 'IMG') {
  80. handleImage(node);
  81. } else {
  82. const images = node.querySelectorAll('img');
  83. images.forEach(handleImage);
  84. }
  85. }
  86. });
  87. } else if (mutation.type === 'attributes' && mutation.target.tagName === 'IMG') {
  88. handleImage(mutation.target);
  89. }
  90. });
  91. });
  92.  
  93. observer.observe(document.body, {
  94. childList: true,
  95. subtree: true,
  96. attributes: true,
  97. attributeFilter: ['src']
  98. });
  99. }
  100.  
  101. window.onload = () => {
  102. handleExistingImages();
  103. observeDOMChanges();
  104. };
  105. })();

QingJ © 2025

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