c.ai X Text Color

Lets you change the text colors as you wish and highlight chosen words

当前为 2024-03-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name c.ai X Text Color
  3. // @namespace c.ai X Text Color
  4. // @match https://character.ai/*
  5. // @grant none
  6. // @license MIT
  7. // @version 2.6.5
  8. // @author Vishanka via chatGPT
  9. // @description Lets you change the text colors as you wish and highlight chosen words
  10. // @icon https://i.imgur.com/ynjBqKW.png
  11. // ==/UserScript==
  12.  
  13.  
  14. (function () {
  15. var plaintextColor = localStorage.getItem('plaintext_color');
  16. var italicColor = localStorage.getItem('italic_color');
  17. var charbubbleColor = localStorage.getItem('charbubble_color') || '#26272B';
  18. var userbubbleColor = localStorage.getItem('userbubble_color') || '#303136';
  19. // Default color if 'plaintext_color' is not set
  20. var defaultColor = '#A2A2AC';
  21.  
  22. // Use the retrieved color or default color
  23. var color = plaintextColor || defaultColor;
  24.  
  25. // Create the CSS style
  26. var css = "p[node='[object Object]'] { color: " + color + " !important; font-family: '__Inter_918210','Noto Sans', sans-serif !important; } p, textarea, button, div.text-sm { font-family: '__Inter_918210','Noto Sans', sans-serif !important; } em { color: " + italicColor + " !important; }";
  27.  
  28. // Add specific CSS based on window location
  29. css += window.location.href.includes("character.ai/chat") ?
  30. `.bg-surface-elevation-2 { background-color: ${charbubbleColor}; } .bg-surface-elevation-3 { background-color: ${userbubbleColor}; }` :
  31. `.bg-surface-elevation-2, .bg-surface-elevation-3 { background-color: #26272B; }`;
  32.  
  33. // css += `.mt-1.bg-surface-elevation-2 { background-color: ${charbubbleColor}; } .mt-1.bg-surface-elevation-3 { background-color: ${userbubbleColor}; }`;
  34.  
  35.  
  36.  
  37. var head = document.getElementsByTagName("head")[0];
  38. var style = document.createElement("style");
  39. style.setAttribute("type", "text/css");
  40. style.innerHTML = css;
  41. head.appendChild(style);
  42. })();
  43.  
  44.  
  45.  
  46. function changeColors() {
  47. const pTags = document.getElementsByTagName("p");
  48. const quotationMarksColor = localStorage.getItem('quotationmarks_color') || '#FFFFFF';
  49. const customColor = localStorage.getItem('custom_color') || '#FFFFFF';
  50. const wordlistCc = JSON.parse(localStorage.getItem('wordlist_cc')) || [];
  51.  
  52. const wordRegex = wordlistCc.length > 0 ? new RegExp('\\b(' + wordlistCc.map(word => word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|') + ')\\b', 'gi') : null;
  53.  
  54. const changes = [];
  55.  
  56. for (let i = 0; i < pTags.length; i++) {
  57. const pTag = pTags[i];
  58. if (
  59. pTag.dataset.colorChanged === "true" ||
  60. pTag.querySelector("code") ||
  61. pTag.querySelector("img") ||
  62. pTag.querySelector("textarea") ||
  63. pTag.querySelector("button") ||
  64. pTag.querySelector("div")
  65. ) {
  66. continue;
  67. }
  68. let text = pTag.innerHTML;
  69.  
  70. //Changes Text within Quotation Marks to white
  71. text = text.replace(/(["“”«»].*?["“”«»])/g, `<span style="color: ${quotationMarksColor}">$1</span>`);
  72. //Changes Text within Quotation Marks and a comma at the end to yellow
  73. text = text.replace(/(["“”«»][^"]*?,["“”«»])/g, `<span style="color: #E0DF7F">$1</span>`);
  74.  
  75. if (wordRegex) {
  76. text = text.replace(wordRegex, `<span style="color: ${customColor}">$1</span>`);
  77. }
  78.  
  79. changes.push({ pTag, text });
  80. }
  81.  
  82. changes.forEach(change => {
  83. const { pTag, text } = change;
  84. pTag.innerHTML = text;
  85. pTag.dataset.colorChanged = "true";
  86. });
  87.  
  88. console.log("Changed colors");
  89. }
  90.  
  91.  
  92. const divElements = document.querySelectorAll('div');
  93.  
  94. divElements.forEach(div => {
  95. const observer = new MutationObserver(changeColors);
  96. observer.observe(div, { subtree: true, childList: true });
  97. });
  98.  
  99.  
  100.  
  101.  
  102.  
  103. function createButton(symbol, onClick) {
  104. const colorpalettebutton = document.createElement('button');
  105. colorpalettebutton.innerHTML = symbol;
  106. colorpalettebutton.style.position = 'relative';
  107. colorpalettebutton.style.background = 'none';
  108. colorpalettebutton.style.border = 'none';
  109. colorpalettebutton.style.fontSize = '18px';
  110. colorpalettebutton.style.top = '-5px';
  111. colorpalettebutton.style.cursor = 'pointer';
  112. colorpalettebutton.addEventListener('click', onClick);
  113. return colorpalettebutton;
  114. }
  115.  
  116. // Function to create the color selector panel
  117. function createColorPanel() {
  118. const panel = document.createElement('div');
  119. panel.id = 'colorPanel';
  120. panel.style.position = 'fixed';
  121. panel.style.top = '50%';
  122. panel.style.left = '50%';
  123. panel.style.transform = 'translate(-50%, -50%)';
  124. panel.style.backgroundColor = 'rgba(19, 19, 22, 0.95)';
  125. panel.style.border = 'none';
  126. panel.style.borderRadius = '5px';
  127. panel.style.padding = '20px';
  128. // panel.style.border = '2px solid #000';
  129. panel.style.zIndex = '9999';
  130.  
  131. const categories = ['italic', 'quotationmarks', 'plaintext', 'custom', 'charbubble', 'userbubble'];
  132.  
  133. const colorPickers = {};
  134.  
  135. // Set a fixed width for the labels
  136. const labelWidth = '150px';
  137.  
  138. categories.forEach(category => {
  139. const colorPicker = document.createElement('input');
  140. colorPicker.type = 'color';
  141.  
  142. // Retrieve stored color from local storage
  143. const storedColor = localStorage.getItem(`${category}_color`);
  144. if (storedColor) {
  145. colorPicker.value = storedColor;
  146. }
  147.  
  148. colorPickers[category] = colorPicker;
  149.  
  150. // Create a div to hold color picker
  151. const colorDiv = document.createElement('div');
  152. colorDiv.style.position = 'relative';
  153. colorDiv.style.width = '20px';
  154. colorDiv.style.height = '20px';
  155. colorDiv.style.marginLeft = '10px';
  156. colorDiv.style.top = '5px';
  157. colorDiv.style.backgroundColor = colorPicker.value;
  158. colorDiv.style.display = 'inline-block';
  159. colorDiv.style.marginRight = '10px';
  160. colorDiv.style.cursor = 'pointer';
  161. colorDiv.style.border = '1px solid black';
  162.  
  163.  
  164. // Event listener to open color picker when the color square is clicked
  165. colorDiv.addEventListener('click', function () {
  166. colorPicker.click();
  167. });
  168.  
  169. // Event listener to update the color div when the color changes
  170. colorPicker.addEventListener('input', function () {
  171. colorDiv.style.backgroundColor = colorPicker.value;
  172. });
  173.  
  174. const label = document.createElement('label');
  175. label.style.width = labelWidth; // Set fixed width for the label
  176. label.appendChild(document.createTextNode(`${category}: `));
  177.  
  178. // Reset button for each color picker
  179. const resetButton = createButton('↺', function () {
  180. colorPicker.value = getDefaultColor(category);
  181. colorDiv.style.backgroundColor = colorPicker.value;
  182. });
  183. resetButton.style.position = 'relative';
  184. resetButton.style.top = '1px';
  185. // Create a div to hold label, color picker, and reset button
  186. const containerDiv = document.createElement('div');
  187. containerDiv.appendChild(label);
  188. containerDiv.appendChild(colorDiv);
  189. containerDiv.appendChild(resetButton);
  190.  
  191. panel.appendChild(containerDiv);
  192. panel.appendChild(document.createElement('br'));
  193. });
  194.  
  195. // Custom word list input
  196. const wordListInput = document.createElement('input');
  197. wordListInput.type = 'text';
  198. wordListInput.placeholder = 'Separate words with commas';
  199. wordListInput.style.width = '250px';
  200. wordListInput.style.height = '35px';
  201. wordListInput.style.borderRadius = '3px';
  202. wordListInput.style.marginBottom = '10px';
  203. panel.appendChild(wordListInput);
  204. panel.appendChild(document.createElement('br'));
  205.  
  206. const wordListContainer = document.createElement('div');
  207. wordListContainer.style.display = 'flex';
  208. wordListContainer.style.flexWrap = 'wrap';
  209. wordListContainer.style.maxWidth = '300px'; // Set a fixed maximum width for the container
  210.  
  211. // Display custom word list buttons
  212. const wordListArray = JSON.parse(localStorage.getItem('wordlist_cc')) || [];
  213. const wordListButtons = [];
  214.  
  215. function createWordButton(word) {
  216. const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
  217.  
  218. const removeSymbol = isMobile ? '×' : '🞮';
  219.  
  220. const wordButton = createButton(`${word} ${removeSymbol}`, function() {
  221. // Remove the word from the list and update the panel
  222. const index = wordListArray.indexOf(word);
  223. if (index !== -1) {
  224. wordListArray.splice(index, 1);
  225. updateWordListButtons();
  226. }
  227. });
  228.  
  229. // Word Buttons
  230. wordButton.style.borderRadius = '3px';
  231. wordButton.style.border = 'none';
  232. wordButton.style.backgroundColor = '#26272B';
  233. wordButton.style.marginBottom = '5px';
  234. wordButton.style.marginRight = '5px';
  235. wordButton.style.fontSize = '16px';
  236. wordButton.classList.add('word-button');
  237. return wordButton;
  238. }
  239.  
  240. function updateWordListButtons() {
  241. wordListContainer.innerHTML = ''; // Clear the container
  242. wordListArray.forEach(word => {
  243. const wordButton = createWordButton(word);
  244. wordListContainer.appendChild(wordButton);
  245. });
  246. }
  247.  
  248. // Append wordListContainer to the panel
  249.  
  250.  
  251.  
  252. updateWordListButtons();
  253.  
  254. // Add Words button
  255. const addWordsButton = document.createElement('button');
  256. addWordsButton.textContent = 'Add';
  257. addWordsButton.style.marginTop = '-8px';
  258. addWordsButton.style.marginLeft = '5px';
  259. addWordsButton.style.borderRadius = '3px';
  260. addWordsButton.style.border = 'none';
  261. addWordsButton.style.backgroundColor = '#26272B';
  262. addWordsButton.addEventListener('click', function() {
  263. // Get the input value, split into words, and add to wordListArray
  264. const wordListValue = wordListInput.value;
  265. const newWords = wordListValue.split(',').map(word => word.trim().toLowerCase()).filter(word => word !== ''); // Convert to lowercase and remove empty entries
  266. wordListArray.push(...newWords);
  267.  
  268. // Update the word list buttons in the panel
  269. updateWordListButtons();
  270. });
  271.  
  272. // Create a div to group the input and button on the same line
  273. const inputButtonContainer = document.createElement('div');
  274. inputButtonContainer.style.display = 'flex';
  275. inputButtonContainer.style.alignItems = 'center';
  276.  
  277. inputButtonContainer.appendChild(wordListInput);
  278. inputButtonContainer.appendChild(addWordsButton);
  279.  
  280. // Append the container to the panel
  281. panel.appendChild(inputButtonContainer);
  282. panel.appendChild(wordListContainer);
  283. // Create initial word list buttons
  284. updateWordListButtons();
  285.  
  286.  
  287. // OK button
  288. const okButton = document.createElement('button');
  289. okButton.textContent = 'Confirm';
  290. okButton.style.marginTop = '-20px';
  291. okButton.style.width = '75px';
  292. okButton.style.height = '35px';
  293. okButton.style.marginRight = '5px';
  294. okButton.style.borderRadius = '3px';
  295. okButton.style.border = 'none';
  296. okButton.style.backgroundColor = '#26272B';
  297. okButton.style.position = 'relative';
  298. okButton.style.left = '24%';
  299. //okButton.style.transform = 'translateX(-50%)';
  300. okButton.addEventListener('click', function() {
  301. // Save selected colors to local storage
  302. categories.forEach(category => {
  303. const oldValue = localStorage.getItem(`${category}_color`);
  304. const newValue = colorPickers[category].value;
  305.  
  306. if (oldValue !== newValue) {
  307. localStorage.setItem(`${category}_color`, newValue);
  308.  
  309. // If 'plaintext' color is changed, auto-reload the page
  310. if (category === 'plaintext') {
  311. window.location.reload();
  312. }
  313. }
  314. });
  315.  
  316.  
  317. // Save custom word list to local storage
  318. const wordListValue = wordListInput.value;
  319. const newWords = wordListValue.split(',').map(word => word.trim().toLowerCase()).filter(word => word !== ''); // Convert to lowercase and remove empty entries
  320. const uniqueNewWords = Array.from(new Set(newWords)); // Remove duplicates
  321.  
  322. // Check for existing words and add only new ones
  323. uniqueNewWords.forEach(newWord => {
  324. if (!wordListArray.includes(newWord)) {
  325. wordListArray.push(newWord);
  326. }
  327. });
  328.  
  329. localStorage.setItem('wordlist_cc', JSON.stringify(wordListArray));
  330.  
  331. updateWordListButtons();
  332.  
  333. // Close the panel
  334. panel.remove();
  335. });
  336.  
  337. // Cancel button
  338. const cancelButton = document.createElement('button');
  339. cancelButton.textContent = 'Cancel';
  340. cancelButton.style.marginTop = '-20px';
  341. cancelButton.style.borderRadius = '3px';
  342. cancelButton.style.width = '75px';
  343. cancelButton.style.marginLeft = '5px';
  344. cancelButton.style.height = '35px';
  345. cancelButton.style.border = 'none';
  346. cancelButton.style.backgroundColor = '#5E5E5E';
  347. cancelButton.style.position = 'relative';
  348. cancelButton.style.left = '25%';
  349. cancelButton.addEventListener('click', function() {
  350. // Close the panel without saving
  351. panel.remove();
  352. });
  353.  
  354. panel.appendChild(document.createElement('br'));
  355. panel.appendChild(okButton);
  356. panel.appendChild(cancelButton);
  357.  
  358. document.body.appendChild(panel);
  359. }
  360.  
  361.  
  362.  
  363. // Function to get the default color for a category
  364. function getDefaultColor(category) {
  365. const defaultColors = {
  366. 'italic': '#E0DF7F',
  367. 'quotationmarks': '#FFFFFF',
  368. 'plaintext': '#A2A2AC',
  369. 'custom': '#E0DF7F',
  370. 'charbubble': '#26272B',
  371. 'userbubble': '#303136'
  372. };
  373. return defaultColors[category];
  374. }
  375.  
  376. const mainButton = createButton('', function() {
  377. const colorPanelExists = document.getElementById('colorPanel');
  378. if (!colorPanelExists) {
  379. createColorPanel();
  380. }
  381. });
  382.  
  383. // Set the background image of the button to the provided image
  384. mainButton.style.backgroundImage = "url('https://i.imgur.com/yBgJ3za.png')";
  385. mainButton.style.backgroundSize = "cover";
  386. mainButton.style.position = "fixed"; // Use "fixed" for a position relative to the viewport
  387. mainButton.style.top = "10px"; // Adjust the top position as needed
  388. mainButton.style.right = "10px"; // Adjust the right position as needed
  389. mainButton.style.width = "22px"; // Adjust the width and height as needed
  390. mainButton.style.height = "22px"; // Adjust the width and height as needed
  391.  
  392. // Function to insert the mainButton into the body of the document
  393. function insertMainButton() {
  394. document.body.appendChild(mainButton);
  395. }
  396.  
  397. // Call the function to insert the mainButton into the body
  398. insertMainButton();
  399.  
  400. console.error('Main button appended to the top right corner.');