Discord 隐藏图片

使图片较为透明。

当前为 2020-05-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Discord: Hide Image
  3. // @name:zh-TW Discord 隱藏圖片
  4. // @name:zh-CN Discord 隐藏图片
  5. // @name:ja Discord 画像を非表示
  6. // @name:ko Discord 이미지 숨기기
  7. // @name:ru Discord Скрыть изображение
  8. // @version 1.0.4
  9. // @description Make images opacity lower.
  10. // @description:zh-TW 使圖片較為透明。
  11. // @description:zh-CN 使图片较为透明。
  12. // @description:ja 画像の不透明度を低くします。
  13. // @description:ko 이미지의 불투명도를 낮추십시오.
  14. // @description:ru Уменьшите непрозрачность изображений.
  15. // @author Hayao-Gai
  16. // @namespace https://github.com/HayaoGai
  17. // @icon https://i.imgur.com/rE9N0R7.png
  18. // @match https://discord.com/channels/*
  19. // @grant GM_getValue
  20. // @grant GM_setValue
  21. // ==/UserScript==
  22.  
  23. /* jshint esversion: 6 */
  24.  
  25. (function() {
  26. 'use strict';
  27.  
  28. // icons made by https://www.flaticon.com/authors/pixel-perfect
  29. const iconOn = `<svg width="35" height="35" viewBox="0 -107 512 512"><path d="m362.667969 298.667969h-213.335938c-82.34375 0-149.332031-67.007813-149.332031-149.335938 0-82.324219 66.988281-149.332031 149.332031-149.332031h213.335938c82.34375 0 149.332031 67.007812 149.332031 149.332031 0 82.328125-66.988281 149.335938-149.332031 149.335938zm-213.335938-266.667969c-64.703125 0-117.332031 52.652344-117.332031 117.332031 0 64.683594 52.628906 117.335938 117.332031 117.335938h213.335938c64.703125 0 117.332031-52.652344 117.332031-117.335938 0-64.679687-52.628906-117.332031-117.332031-117.332031zm0 0"/><path d="m362.667969 234.667969c-47.0625 0-85.335938-38.273438-85.335938-85.335938 0-47.058593 38.273438-85.332031 85.335938-85.332031 47.058593 0 85.332031 38.273438 85.332031 85.332031 0 47.0625-38.273438 85.335938-85.332031 85.335938zm0-138.667969c-29.398438 0-53.335938 23.914062-53.335938 53.332031 0 29.421875 23.9375 53.335938 53.335938 53.335938 29.394531 0 53.332031-23.914063 53.332031-53.335938 0-29.417969-23.9375-53.332031-53.332031-53.332031zm0 0"/></svg>`;
  30. const iconOff = `<svg width="35" height="35" viewBox="0 -107 512 512"><path d="m362.667969 0h-213.335938c-82.324219 0-149.332031 66.988281-149.332031 149.332031 0 82.347657 67.007812 149.335938 149.332031 149.335938h213.335938c82.324219 0 149.332031-66.988281 149.332031-149.335938 0-82.34375-67.007812-149.332031-149.332031-149.332031zm-213.335938 234.667969c-47.058593 0-85.332031-38.273438-85.332031-85.335938 0-47.058593 38.273438-85.332031 85.332031-85.332031 47.0625 0 85.335938 38.273438 85.335938 85.332031 0 47.0625-38.273438 85.335938-85.335938 85.335938zm0 0"/></svg>`;
  31.  
  32. // css
  33. const textStyle = `
  34. .hide-set {
  35. transition: opacity 0.3s;
  36. opacity: 0.1 !important;
  37. }
  38. .switch-set {
  39. fill: white;
  40. text-align: center;
  41. bottom: 10px;
  42. cursor: pointer;
  43. pointer-events: all;
  44. }`;
  45.  
  46. // update
  47. let updating = false;
  48.  
  49. css();
  50.  
  51. init(10);
  52.  
  53. locationChange();
  54.  
  55. window.addEventListener("scroll", update, true);
  56.  
  57. function init(times) {
  58. for (let i = 0; i < times; i++) {
  59. setTimeout(switchButton, 500 * i);
  60. setTimeout(() => getTarget("img:not(.hide-set)"), 500 * i); // all images.
  61. setTimeout(() => getTarget("video:not(.hide-set)"), 500 * i); // all videos ( gifs ).
  62. setTimeout(() => getTarget(".animatedContainer-1pJv5C:not(.hide-set)"), 500 * i); // the image on top of server channel lists.
  63. setTimeout(() => getTarget(".avatarContainer-2inGBK:not(.hide-set)"), 500 * i); // the avatar images in channels.
  64. }
  65. stopHide();
  66. }
  67.  
  68. function switchButton() {
  69. // check exist
  70. const isExist = document.querySelector(".switch-set");
  71. if (!!isExist) return;
  72. // left panel ( parent )
  73. const leftPanel = document.querySelector("nav.wrapper-1Rf91z");
  74. if (!leftPanel) return;
  75. // add switch button
  76. const button = document.createElement("div");
  77. button.className = "unreadMentionsIndicatorBottom-BXS58x switch-set";
  78. button.innerHTML = getToggle() ? iconOn : iconOff;
  79. button.addEventListener("click", onClick);
  80. leftPanel.appendChild(button);
  81. }
  82.  
  83. function getToggle() {
  84. return GM_getValue("switch", true);
  85. }
  86.  
  87. function onClick() {
  88. const afterClick = !getToggle();
  89. GM_setValue("switch", afterClick);
  90. this.innerHTML = afterClick ? iconOn : iconOff;
  91. init(3);
  92. }
  93.  
  94. function getTarget(target) {
  95. // check toggle
  96. if (!getToggle()) return;
  97. // hide target
  98. document.querySelectorAll(target).forEach(t => {
  99. t.classList.add("hide-set");
  100. t.addEventListener("mouseenter", showTarget);
  101. t.addEventListener("mouseleave", hideTarget);
  102. });
  103. }
  104.  
  105. function showTarget() {
  106. this.style.opacity = 1;
  107. }
  108.  
  109. function hideTarget() {
  110. this.style.opacity = 0.1;
  111. }
  112.  
  113. function stopHide() {
  114. // check toggle
  115. if (getToggle()) return;
  116. // show target
  117. document.querySelectorAll(".hide-set").forEach(hide => {
  118. hide.removeEventListener("mouseenter", showTarget);
  119. hide.removeEventListener("mouseleave", hideTarget);
  120. hide.classList.remove("hide-set");
  121. hide.style.opacity = 1;
  122. });
  123. }
  124.  
  125. function update() {
  126. if (updating) return;
  127. updating = true;
  128. init(3);
  129. setTimeout(() => { updating = false; }, 1000);
  130. }
  131.  
  132. function css() {
  133. const style = document.createElement("style");
  134. style.type = "text/css";
  135. style.innerHTML = textStyle;
  136. document.head.appendChild(style);
  137. }
  138.  
  139. function locationChange() {
  140. window.addEventListener('locationchange', () => init(3));
  141. // situation 1
  142. history.pushState = (f => function pushState(){
  143. var ret = f.apply(this, arguments);
  144. window.dispatchEvent(new Event('pushState'));
  145. window.dispatchEvent(new Event('locationchange'));
  146. return ret;
  147. })(history.pushState);
  148. // situation 2
  149. history.replaceState = (f => function replaceState(){
  150. var ret = f.apply(this, arguments);
  151. window.dispatchEvent(new Event('replaceState'));
  152. window.dispatchEvent(new Event('locationchange'));
  153. return ret;
  154. })(history.replaceState);
  155. // situation 3
  156. window.addEventListener('popstate', () => window.dispatchEvent(new Event('locationchange')));
  157. }
  158.  
  159. })();