通义千问侧边栏收缩(Tongyi Qianwen Sidebar Toggle)

Add a toggleable sidebar to Aliyun Tongyi Qianwen page.

  1. // ==UserScript==
  2. // @name 通义千问侧边栏收缩(Tongyi Qianwen Sidebar Toggle)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Add a toggleable sidebar to Aliyun Tongyi Qianwen page.
  6. // @author Epool
  7. // @match https://tongyi.aliyun.com/qianwen/*
  8. // @grant GM_addStyle
  9. // @run-at document-idle
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 注入CSS样式
  17. function GM_addStyle(css) {
  18. var style = document.createElement('style');
  19. style.appendChild(document.createTextNode(css));
  20. document.head.appendChild(style);
  21. }
  22.  
  23. GM_addStyle(`
  24. .container--IUBnVmjY .contentWrap--x9xW84h6 .side--L0W1WdHl {
  25. width: 275px; /* 假设初始宽度 */
  26. transition: width 0.3s ease; /* 添加过渡效果 */
  27. }
  28.  
  29. /* 收缩状态的样式 */
  30. .container--IUBnVmjY .contentWrap--x9xW84h6 .side--L0W1WdHl.collapsed {
  31. display: none;
  32. transition: width 0.3s ease; /* 添加过渡效果 */
  33. }
  34.  
  35. /* 按钮样式 */
  36. #toggle-sidebar {
  37. position: absolute;
  38. right: calc(50% - 50px); /* 让按钮位于侧边栏右侧的中间位置 */
  39. top: 10px;
  40. z-index: 9999;
  41. padding: 5px 10px;
  42. background-color: #ffffff;
  43. border: 1px solid #000000;
  44. cursor: pointer;
  45. }
  46. `);
  47.  
  48. // 延迟读取侧边栏元素
  49. setTimeout(function() {
  50. var sidebar = document.querySelector('.side--L0W1WdHl');
  51. if (!sidebar) {
  52. // 如果不存在这个侧边栏,则退出脚本
  53. return;
  54. }
  55.  
  56. // 创建按钮
  57. var toggleButton;
  58. if (document.getElementById('toggle-sidebar')) {
  59. toggleButton = document.getElementById('toggle-sidebar');
  60. } else {
  61. toggleButton = document.createElement('button');
  62. toggleButton.id = 'toggle-sidebar';
  63. toggleButton.textContent = '展开/收缩';
  64. document.body.appendChild(toggleButton);
  65. }
  66.  
  67. // 添加点击事件监听器
  68. toggleButton.addEventListener('click', function() {
  69. sidebar.classList.toggle('collapsed');
  70. });
  71. }, 2000); // 延迟2秒读取侧边栏元素
  72.  
  73. // 使用MutationObserver监听侧边栏元素的出现
  74. var observer = new MutationObserver(function(mutationsList) {
  75. for (var mutation of mutationsList) {
  76. if (mutation.addedNodes.length > 0) {
  77. // 遍历新增的节点,检查是否包含侧边栏元素
  78. for (var node of mutation.addedNodes) {
  79. if (node.classList && node.classList.contains('side--L0W1WdHl')) {
  80. // 创建按钮
  81. var toggleButton;
  82. if (document.getElementById('toggle-sidebar')) {
  83. toggleButton = document.getElementById('toggle-sidebar');
  84. } else {
  85. toggleButton = document.createElement('button');
  86. toggleButton.id = 'toggle-sidebar';
  87. toggleButton.textContent = '展开/收缩';
  88. document.body.appendChild(toggleButton);
  89. }
  90.  
  91. // 添加点击事件监听器
  92. toggleButton.addEventListener('click', function() {
  93. node.classList.toggle('collapsed');
  94. });
  95.  
  96. // 停止观察器
  97. observer.disconnect();
  98. return;
  99. }
  100. }
  101. }
  102. }
  103. });
  104.  
  105. // 监听整个文档的变化
  106. observer.observe(document.documentElement, {
  107. childList: true,
  108. subtree: true
  109. });
  110. })();

QingJ © 2025

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