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

QingJ © 2025

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