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/1189713/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;
  23. let buttons = [];
  24. const observer = new MutationObserver((mutations) => {
  25. if (menuElement == null && mutations.length > 0) {
  26. const el = document.getElementById('tms-script-cai-universal-toolbelt');
  27. if (el !== null) {
  28. menuElement = el;
  29. }
  30. if (document.getElementsByClassName('chatfooterbg-normal').length > 0) {
  31. init();
  32. }
  33. }
  34. });
  35.  
  36. observer.observe(document.body, { attributes: true, childList: true, subtree: true });
  37.  
  38. _processWaitList = function() {
  39. menuElement.append(buttons);
  40. };
  41.  
  42. _initCallback = function() {
  43. const parentElement = document.getElementsByClassName('chatfooterbg-normal')[0];
  44.  
  45. menuElement = document.createElement('div');
  46. menuElement.setAttribute('id', 'tms-script-cai-universal-toolbelt');
  47. menuElement.classList.add('cai-tool-menu', 'cai-tool-menu-hide');
  48. const toggleIconSpan = document.createElement('span');
  49. toggleIconSpan.classList.add('cai-tool-toggle');
  50. toggleIconSpan.innerHTML = _CAIToolMenu_ArrowUp;
  51. const menuToggleBtn = document.createElement('button');
  52. menuToggleBtn.classList.add('cai-tool-menu-toggle');
  53. menuToggleBtn.append(toggleIconSpan);
  54. menuToggleBtn.dataset.menuHidden = 'true';
  55. menuToggleBtn.onclick = () => {
  56. if (menuToggleBtn.dataset.menuHidden === 'true') {
  57. menuElement.classList.remove('cai-tool-menu-hide');
  58. menuToggleBtn.innerHTML = _CAIToolMenu_ArrowUp;
  59. menuToggleBtn.dataset.menuHidden = 'false';
  60. } else {
  61. menuElement.classList.add('cai-tool-menu-hide');
  62. menuToggleBtn.innerHTML = _CAIToolMenu_ArrowDown;
  63. menuToggleBtn.dataset.menuHidden = 'true';
  64. }
  65. };
  66. parentElement.prepend(menuElement, menuToggleBtn);
  67. _processWaitList();
  68. console.log('menu setup finished');
  69. };
  70.  
  71. init = function() {
  72. if (document.readyState === 'complete') {
  73. _initCallback();
  74. } else {
  75. window.addEventListener("load", _initCallback);
  76. }
  77. const styleHTML = document.createElement('style');
  78. styleHTML.innerHTML = `
  79. .cai-tool-btn {
  80. border-radius: 32px;
  81. cursor: pointer;
  82. margin-right: .25rem;
  83. margin-left: .25rem;
  84. padding: .1rem;
  85. flex: 0 0 auto;
  86. width: auto;
  87. border: 1px solid transparent;
  88. }
  89. .cai-tool-menu {
  90. display: flex;
  91. align-items: center;
  92. justify-content: center;
  93. }
  94. .cai-tool-menu-hide {
  95. display: none !important;
  96. }
  97. .cai-tool-menu-toggle {
  98. cursor: pointer;
  99. display: flex;
  100. align-items: center;
  101. justify-content: center;
  102. border: 1px solid transparent;
  103. background-color: transparent;
  104. }
  105. .cai-tool-toggle {
  106. margin-top: -.5rem;
  107. margin-bottom: -.5rem;
  108. flex: 0 0 auto;
  109. width: auto;
  110. }
  111. `;
  112. document.body.appendChild(styleHTML);
  113. console.log('added menu style');
  114. };
  115.  
  116. addButton = function() {
  117. const newBtn = document.createElement('button');
  118. newBtn.classList.add('cai-tool-btn');
  119. if (menuElement != null) {
  120. menuElement.append(newBtn);
  121. console.log('new button added!');
  122. } else {
  123. console.log('no menu initialized, adding button to wait list...');
  124. buttons.push(newBtn);
  125. }
  126.  
  127. return newBtn;
  128. };
  129.  
  130. return {
  131. "newButton": addButton
  132. };
  133. }();

QingJ © 2025

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