CAI Universal Toolbelt Menu

Reusable menu structure for various c.ai TM scripts that require a menu

目前为 2023-05-12 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/466064/1189764/CAI%20Universal%20Toolbelt%20Menu.js

  1. // ==UserScript==
  2. // @exclude *
  3. // @author notdoingthateither
  4.  
  5. // ==UserLibrary==
  6. // @name CAI Universal Toolbelt Menu
  7. // @description Reusable menu structure for various c.ai TM scripts that require a menu
  8. // @license MIT
  9.  
  10.  
  11. // ==/UserScript==
  12.  
  13. // ==/UserLibrary==
  14.  
  15. CAIToolMenu = window.CAIToolMenu || {};
  16.  
  17. CAIToolMenu = function() {
  18.  
  19. const _CAIToolMenu_ArrowUp = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-bar-up" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M8 10a.5.5 0 0 0 .5-.5V3.707l2.146 2.147a.5.5 0 0 0 .708-.708l-3-3a.5.5 0 0 0-.708 0l-3 3a.5.5 0 1 0 .708.708L7.5 3.707V9.5a.5.5 0 0 0 .5.5zm-7 2.5a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13a.5.5 0 0 1-.5-.5z"/> </svg>';
  20. const _CAIToolMenu_ArrowDown = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-bar-down" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M1 3.5a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13a.5.5 0 0 1-.5-.5zM8 6a.5.5 0 0 1 .5.5v5.793l2.146-2.147a.5.5 0 0 1 .708.708l-3 3a.5.5 0 0 1-.708 0l-3-3a.5.5 0 0 1 .708-.708L7.5 12.293V6.5A.5.5 0 0 1 8 6z"/> </svg>';
  21.  
  22. let menuElement = document.getElementById('tms-script-cai-universal-toolbelt');
  23. if (menuElement == null) {
  24. console.log('init menu.');
  25. init();
  26. } else {
  27. console.log('menu already initialized.');
  28. }
  29.  
  30. init = function() {
  31. const parentElement = document.createElement('div');
  32. parentElement.setAttribute('id', 'tms-script-cai-universal-toolbelt-root');
  33. parentElement.setAttribute('style', 'display: none');
  34. parentElement.classList.add('cai-tool-root');
  35.  
  36. menuElement = document.createElement('div');
  37. menuElement.setAttribute('id', 'tms-script-cai-universal-toolbelt');
  38. menuElement.classList.add('cai-tool-menu', 'cai-tool-menu-hide');
  39. const toggleIconSpanL = document.createElement('span');
  40. toggleIconSpanL.classList.add('cai-tool-toggle');
  41. toggleIconSpanL.innerHTML = _CAIToolMenu_ArrowUp;
  42. const caitmSpan = document.createElement('span');
  43. caitmSpan.innerHTML = 'c.ai ToolMenu';
  44.  
  45. const toggleIconSpanR = document.createElement('span');
  46. toggleIconSpanR.classList.add('cai-tool-toggle');
  47. toggleIconSpanR.innerHTML = _CAIToolMenu_ArrowUp;
  48.  
  49. const menuToggleBtn = document.createElement('button');
  50. menuToggleBtn.classList.add('cai-tool-menu-toggle');
  51. menuToggleBtn.append(toggleIconSpanL, caitmSpan, toggleIconSpanR);
  52. menuToggleBtn.dataset.menuHidden = 'true';
  53. menuToggleBtn.onclick = () => {
  54. if (menuToggleBtn.dataset.menuHidden === 'true') {
  55. menuElement.classList.remove('cai-tool-menu-hide');
  56. toggleIconSpanL.innerHTML = _CAIToolMenu_ArrowDown;
  57. toggleIconSpanR.innerHTML = _CAIToolMenu_ArrowDown;
  58. menuToggleBtn.dataset.menuHidden = 'false';
  59. } else {
  60. menuElement.classList.add('cai-tool-menu-hide');
  61. toggleIconSpanL.innerHTML = _CAIToolMenu_ArrowUp;
  62. toggleIconSpanR.innerHTML = _CAIToolMenu_ArrowUp;
  63. menuToggleBtn.dataset.menuHidden = 'true';
  64. }
  65. };
  66.  
  67. parentElement.append(menuElement, menuToggleBtn);
  68. document.body.appendChild(parentElement);
  69. console.log('menu initialized.');
  70. const styleHTML = document.createElement('style');
  71. styleHTML.innerHTML = `
  72. .cai-tool-btn {
  73. border-radius: 32px;
  74. cursor: pointer;
  75. margin: .25rem .25rem 0 .25rem;
  76. border: 1px solid;
  77. width: fit-content;
  78. min-width: 32px;
  79. min-height: 32px;
  80. }
  81.  
  82. .cai-tool-root {
  83. position: fixed;
  84. bottom: 0;
  85. z-index: 200;
  86. width: 100%;
  87. display: flex;
  88. align-items: center;
  89. flex-direction: column;
  90. }
  91. .cai-tool-menu {
  92. display: flex;
  93. flex-wrap: wrap;
  94. flex-direction: column;
  95. align-items: center;
  96. max-width: 300px;
  97. z-index: 201;
  98. }
  99. .cai-tool-menu-hide {
  100. display: none !important;
  101. }
  102. .cai-tool-menu-toggle {
  103. cursor: pointer;
  104. display: flex;
  105. border: 1px solid transparent;
  106. background-color: transparent;
  107. align-items: center;
  108. justify-content: center;
  109. width: 100%;
  110. font-size: 12px;
  111. font-weight: 600;
  112. letter-spacing: .05em;
  113. z-index: 202;
  114. }
  115. .cai-tool-toggle {
  116. margin-top: -.5rem;
  117. margin-bottom: -.5rem;
  118. padding: .25rem;
  119. width: 32px;
  120. flex: 0 0 auto;
  121. width: auto;
  122. }
  123. `;
  124. document.body.appendChild(styleHTML);
  125. };
  126.  
  127. addButton = function() {
  128. const newBtn = document.createElement('button');
  129. newBtn.classList.add('cai-tool-btn');
  130. buttons.push(newBtn);
  131. if (menuElement != null) {
  132. menuElement.append(newBtn);
  133. console.log('new button added!');
  134. onButtonAdded();
  135. }
  136.  
  137. return newBtn;
  138. };
  139.  
  140. onButtonAdded = function() {
  141. const parent = document.getElementById('tms-script-cai-universal-toolbelt-root');
  142. if (parent.hasAttribute('style')) parent.removeAttribute('style');
  143. }
  144.  
  145. return {
  146. "newButton": addButton
  147. };
  148. }();

QingJ © 2025

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