nowhots.com 布局调整

调整 nowhots.com 布局,交换左右侧栏,并调整导航菜单高度。

  1. // ==UserScript==
  2. // @name nowhots.com 布局调整
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.17
  5. // @description 调整 nowhots.com 布局,交换左右侧栏,并调整导航菜单高度。
  6. // @author Gemini, Doubao
  7. // @match https://nowhots.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (() => {
  13. 'use strict';
  14. // 获取元素的辅助函数
  15. const getEl = (cls) => document.querySelector(cls);
  16.  
  17. // 从本地存储获取导航位置设置,默认为左侧
  18. const getNavPosition = () => localStorage.getItem('nowhotsNavPosition') || 'left';
  19.  
  20. // 保存导航位置设置到本地存储
  21. const saveNavPosition = (position) => localStorage.setItem('nowhotsNavPosition', position);
  22.  
  23. // 检测当前模式 - 注意:class=dark-mode 为日间模式的配色
  24. const isDayMode = () => document.body.classList.contains('dark-mode');
  25.  
  26. // 获取当前模式的按钮样式
  27. const getButtonStyles = () => {
  28. // 日夜间模式样式配置
  29. const styles = {
  30. day: {
  31. active: {
  32. bgColor: 'rgb(248, 248, 248)',
  33. darkreader: 'var(--darkreader-background-f8f8f8, #1c1e1f)'
  34. }
  35. },
  36. night: {
  37. active: {
  38. bgColor: 'rgb(82, 83, 84)',
  39. darkreader: 'var(--darkreader-background-525354, #3e4446)'
  40. }
  41. }
  42. };
  43. // 返回当前模式的样式
  44. return isDayMode() ? styles.night : styles.day;
  45. };
  46. // 添加导航设置选项
  47. const addNavSettings = () => {
  48. const setupContainer = getEl('.barrier-setup-base-container-box');
  49. if (!setupContainer) return;
  50.  
  51. // 创建导航设置选项
  52. const navSettingsDiv = document.createElement('div');
  53. navSettingsDiv.className = 'barrier-setup-base-container-common-box';
  54. navSettingsDiv.innerHTML = `
  55. <div class="barrier-setup-base-container-common-text-box">
  56. <span>导航显示</span>
  57. </div>
  58. <div class="barrier-setup-base-container-common-switch-box" id="nav-position-left">
  59. <span>左</span>
  60. </div>
  61. <div class="barrier-setup-base-container-common-switch-box" id="nav-position-right">
  62. <span>右</span>
  63. </div>
  64. `;
  65. // 插入到设置容器中
  66. setupContainer.appendChild(navSettingsDiv);
  67.  
  68. // 更新按钮状态
  69. updateSettingsUI();
  70. // 添加点击事件
  71. document.getElementById('nav-position-left').addEventListener('click', () => {
  72. if (getNavPosition() !== 'left') {
  73. saveNavPosition('left');
  74. window.location.reload();
  75. }
  76. });
  77. document.getElementById('nav-position-right').addEventListener('click', () => {
  78. if (getNavPosition() !== 'right') {
  79. saveNavPosition('right');
  80. window.location.reload();
  81. }
  82. });
  83. // 监听 body 类变化,自动更新按钮样式
  84. const observer = new MutationObserver(() => updateSettingsUI());
  85. observer.observe(document.body, { attributes: true });
  86. };
  87.  
  88. // 更新设置按钮UI状态
  89. const updateSettingsUI = () => {
  90. const position = getNavPosition();
  91. const leftBtn = document.getElementById('nav-position-left');
  92. const rightBtn = document.getElementById('nav-position-right');
  93.  
  94. if (!leftBtn || !rightBtn) return;
  95. // 重置按钮样式
  96. [leftBtn, rightBtn].forEach(btn => {
  97. btn.removeAttribute('style');
  98. btn.removeAttribute('data-darkreader-inline-bgcolor');
  99. });
  100. // 获取当前模式的按钮样式
  101. const styles = getButtonStyles();
  102. // 设置激活按钮的样式
  103. const activeBtn = position === 'left' ? leftBtn : rightBtn;
  104. activeBtn.style.backgroundColor = styles.active.bgColor;
  105. activeBtn.setAttribute('data-darkreader-inline-bgcolor', styles.active.darkreader);
  106. };
  107.  
  108. // 交换左右侧栏位置
  109. const swapSidebars = () => {
  110. const leftBox = getEl('.main-scope-left-box');
  111. const rightBox = getEl('.main-scope-right-box');
  112.  
  113. if (leftBox && rightBox && leftBox.parentNode) {
  114. try {
  115. const parent = leftBox.parentNode;
  116. parent.appendChild(leftBox);
  117. parent.insertBefore(rightBox, parent.firstChild);
  118. } catch (error) {
  119. console.error('交换左右侧栏位置时出错:', error);
  120. }
  121. }
  122. };
  123. // 调整左侧菜单高度
  124. const adjustMenuHeight = () => {
  125. const leftMenuBox = getEl('.main-scope-left-menu-box');
  126. const centerBox = getEl('.main-scope-center-box');
  127. const leftMenuOverflowBox = getEl('.main-scope-left-menu-overflow-box');
  128.  
  129. if (leftMenuBox && centerBox && leftMenuOverflowBox) {
  130. try {
  131. const height = centerBox.offsetHeight;
  132. const setHeight = (el, h) => {
  133. el.style.setProperty('height', `${h}px`, 'important');
  134. };
  135. setHeight(leftMenuBox, height);
  136. setHeight(leftMenuOverflowBox, height - 36);
  137. } catch (error) {
  138. console.error('设置左侧菜单和溢出框高度时出错:', error);
  139. }
  140. }
  141. };
  142. // 应用导航位置设置
  143. const applySettings = () => {
  144. const position = getNavPosition();
  145. if (position === 'right') {
  146. swapSidebars();
  147. }
  148. adjustMenuHeight();
  149. };
  150. // 初始化函数
  151. const init = () => {
  152. // 等待DOM完全加载后添加设置选项
  153. if (document.readyState === 'loading') {
  154. document.addEventListener('DOMContentLoaded', () => {
  155. setTimeout(addNavSettings, 1000);
  156. });
  157. } else {
  158. setTimeout(addNavSettings, 1000);
  159. }
  160. };
  161.  
  162. // 页面加载完成后执行应用设置
  163. window.addEventListener('load', () => {
  164. applySettings();
  165. });
  166.  
  167. // 监听页面变化,动态调整高度 (保持不变)
  168. const observer = new MutationObserver(() => {
  169. adjustMenuHeight();
  170. });
  171.  
  172. window.addEventListener('load', () => {
  173. const mainContent = document.querySelector('.main-scope-content');
  174. if (mainContent) {
  175. observer.observe(mainContent, {
  176. childList: true,
  177. subtree: true,
  178. attributes: true
  179. });
  180. }
  181. });
  182.  
  183. init();
  184. })();

QingJ © 2025

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