图片样式屏蔽器

隐藏所有图片元素,可以用来看网页小说和视频,脚本菜单启用/禁用脚本,也可以通过A键加B键触发

  1. // ==UserScript==
  2. // @name 图片样式屏蔽器
  3. // @version 1.5
  4. // @description 隐藏所有图片元素,可以用来看网页小说和视频,脚本菜单启用/禁用脚本,也可以通过A键加B键触发
  5. // @author ChatGPT
  6. // @match *://*/*
  7. // @grant GM_registerMenuCommand
  8. // @grant GM_unregisterMenuCommand
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @run-at document-start
  12. // @namespace https://greasyfork.org/users/452911
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. const currentHostname = window.location.hostname;
  19. const styleId = 'image-style-blocker';
  20. let isStyleBlocked = GM_getValue(`use_image_style_blocker_${currentHostname}`, false);
  21.  
  22. // 检查并应用图片样式屏蔽器
  23. checkAndApplyImageStyleBlocker();
  24.  
  25. let aKeyPressed = false;
  26. let bKeyPressed = false;
  27.  
  28. // 监听快捷键事件
  29. document.addEventListener('keydown', function(e) {
  30. if (e.key === 'a') {
  31. aKeyPressed = true;
  32. }
  33. if (e.key === 'b') {
  34. bKeyPressed = true;
  35. }
  36. if (aKeyPressed && bKeyPressed) {
  37. e.preventDefault(); // 阻止默认行为
  38. toggleImageStyleBlocker();
  39. aKeyPressed = false; // 重置状态
  40. bKeyPressed = false; // 重置状态
  41. }
  42. });
  43.  
  44. document.addEventListener('keyup', function(e) {
  45. if (e.key === 'a') {
  46. aKeyPressed = false;
  47. }
  48. if (e.key === 'b') {
  49. bKeyPressed = false;
  50. }
  51. });
  52.  
  53. // 动态创建或更新菜单项
  54. function updateMenu() {
  55. GM_unregisterMenuCommand("启用图片样式屏蔽器");
  56. GM_unregisterMenuCommand("禁用图片样式屏蔽器");
  57.  
  58. if (isStyleBlocked) {
  59. GM_registerMenuCommand("禁用图片样式屏蔽器", () => {
  60. GM_setValue(`use_image_style_blocker_${currentHostname}`, false);
  61. isStyleBlocked = false;
  62. removeImageStyleBlocker();
  63. updateMenu(); // 更新菜单项
  64. });
  65. } else {
  66. GM_registerMenuCommand("启用图片样式屏蔽器", () => {
  67. GM_setValue(`use_image_style_blocker_${currentHostname}`, true);
  68. isStyleBlocked = true;
  69. applyImageStyleBlocker();
  70. updateMenu(); // 更新菜单项
  71. });
  72. }
  73. }
  74.  
  75. function checkAndApplyImageStyleBlocker() {
  76. isStyleBlocked = GM_getValue(`use_image_style_blocker_${currentHostname}`, false);
  77. updateMenu(); // 更新菜单项
  78. if (isStyleBlocked) {
  79. applyImageStyleBlocker();
  80. } else {
  81. removeImageStyleBlocker();
  82. }
  83. }
  84.  
  85. function applyImageStyleBlocker() {
  86. let style = document.createElement('style');
  87. style.id = styleId;
  88. style.innerHTML = `img,[style*='height:'][style*='width:'] {display: none !important;visibility: hidden; opacity: 0; z-index: -999; width: 0; height: 0; pointer-events: none; position: absolute; left: -9999px; top: -9999px;}`;
  89. document.head.appendChild(style);
  90. }
  91.  
  92. function removeImageStyleBlocker() {
  93. let style = document.getElementById(styleId);
  94. if (style) {
  95. style.remove();
  96. }
  97. }
  98.  
  99. function toggleImageStyleBlocker() {
  100. isStyleBlocked = !isStyleBlocked;
  101. GM_setValue(`use_image_style_blocker_${currentHostname}`, isStyleBlocked);
  102. if (isStyleBlocked) {
  103. applyImageStyleBlocker();
  104. } else {
  105. removeImageStyleBlocker();
  106. }
  107. updateMenu(); // 更新菜单项
  108. }
  109. })();