CAI Universal Toolbelt Menu

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

目前為 2023-05-12 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/466064/1189806/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. let menuElement = document.getElementById('tms-script-cai-universal-toolbelt');
  20. let toggleIconSpanL;
  21. let toggleIconSpanR;
  22. let menuToggleBtn;
  23.  
  24. init = function() {
  25. 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>';
  26. 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>';
  27. const parentElement = document.createElement('div');
  28. parentElement.setAttribute('id', 'tms-script-cai-universal-toolbelt-root');
  29. parentElement.setAttribute('style', 'display: none');
  30. parentElement.classList.add('cai-tool-root');
  31.  
  32. menuElement = document.createElement('div');
  33. menuElement.setAttribute('id', 'tms-script-cai-universal-toolbelt');
  34. menuElement.classList.add('cai-tool-menu', 'cai-tool-menu-hide');
  35. toggleIconSpanL = document.createElement('span');
  36. toggleIconSpanL.classList.add('cai-tool-toggle');
  37. toggleIconSpanL.innerHTML = _CAIToolMenu_ArrowUp;
  38. const caitmSpan = document.createElement('span');
  39. caitmSpan.innerHTML = 'TM';
  40.  
  41. toggleIconSpanR = document.createElement('span');
  42. toggleIconSpanR.classList.add('cai-tool-toggle');
  43. toggleIconSpanR.innerHTML = _CAIToolMenu_ArrowUp;
  44.  
  45. menuToggleBtn = document.createElement('button');
  46. menuToggleBtn.classList.add('cai-tool-menu-toggle');
  47. menuToggleBtn.append(toggleIconSpanL, caitmSpan, toggleIconSpanR);
  48. menuToggleBtn.dataset.menuHidden = 'true';
  49. menuToggleBtn.onclick = () => {
  50. triggerMenuClick(menuToggleBtn.dataset.menuHidden === 'true');
  51. };
  52.  
  53. parentElement.append(menuElement, menuToggleBtn);
  54. document.body.appendChild(parentElement);
  55. console.log('menu initialized.');
  56. const styleHTML = document.createElement('style');
  57. styleHTML.innerHTML = `
  58. .cai-tool-root {
  59. position: fixed;
  60. bottom: 5px;
  61. z-index: 200;
  62. display: flex;
  63. align-items: start;
  64. flex-direction: column;
  65. }
  66.  
  67. .cai-tool-menu {
  68. display: flex;
  69. flex-wrap: wrap;
  70. flex-direction: column;
  71. max-width: 300px;
  72. z-index: 201;
  73. }
  74.  
  75. .cai-tool-menu-hide {
  76. display: none !important;
  77. }
  78. .cai-tool-btn {
  79. border-radius: 32px;
  80. cursor: pointer;
  81. margin: .25rem .25rem 0 .25rem;
  82. border: 1px solid;
  83. width: fit-content;
  84. min-width: 32px;
  85. min-height: 32px;
  86. }
  87. .cai-tool-menu-toggle {
  88. cursor: pointer;
  89. display: flex;
  90. border: 1px solid transparent;
  91. background-color: transparent;
  92. align-items: center;
  93. justify-content: center;
  94. font-size: 12px;
  95. font-weight: 600;
  96. letter-spacing: .05em;
  97. z-index: 202;
  98. }
  99. .cai-tool-toggle {
  100. margin-top: -.5rem;
  101. margin-bottom: -.5rem;
  102. padding: .25rem;
  103. width: 32px;
  104. flex: 0 0 auto;
  105. width: auto;
  106. }
  107. `;
  108. document.body.appendChild(styleHTML);
  109. };
  110.  
  111. addButton = function() {
  112. const newBtn = document.createElement('button');
  113. newBtn.classList.add('cai-tool-btn');
  114.  
  115. if (menuElement != null) {
  116. menuElement.append(newBtn);
  117. console.log('new button added!');
  118. onButtonAdded();
  119. }
  120.  
  121. return newBtn;
  122. };
  123.  
  124. onButtonAdded = function() {
  125. const parent = document.getElementById('tms-script-cai-universal-toolbelt-root');
  126. if (parent.hasAttribute('style')) parent.removeAttribute('style');
  127. };
  128. triggerMenuClick = function(isHidden) {
  129. if (isHidden) {
  130. menuElement.classList.remove('cai-tool-menu-hide');
  131. toggleIconSpanL.innerHTML = _CAIToolMenu_ArrowDown;
  132. toggleIconSpanR.innerHTML = _CAIToolMenu_ArrowDown;
  133. menuToggleBtn.dataset.menuHidden = 'false';
  134. } else {
  135. menuElement.classList.add('cai-tool-menu-hide');
  136. toggleIconSpanL.innerHTML = _CAIToolMenu_ArrowUp;
  137. toggleIconSpanR.innerHTML = _CAIToolMenu_ArrowUp;
  138. menuToggleBtn.dataset.menuHidden = 'true';
  139. }
  140. }
  141. if (menuElement == null) {
  142. console.log('init menu.');
  143. init();
  144. } else {
  145. console.log('menu already initialized.');
  146. }
  147.  
  148. return {
  149. "newButton": addButton,
  150. "showMenu": triggerMenuClick
  151. };
  152. }();

QingJ © 2025

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