知乎 - 强行设为暗色主题

根据网页源代码,很久以前,知乎网页版就已经有了暗色主题。设置项都有了。但官方迟迟不开放使用。等不下去了,先这样了。现已支持切换主题。使用按钮切换主题后,如果页面显示有错误,刷新即可。

目前為 2020-05-10 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name 知乎 - 强行设为暗色主题
  3. // @description 根据网页源代码,很久以前,知乎网页版就已经有了暗色主题。设置项都有了。但官方迟迟不开放使用。等不下去了,先这样了。现已支持切换主题。使用按钮切换主题后,如果页面显示有错误,刷新即可。
  4. // @namespace RainSlide
  5. // @author RainSlide
  6. // @icon https://static.zhihu.com/static/favicon.ico
  7. // @version 1.1
  8. // @match https://*.zhihu.com/*
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_unregisterMenuCommand
  13. // @inject-into context
  14. // @run-at document-start
  15. // ==/UserScript==
  16.  
  17. const name = "data-theme";
  18. const d = document.documentElement;
  19.  
  20. Object.defineProperties(
  21. this, {
  22. "localTheme": {
  23. configurable: true,
  24. enumerable: false,
  25. get: () => GM_getValue(name),
  26. set: value => GM_setValue(name, value)
  27. },
  28. "pageTheme": {
  29. configurable: true,
  30. enumerable: false,
  31. get: () => d.getAttribute(name),
  32. set: value => d.setAttribute(name, value)
  33. }
  34. }
  35. );
  36.  
  37. const dark = "dark";
  38. const light = "light";
  39.  
  40. // 将 localTheme 的 state 同步至 pageTheme
  41. const sync = () => {
  42. if (pageTheme !== localTheme) {
  43. pageTheme = localTheme;
  44. }
  45. };
  46.  
  47. // 如果设置了暗色主题
  48. if ( localTheme === dark ) {
  49.  
  50. sync(); // if ( pageTheme !== dark ) pageTheme = dark;
  51.  
  52. // 强行设为暗色主题
  53. document.addEventListener(
  54. "DOMContentLoaded", () => {
  55. const data = document.getElementById("js-initialData");
  56. if (data) data.textContent = data.textContent.replace(
  57. '"theme":"light"', '"theme":"dark"'
  58. );
  59. }
  60. );
  61.  
  62. }
  63.  
  64. // 定时同步 state
  65. setInterval(sync, 2000);
  66.  
  67. // 切换主题
  68.  
  69. const setTheme = theme => pageTheme = localTheme = theme;
  70.  
  71. const invertMap = new Map([
  72. [ dark, light ],
  73. [ light, dark ]
  74. ]);
  75.  
  76. const switchTheme = () => setTheme( invertMap.get(localTheme) || dark );
  77.  
  78. // 脚本管理器菜单按钮
  79. this.GM_registerMenuCommand &&
  80. this.GM_registerMenuCommand( "切换主题", switchTheme );
  81.  
  82. // 页面右下角按钮
  83. window.addEventListener(
  84. "load", () => {
  85. const buttons = document.querySelector('.CornerButtons');
  86. if (buttons) {
  87.  
  88. const container = document.createElement("div");
  89. const button = document.createElement("button");
  90.  
  91. const label = "切换主题";
  92.  
  93. container.className = "CornerAnimayedFlex";
  94.  
  95. new Map([
  96. [ "data-tooltip", label ],
  97. [ "data-tooltip-position", "left" ],
  98. [ "data-tooltip-will-hide-on-click", "true" ],
  99. [ "aria-label", label ],
  100. [ "type", "button" ],
  101. [ "class", "Button CornerButton Button--plain" ]
  102. ]).forEach(
  103. (value, key) => button.setAttribute(key, value)
  104. );
  105.  
  106. button.innerHTML =
  107. // 图标来自 Google Material Icons
  108. `<svg xmlns="http://www.w3.org/2000/svg" class="Zi" aria-label="${ label }" fill="currentColor" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M20 8.69V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12 20 8.69zM12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6c3.31 0 6 2.69 6 6s-2.69 6-6 6z"/></svg>`;
  109.  
  110. button.addEventListener("mouseenter", sync);
  111. button.addEventListener("focus", sync);
  112. button.addEventListener("click", switchTheme);
  113.  
  114. container.appendChild(button);
  115. buttons.insertBefore(container, buttons.firstElementChild);
  116.  
  117. }
  118. }
  119. );
  120.  
  121. /*
  122. <div class="CornerAnimayedFlex">
  123. <button data-tooltip="暗色主题" data-tooltip-position="left" data-tooltip-will-hide-on-click="true" aria-label="暗色主题" type="button" class="Button CornerButton Button--plain">
  124. <svg xmlns="http://www.w3.org/2000/svg" class="Zi" aria-label="暗色主题" fill="currentColor" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M20 8.69V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12 20 8.69zM12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6c3.31 0 6 2.69 6 6s-2.69 6-6 6z"/></svg>
  125. </button>
  126. </div>
  127. */

QingJ © 2025

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