微信读书底色修改

修改微信读书的背景颜色,提升阅读体验

  1. // ==UserScript==
  2. // @name 微信读书底色修改
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 修改微信读书的背景颜色,提升阅读体验
  6. // @author 木子火玄
  7. // @match https://weread.qq.com/*
  8. // @icon https://weread.qq.com/favicon.ico
  9. // @license GNU GPLv3
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_registerMenuCommand
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // 默认背景色
  19. const DEFAULT_BACKGROUND_COLOR = '#FFF9E6';
  20. let isColorEnabled = GM_getValue('isColorEnabled', true);
  21.  
  22. // 判断颜色是否为浅色
  23. function isLightColor(color) {
  24. // 移除#符号并转换为RGB
  25. const hex = color.replace('#', '');
  26. const r = parseInt(hex.substr(0, 2), 16);
  27. const g = parseInt(hex.substr(2, 2), 16);
  28. const b = parseInt(hex.substr(4, 2), 16);
  29.  
  30. // 计算亮度 (根据人眼对不同颜色的敏感度)
  31. const brightness = (r * 299 + g * 587 + b * 114) / 1000;
  32.  
  33. // 亮度大于125认为是浅色
  34. return brightness > 125;
  35. }
  36.  
  37. // 应用阅读模式样式
  38. function applyReadingStyle(backgroundColor) {
  39. // 检查是否已存在样式标签
  40. let styleTag = document.getElementById('reading-mode-style');
  41. if (!styleTag) {
  42. styleTag = document.createElement('style');
  43. styleTag.id = 'reading-mode-style';
  44. document.head.appendChild(styleTag);
  45. }
  46.  
  47. // 计算文本颜色(根据背景色亮度)
  48. const isLightBackground = isLightColor(backgroundColor);
  49. const textColor = isLightBackground ? '#333333' : '#ffffff';
  50.  
  51. // 设置CSS规则
  52. styleTag.textContent = `
  53. body {
  54. background-color: ${backgroundColor} !important;
  55. color: ${textColor} !important;
  56. }
  57.  
  58. /* 保持文本容器的背景色,但确保文本可见 */
  59. .post-content, .readerTopBar, .app_content {
  60. background-color: ${backgroundColor} !important;
  61. color: ${textColor} !important;
  62. }
  63.  
  64. /* 确保链接文本可见 */
  65. a {
  66. color: ${isLightBackground ? '#0066cc' : '#66b3ff'} !important;
  67. }
  68. `;
  69. }
  70.  
  71. // 初始化背景色
  72. function initBackgroundColor() {
  73. const backgroundColor = GM_getValue('backgroundColor', DEFAULT_BACKGROUND_COLOR);
  74. if (isColorEnabled) {
  75. applyReadingStyle(backgroundColor);
  76. }
  77. }
  78.  
  79. // 注册(不可用)菜单命令
  80. function registerMenu() {
  81. GM_registerMenuCommand('设置背景颜色', function() {
  82. const currentColor = GM_getValue('backgroundColor', DEFAULT_BACKGROUND_COLOR);
  83. const newColor = prompt('请输入新的背景颜色(十六进制格式,如:#FFF9E6):', currentColor);
  84. if (newColor && /^#[0-9A-Fa-f]{6}$/.test(newColor)) {
  85. GM_setValue('backgroundColor', newColor);
  86. applyReadingStyle(newColor);
  87. }
  88. });
  89. }
  90.  
  91. // 添加颜色切换按钮
  92. function addColorToggleButton() {
  93. const readerControls = document.querySelector('.readerControls');
  94. if (!readerControls) return;
  95.  
  96. const toggleButton = document.createElement('button');
  97. toggleButton.title = '切换背景颜色';
  98. toggleButton.className = 'readerControls_item cg_color';
  99. toggleButton.innerHTML = '<span class="icon"></span>';
  100.  
  101. // 添加按钮样式
  102. const style = document.createElement('style');
  103. style.textContent = `
  104. .cg_color .icon {
  105. background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23999"><path d="M12 3c-4.97 0-9 4.03-9 9s4.03 9 9 9c.83 0 1.5-.67 1.5-1.5 0-.39-.15-.74-.39-1.01-.23-.26-.38-.61-.38-.99 0-.83.67-1.5 1.5-1.5H16c2.76 0 5-2.24 5-5 0-4.42-4.03-8-9-8zm-5.5 9c-.83 0-1.5-.67-1.5-1.5S5.67 9 6.5 9 8 9.67 8 10.5 7.33 12 6.5 12zm3-4C8.67 8 8 7.33 8 6.5S8.67 5 9.5 5s1.5.67 1.5 1.5S10.33 8 9.5 8zm5 0c-.83 0-1.5-.67-1.5-1.5S13.67 5 14.5 5s1.5.67 1.5 1.5S15.33 8 14.5 8zm3 4c-.83 0-1.5-.67-1.5-1.5S16.67 9 17.5 9s1.5.67 1.5 1.5-.67 1.5-1.5 1.5z"/></svg>');
  106. width: 24px;
  107. height: 24px;
  108. display: inline-block;
  109. background-size: contain;
  110. background-repeat: no-repeat;
  111. background-position: center;
  112. transition: opacity 0.2s ease;
  113. opacity: 0.7;
  114. }
  115. .cg_color:hover .icon {
  116. opacity: 1;
  117. }
  118. .cg_color.active .icon {
  119. background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%230066cc"><path d="M12 3c-4.97 0-9 4.03-9 9s4.03 9 9 9c.83 0 1.5-.67 1.5-1.5 0-.39-.15-.74-.39-1.01-.23-.26-.38-.61-.38-.99 0-.83.67-1.5 1.5-1.5H16c2.76 0 5-2.24 5-5 0-4.42-4.03-8-9-8zm-5.5 9c-.83 0-1.5-.67-1.5-1.5S5.67 9 6.5 9 8 9.67 8 10.5 7.33 12 6.5 12zm3-4C8.67 8 8 7.33 8 6.5S8.67 5 9.5 5s1.5.67 1.5 1.5S10.33 8 9.5 8zm5 0c-.83 0-1.5-.67-1.5-1.5S13.67 5 14.5 5s1.5.67 1.5 1.5S15.33 8 14.5 8zm3 4c-.83 0-1.5-.67-1.5-1.5S16.67 9 17.5 9s1.5.67 1.5 1.5-.67 1.5-1.5 1.5z"/></svg>');
  120. }
  121. `;
  122. document.head.appendChild(style);
  123.  
  124. // 设置按钮状态
  125. if (isColorEnabled) {
  126. toggleButton.classList.add('active');
  127. }
  128.  
  129. // 添加点击事件
  130. toggleButton.addEventListener('click', function() {
  131. isColorEnabled = !isColorEnabled;
  132. GM_setValue('isColorEnabled', isColorEnabled);
  133.  
  134. if (isColorEnabled) {
  135. toggleButton.classList.add('active');
  136. const backgroundColor = GM_getValue('backgroundColor', DEFAULT_BACKGROUND_COLOR);
  137. applyReadingStyle(backgroundColor);
  138. } else {
  139. toggleButton.classList.remove('active');
  140. // 移除自定义样式
  141. const styleTag = document.getElementById('reading-mode-style');
  142. if (styleTag) {
  143. styleTag.remove();
  144. }
  145. }
  146. });
  147.  
  148. readerControls.appendChild(toggleButton);
  149. }
  150.  
  151. // 页面加载完成后执行
  152. window.addEventListener('load', function() {
  153. initBackgroundColor();
  154. registerMenu();
  155. addColorToggleButton();
  156. });
  157. })();

QingJ © 2025

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