Caveduck Modifier

修改Caveduck網站的樣式。

目前為 2024-12-03 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Caveduck Modifier
  3. // @namespace https://labs.muyi.tw/caveduck_modifier/
  4. // @version 0.28
  5. // @description 修改Caveduck網站的樣式。
  6. // @license AGPL-3.0-or-later
  7. // @author 慕儀
  8. // @match *://caveduck.io/*
  9. // @grant GM_addStyle
  10. // @icon data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDEyODAgMTI4MCI+CiAgPGRlZnM+CiAgICA8c3R5bGU+CiAgICAgIC5jbHMtMSB7CiAgICAgICAgZmlsbDogI2ZmZjsKICAgICAgfQoKICAgICAgLmNscy0yIHsKICAgICAgICBmaWxsOiAjZjJiNDEyOwogICAgICB9CiAgICA8L3N0eWxlPgogIDwvZGVmcz4KICA8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMjguNy4xLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogMS4yLjAgQnVpbGQgMTQyKSAgLS0+CiAgPGc+CiAgICA8ZyBpZD0iQ2F2ZWR1Y2siPgogICAgICA8ZyBpZD0iQ2F2ZWR1Y2stMiIgZGF0YS1uYW1lPSJDYXZlZHVjayI+CiAgICAgICAgPHBhdGggZD0iTTEwOTYuMSw0NTMuNWMzMDUuMiw0OTcuMywxNTIuNiw3NDYtNDU3LjgsNzQ2Uy0xMjQuNiw5NTAuOCwxODAuNiw0NTMuNWMzMDUuMi00OTcuMyw2MTAuMy00OTcuMyw5MTUuNSwwWiIvPgogICAgICAgIDxwYXRoIGNsYXNzPSJjbHMtMSIgZD0iTTExNDcuNiw0NTMuNWMyMDguOCwzNjEuNywxMDQuNCw1NDIuNS0zMTMuMiw1NDIuNXMtNTIyLTE4MC44LTMxMy4yLTU0Mi41YzIwOC44LTM2MS43LDQxNy42LTM2MS43LDYyNi41LDBaIi8+CiAgICAgICAgPHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNjg0LDcyMS4yYzEwMC4yLDE3My42LDUwLjEsMjYwLjQtMTUwLjMsMjYwLjRzLTI1MC42LTg2LjgtMTUwLjMtMjYwLjRjMTAwLjItMTczLjYsMjAwLjUtMTczLjYsMzAwLjcsMFoiLz4KICAgICAgICA8cGF0aCBkPSJNODcxLjksNjEyLjdjMjUuMSw0My40LDEyLjUsNjUuMS0zNy42LDY1LjFzLTYyLjYtMjEuNy0zNy42LTY1LjFjMjUuMS00My40LDUwLjEtNDMuNCw3NS4yLDBaIi8+CiAgICAgIDwvZz4KICAgIDwvZz4KICA8L2c+Cjwvc3ZnPg==
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. let inLanguage;
  17. let debouncedAutoHeight;
  18. const $ = (selector) => document.querySelectorAll(selector);
  19. const $$ = (selector) => document.querySelector(selector);
  20. const tarAutoHeight = `prompt-input`;
  21. const tarAutoScrollHeight = `lorebook-data-input textarea, #charDesc`;
  22. const textReplaceSelector = `#chatMessages b:not([data-text-replaced]), #chatMessages p:not([data-text-replaced])`;
  23. const cURL = window.location.href;
  24. const muyiStyles = 'https://labs.muyi.tw/caveduck_modifier/style2.css?v=11312010702';
  25. const fontStyles = `
  26. user-input-form div[ng-repeat] textarea,
  27. #chatMessages b,
  28. #chatMessages p,
  29. form[ng-if~="!!chat.editMode"] textarea {
  30. font: normal clamp(16px, .95vw, 32px) / 1.75em var(--m_ff1);
  31. }
  32. #chatMessages b {
  33. font-family: var(--m_ff2);
  34. font-weight: 400;
  35. }
  36. form[ng-if~="!!chat.editMode"] textarea {
  37. font-size: var(--m_font-size);
  38. }
  39. user-input-form div[ng-repeat] textarea {
  40. font-size: var(--m_font-size);
  41. }
  42. `;
  43. const charMap = {
  44. '\\.{2,}': '⋯⋯',
  45. '⋯': '⋯⋯',
  46. '⋯{3,}': '⋯⋯',
  47. '!': '!',
  48. '\\?': '?',
  49. '~': '~',
  50. ';': ';',
  51. ':': ':',
  52. ',': ',',
  53. '\\.': '。',
  54. '\\(': '(',
  55. '\\)': ')'
  56. };
  57. const locale = {
  58. 'zh-hant': {
  59. cb_fontOverride: ['覆蓋字型', '作用頁:Talk<br>用慕儀喜歡的自訂字型取代預設字型。'],
  60. cb_shortButtons: ['快捷按鈕', '作用頁:Talk<br>將「我的資訊」與「使用者筆記」按鈕移到右側。'],
  61. cb_replaceText: ['取代符號', '作用頁:Talk<br>Claude 3 Haiku會使用錯誤的中文標點符號,這個功能可以修正它。'],
  62. cb_deskFix: ['桌面顯示修正', '作用頁:Talk<br>修正高解析度下的顯示體驗,讓對話畫面佔用全版,且圖片顯示區域更大。'],
  63. cb_mdFix: ['行動顯示修正', '作用頁:Talk<br>修正行動裝置的顯示問題。'],
  64. cb_hideLLM: ['隱藏低效LLM', '作用頁:Talk<br>隱藏表現與成本不合比例的LLM,避免誤選取。'],
  65. cb_autoHeight: ['編輯框自動高度', '作用頁:Edit Character、Lorebook、Custom prompt<br>每個項目使用卷軸十分愚蠢,勾選此項可以將其設為自動高度。'],
  66. toggleButton: '慕儀\n神器',
  67. reloadButton: '套用並重載',
  68. },
  69. 'en': {
  70. cb_fontOverride: ['Override Font', 'Active on: Talk<br>Replace default font with MuYi\'s preferred custom font.'],
  71. cb_shortButtons: ['Shortcut buttons', 'Active on: Talk<br>Move the "My Information" and "User Notes" buttons to the right side.'],
  72. cb_replaceText: ['Replace Symbols', 'Active on: Talk<br>Claude 3 Haiku uses incorrect Chinese punctuation. This feature fixes it.'],
  73. cb_deskFix: ['Desktop Display Fix', 'Active on: Talk<br>Fix display experience on high resolution, making the chat screen occupy the full screen and enlarging the image display area.'],
  74. cb_mdFix: ['Mobile Display Fix', 'Active on: Talk<br>Fix the display issues of mobile devices.'],
  75. cb_hideLLM: ['Hide unimportant LLMs', 'Active on: Talk<br>Hide LLMs whose performance does not correlate with their costs to avoid misselection.'],
  76. cb_autoHeight: ['Auto Height for Edit Box', 'Active on: Edit Character、Lorebook、Custom prompt<br>Using scrollbars for each item is stupid. Enable this to auto-height.'],
  77. toggleButton: 'MuYi\'s\nToolbox',
  78. reloadButton: 'Apply and reload',
  79. },
  80. };
  81. const settings = [
  82. {
  83. localeName: 'cb_fontOverride',
  84. key: 'sw_fontOverride'
  85. },
  86. {
  87. localeName: 'cb_shortButtons',
  88. key: 'sw_shortButtons'
  89. },
  90. {
  91. localeName: 'cb_replaceText',
  92. key: 'sw_replaceText'
  93. },
  94. {
  95. localeName: 'cb_deskFix',
  96. key: 'sw_deskFix'
  97. },
  98. {
  99. localeName: 'cb_hideLLM',
  100. key: 'sw_hideLLM'
  101. },
  102. {
  103. localeName: 'cb_mdFix',
  104. key: 'sw_mdFix'
  105. },
  106. {
  107. localeName: 'cb_autoHeight',
  108. key: 'sw_autoHeight'
  109. }
  110. ];
  111.  
  112. const switches = {};
  113. settings.forEach(setting => {
  114. switches[setting.key] = JSON.parse(localStorage.getItem(`enable${setting.key.slice(3)}`) || 'false');
  115. });
  116.  
  117. const domElements = {
  118. o_editMyInfoButton: 'button[ng-click="onEditMyInfoButtonClicked()"]',
  119. o_editUserNoteButton: 'button[ng-click="onEditUserNoteButtonClicked()"]',
  120. o_optionButton: '#optionButton',
  121. o_editButton: '#editButton',
  122. o_imgButton: '.hidden[ng-show~="backgroundImage"]'
  123. };
  124.  
  125. const [
  126. o_editMyInfoButton,
  127. o_editUserNoteButton,
  128. o_optionButton,
  129. o_editButton,
  130. o_imgButton
  131. ] = Object.values(domElements).map(selector => $$(selector));
  132.  
  133. // 添加自訂樣式
  134. function addCustomStyles() {
  135. GM_addStyle(fontStyles);
  136. console.log("Custom styles added.");
  137. }
  138.  
  139. // 自動調整高度的核心函式
  140. function autoHeight(el) {
  141. el.style.height = 'auto';
  142. el.style.overflow = 'auto';
  143. }
  144.  
  145. function autoScrollHeight(el) {
  146. autoHeight(el);
  147. el.style.height = `${el.scrollHeight}px`;
  148. }
  149.  
  150. // 初始化符合條件的元素
  151. function initializeAutoHeight() {
  152. if (!debouncedAutoHeight) {
  153. debouncedAutoHeight = debounce(() => {
  154. $(tarAutoHeight).forEach(autoHeight);
  155. $(tarAutoScrollHeight).forEach(autoScrollHeight);
  156. }, 666, 3);
  157. debouncedAutoHeight();
  158. window.addEventListener('keydown', debouncedAutoHeight);
  159. window.addEventListener('click', debouncedAutoHeight);
  160. }
  161. }
  162.  
  163.  
  164. // 替換指定選擇符的內容
  165. function replaceTextContent() {
  166. const processedAttribute = "data-text-replaced"; // 標記屬性名稱
  167. const el = $(`${textReplaceSelector}:not([${processedAttribute}])`);
  168. el.forEach((el) => {
  169. let originalText = el.textContent;
  170. for (const [pattern, replacement] of Object.entries(charMap)) {
  171. originalText = originalText.replace(new RegExp(pattern, 'g'), replacement);
  172. }
  173. el.textContent = originalText;
  174. el.setAttribute(processedAttribute, ""); // 添加標記屬性
  175. });
  176. }
  177.  
  178. // 延遲觸發的去抖函式
  179. function debounce(func, delay, repeat) {
  180. let timer = null;
  181. let count = 1;
  182. return () => {
  183. func();
  184. if (timer) clearInterval(timer);
  185. timer = setInterval(() => {
  186. func();
  187. count += 1;
  188. if (count >= repeat) {
  189. clearInterval(timer);
  190. }
  191. }, delay);
  192. };
  193. }
  194.  
  195. // 啟動 MutationObserver
  196. function initializeObserver() {
  197. const observer = new MutationObserver(() => {
  198. mainAction();
  199. });
  200. observer.observe(document.body, { childList: true, subtree: true });
  201. console.log("MutationObserver initialized.");
  202. }
  203.  
  204. // 檢查 inLanguage 並啟動必要功能
  205. function checkInLanguage() {
  206. const script = $$('script[type="application/ld+json"]');
  207. if (script) {
  208. try {
  209. const jsonData = JSON.parse(script.textContent);
  210. inLanguage = jsonData[0]?.inLanguage || '';
  211. } catch (error) {
  212. console.error("Failed to parse JSON:", error);
  213. }
  214. }
  215. }
  216.  
  217. function createSettingsUI() {
  218. const lang = ['zh-hant', 'zh-hans'].includes(inLanguage) ? 'zh-hant' : 'en';
  219. const texts = locale[lang];
  220.  
  221. // 創建核取方塊
  222. const createCheckbox = (setting) => {
  223. const container = document.createElement('div');
  224. const checkbox = document.createElement('input');
  225. const label = document.createElement('label');
  226. const desc = document.createElement('div');
  227. desc.className = 'desc';
  228.  
  229. checkbox.type = 'checkbox';
  230. const storageKey = `enable${setting.key.slice(3)}`;
  231. checkbox.id = storageKey;
  232.  
  233. const isChecked = JSON.parse(localStorage.getItem(storageKey) || 'false');
  234. checkbox.checked = isChecked;
  235. label.setAttribute('for', storageKey);
  236. label.textContent = texts[setting.localeName][0];
  237. desc.innerHTML = texts[setting.localeName][1];
  238. label.appendChild(desc);
  239. checkbox.addEventListener('change', () => {
  240. localStorage.setItem(storageKey, checkbox.checked);
  241. });
  242. container.appendChild(checkbox);
  243. container.appendChild(label);
  244. return container;
  245. };
  246.  
  247. // 創建按鈕和設定視窗
  248. const mt = document.createElement('div');
  249. mt.id = 'mt';
  250.  
  251. const toggleButton = document.createElement('button');
  252. toggleButton.className = 'button--red mt_toggleButton';
  253. toggleButton.textContent = texts.toggleButton;
  254. if (lang === 'zh-hant') toggleButton.style.fontSize = '1rem';
  255.  
  256. const settingsPanel = document.createElement('div');
  257. settingsPanel.className = 'mt_fixed mt_settingsPanel';
  258. settingsPanel.style.display = 'none';
  259.  
  260. // 添加核取方塊
  261. settings.forEach((setting) => {
  262. settingsPanel.appendChild(createCheckbox(setting));
  263. });
  264.  
  265. // 重整按鈕
  266. const reloadButton = document.createElement('button');
  267. reloadButton.textContent = texts.reloadButton;
  268. reloadButton.className = 'button--red';
  269. reloadButton.addEventListener('click', () => location.reload());
  270. settingsPanel.appendChild(reloadButton);
  271.  
  272. // 切換視窗顯示
  273. toggleButton.addEventListener('click', () => {
  274. settingsPanel.style.display = settingsPanel.style.display === 'none' ? 'block' : 'none';
  275. });
  276.  
  277. // 添加到頁面
  278. document.body.appendChild(mt);
  279. mt.appendChild(toggleButton);
  280. document.body.appendChild(settingsPanel);
  281.  
  282. // 快捷按鈕
  283. if (switches.sw_shortButtons && mURL('*/talk/*')) {
  284. ['👤', '📝'].forEach((text, index) => {
  285. const button = document.createElement('button');
  286. button.textContent = text;
  287. button.addEventListener('click', () => {
  288. o_optionButton.click();
  289. o_editButton.click();
  290. (index === 0 ? o_editMyInfoButton : o_editUserNoteButton).click();
  291. });
  292. mt.appendChild(button);
  293. });
  294. }
  295. }
  296.  
  297. function checkSettings() {
  298. checkInLanguage();
  299. settings.forEach(setting => {
  300. switches[setting.switchVar] = JSON.parse(localStorage.getItem(setting.storageKey) || 'false');
  301. });
  302.  
  303. if (switches.sw_fontOverride) addCustomStyles();
  304. mainAction();
  305. }
  306.  
  307. function mURL(pattern) {
  308. const patternParts = pattern.split('*');
  309. let lastIndex = 0;
  310. for (let part of patternParts) {
  311. if (part === "") continue;
  312. const index = cURL.indexOf(part, lastIndex);
  313. if (index === -1) return false;
  314. lastIndex = index + part.length;
  315. }
  316. return true;
  317. }
  318.  
  319. function setStylesheet() {
  320. const link = document.createElement("link");
  321. link.rel = 'stylesheet';
  322. link.href = muyiStyles;
  323. document.head.appendChild(link);
  324. }
  325.  
  326. function mainAction() {
  327. if (switches.sw_autoHeight && (mURL('*/created-characters/*') || mURL('*/prompt-build-script/*') || mURL('*/lorebook-editor/*'))) initializeAutoHeight();
  328. if (['zh-hant', 'zh-hans', 'ja', 'ko'].includes(inLanguage) && (switches.sw_replaceText)) replaceTextContent();
  329. if (mURL('*/talk/*')) {
  330. if (switches.sw_mdFix) o_imgButton.classList.add('mt_fix');
  331. if (switches.sw_deskFix) {
  332. $('.container, .items-center.text-lg.relative, div.flex.overflow-hidden.flex-grow>div.flex[class*="md:w-[40%]"], div.flex.overflow-hidden.flex-grow>div.flex[class*="md:w-[60%]"]').forEach(el => {
  333. el.classList.add('mt_fix');
  334. });
  335. }
  336. if (switches.sw_hideLLM) {
  337. $(`
  338. input#gpt-4-turbo, label[for="gpt-4-turbo"],
  339. input#claude-v2, label[for="claude-v2"],
  340. input#claude-v3-opus, label[for="claude-v3-opus"],
  341. input#claude-v3-sonnet, label[for="claude-v3-sonnet"],
  342. input[id="claude-v3.5-sonnet"], label[for="claude-v3.5-sonnet"],
  343. input#gpt-4-turbo, label[for="gpt-4-turbo"],
  344. input#gpt-4-turbo, label[for="gpt-4-turbo"]
  345. `).forEach(el => {
  346. el.style.display = 'none';
  347. });
  348. }
  349. }
  350. }
  351.  
  352. window.addEventListener('load', () => {
  353. setStylesheet();
  354. checkSettings();
  355. createSettingsUI();
  356. setTimeout(initializeObserver, 100);
  357. });
  358. })();