知乎 - 强行启用暗色主题

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

  1. // ==UserScript==
  2. // @name 知乎 - 强行启用暗色主题
  3. // @description 根据网页源代码,很久以前,知乎网页版就已经有了暗色主题。设置项都有了。但官方迟迟不开放使用。等不下去了,先这样了。现已支持切换主题。使用按钮切换主题后,如果页面显示有错误,刷新即可。
  4. // @namespace RainSlide
  5. // @author RainSlide
  6. // @license blessing
  7. // @icon https://static.zhihu.com/static/favicon.ico
  8. // @version 1.2
  9. // @match https://*.zhihu.com/*
  10. // @inject-into context
  11. // @run-at document-start
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_registerMenuCommand
  15. // @grant GM_unregisterMenuCommand
  16. // ==/UserScript==
  17.  
  18. "use strict";
  19.  
  20. const name = "data-theme";
  21. const d = document.documentElement;
  22.  
  23. Object.defineProperties(
  24. this, {
  25. localTheme: {
  26. configurable: true,
  27. enumerable: false,
  28. get: () => GM_getValue(name),
  29. set: value => GM_setValue(name, value)
  30. },
  31. pageTheme: {
  32. configurable: true,
  33. enumerable: false,
  34. get: () => d.getAttribute(name),
  35. set: value => d.setAttribute(name, value)
  36. }
  37. }
  38. );
  39.  
  40. const dark = "dark";
  41. const light = "light";
  42.  
  43. // 将 localTheme 的值同步至 pageTheme
  44. const sync = () => {
  45. if (pageTheme !== localTheme) {
  46. pageTheme = localTheme;
  47. }
  48. };
  49.  
  50. sync();
  51. setInterval(sync, 2000);
  52.  
  53. // 强行启用暗色主题
  54. localTheme === dark &&
  55. document.addEventListener(
  56. "DOMContentLoaded", () => {
  57. const data = document.getElementById("js-initialData");
  58. if (data !== null) {
  59. data.textContent = data.textContent.replace(
  60. '"theme":"light"', '"theme":"dark"'
  61. );
  62. }
  63. }
  64. );
  65.  
  66. // 切换主题
  67. const switchTheme = () => {
  68. switch (localTheme) {
  69. case dark: pageTheme = localTheme = light; break;
  70. case light: pageTheme = localTheme = dark; break;
  71. default:
  72. console.warn("localTheme is not dark or light!");
  73. pageTheme = localTheme = dark;
  74. }
  75. };
  76.  
  77. const label = "切换主题";
  78.  
  79. // 脚本管理器菜单按钮
  80. typeof GM_registerMenuCommand === "function" &&
  81. GM_registerMenuCommand(label, switchTheme);
  82.  
  83. // 页面右下角按钮
  84. // 图标来自 Google Material Icons
  85. const icon = `<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>`;
  86.  
  87. window.addEventListener(
  88. "load", () => {
  89. const buttons = document.querySelector('.CornerButtons');
  90. if (buttons !== null) {
  91.  
  92. const container = document.createElement("div");
  93. const button = document.createElement("button");
  94.  
  95. container.className = "CornerAnimayedFlex";
  96.  
  97. button.type = "button";
  98. button.className = "Button CornerButton Button--plain";
  99. button.innerHTML = icon;
  100.  
  101. button.setAttribute("aria-label", label);
  102. button.setAttribute("data-tooltip", label);
  103. button.setAttribute("data-tooltip-position", "left");
  104. button.setAttribute("data-tooltip-will-hide-on-click", "true");
  105.  
  106. button.addEventListener("mouseenter", sync);
  107. button.addEventListener("focus", sync);
  108. button.addEventListener("click", switchTheme);
  109.  
  110. container.appendChild(button);
  111. buttons.insertBefore(container, buttons.firstElementChild);
  112.  
  113. }
  114. }
  115. );
  116.  
  117. /*
  118. <div class="CornerAnimayedFlex">
  119. <button data-tooltip="暗色主题" data-tooltip-position="left" data-tooltip-will-hide-on-click="true" aria-label="暗色主题" type="button" class="Button CornerButton Button--plain">
  120. <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>
  121. </button>
  122. </div>
  123. */

QingJ © 2025

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