Character.AI X Text Color

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

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

  1. // ==UserScript==
  2. // @name Character.AI X Text Color
  3. // @namespace Character.AI X Text Color by Vishanka
  4. // @match https://*.character.ai/*
  5. // @grant none
  6. // @license MIT
  7. // @version 2.4
  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.  
  17. // Default color if 'plaintext_color' is not set
  18. var defaultColor = '#958C7F';
  19.  
  20. // Use the retrieved color or default color
  21. var color = plaintextColor || defaultColor;
  22.  
  23. // Create the CSS style
  24. // var css =
  25. //"p[node='[object Object]'] { color: " + color + " !important; }";
  26. //"p, div, textarea { font-family: 'Noto Sans', sans-serif !important; }";
  27. var css = "p[node='[object Object]'] { color: " + color + " !important; font-family: '__Inter_cc86f0','Noto Sans', sans-serif !important; } p, textarea, button, div.text-sm { font-family: '__Inter_cc86f0','Noto Sans', sans-serif !important; }";
  28. //var css = "p[node='[object Object]'] { color: " + color + " !important; }";
  29.  
  30. var head = document.getElementsByTagName("head")[0];
  31. var style = document.createElement("style");
  32. style.setAttribute("type", "text/css");
  33. style.innerHTML = css;
  34. head.appendChild(style);
  35.  
  36.  
  37.  
  38. })();
  39.  
  40. function changeColors() {
  41. const pTags = document.getElementsByTagName("p");
  42. for (let i = 0; i < pTags.length; i++) {
  43. const pTag = pTags[i];
  44. if (
  45. pTag.dataset.colorChanged === "true" ||
  46. pTag.querySelector("code") ||
  47. pTag.querySelector("img") ||
  48. pTag.querySelector("textarea")
  49. ) {
  50. continue;
  51. }
  52. let text = pTag.innerHTML;
  53.  
  54. const aTags = pTag.getElementsByTagName("a"); // Get all <a> tags within the <p> tag
  55.  
  56. // Remove the <a> tags temporarily
  57. for (let j = 0; j < aTags.length; j++) {
  58. const aTag = aTags[j];
  59. text = text.replace(aTag.outerHTML, "REPLACE_ME_" + j); // Use a placeholder to be able to restore the links later
  60. }
  61.  
  62. //Changes Text within Quotation Marks to white
  63. text = text.replace(/(["“”«»].*?["“”«»])/g, `<span style="color: ${localStorage.getItem('quotationmarks_color') || '#FFFFFF'}">$1</span>`);
  64. //Changes Text within Quotation Marks and a comma at the end to yellow
  65. text = text.replace(/(["“”«»][^"]*?,["“”«»])/g, '<span style="color: #E0DF7F">$1</span>');
  66. // Changes Italic Text Color
  67. text = text.replace(/<em>(.*?)<\/em>/g,`<span style="color: ${localStorage.getItem('italic_color') || '#E0DF7F'}; font-style: italic;">$1</span>`);
  68. // Changes Textcolor within Percentages to blue
  69. // text = text.replace(/([%][^"]*?[%])/g, '<span style="color: #0000FF">$1</span>');
  70.  
  71. var wordlist_cc = JSON.parse(localStorage.getItem('wordlist_cc')) || [];
  72.  
  73. // Check if the wordlist is not empty before creating the regex
  74. if (wordlist_cc.length > 0) {
  75. // Escape special characters in each word and join them with the "|" operator
  76. var wordRegex = new RegExp('\\b(' + wordlist_cc.map(word => word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|') + ')\\b', 'gi');
  77.  
  78. // Replace matching words in the text
  79. text = text.replace(wordRegex, `<span style="color: ${localStorage.getItem('custom_color') || '#FFFFFF'}">$1</span>`);
  80. }
  81.  
  82.  
  83.  
  84. // Restore the <a> tags
  85. for (let j = 0; j < aTags.length; j++) {
  86. const aTag = aTags[j];
  87. text = text.replace("REPLACE_ME_" + j, aTag.outerHTML);
  88. }
  89.  
  90. pTag.innerHTML = text;
  91. pTag.dataset.colorChanged = "true";
  92. }
  93.  
  94. console.log("Changed colors");
  95. }
  96.  
  97. const observer = new MutationObserver(changeColors);
  98. observer.observe(document, { subtree: true, childList: true });
  99. changeColors();
  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'];
  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. // Save custom word list to local storage
  317. const wordListValue = wordListInput.value;
  318. const newWords = wordListValue.split(',').map(word => word.trim().toLowerCase()).filter(word => word !== ''); // Convert to lowercase and remove empty entries
  319. const uniqueNewWords = Array.from(new Set(newWords)); // Remove duplicates
  320.  
  321. // Check for existing words and add only new ones
  322. uniqueNewWords.forEach(newWord => {
  323. if (!wordListArray.includes(newWord)) {
  324. wordListArray.push(newWord);
  325. }
  326. });
  327.  
  328. localStorage.setItem('wordlist_cc', JSON.stringify(wordListArray));
  329.  
  330. updateWordListButtons();
  331.  
  332. // Close the panel
  333. panel.remove();
  334. });
  335.  
  336. // Cancel button
  337. const cancelButton = document.createElement('button');
  338. cancelButton.textContent = 'Cancel';
  339. cancelButton.style.marginTop = '-20px';
  340. cancelButton.style.borderRadius = '3px';
  341. cancelButton.style.width = '75px';
  342. cancelButton.style.marginLeft = '5px';
  343. cancelButton.style.height = '35px';
  344. cancelButton.style.border = 'none';
  345. cancelButton.style.backgroundColor = '#5E5E5E';
  346. cancelButton.style.position = 'relative';
  347. cancelButton.style.left = '25%';
  348. cancelButton.addEventListener('click', function() {
  349. // Close the panel without saving
  350. panel.remove();
  351. });
  352.  
  353. panel.appendChild(document.createElement('br'));
  354. panel.appendChild(okButton);
  355. panel.appendChild(cancelButton);
  356.  
  357. document.body.appendChild(panel);
  358. }
  359.  
  360.  
  361.  
  362. // Function to get the default color for a category
  363. function getDefaultColor(category) {
  364. const defaultColors = {
  365. 'italic': '#E0DF7F',
  366. 'quotationmarks': '#FFFFFF',
  367. 'plaintext': '#878788',
  368. 'custom': '#E0DF7F'
  369. };
  370. return defaultColors[category];
  371. }
  372.  
  373. const mainButton = createButton('', function() {
  374. const colorPanelExists = document.getElementById('colorPanel');
  375. if (!colorPanelExists) {
  376. createColorPanel();
  377. }
  378. });
  379.  
  380. // Set the background image of the button to the provided image
  381. mainButton.style.backgroundImage = "url('https://i.imgur.com/yBgJ3za.png')";
  382. mainButton.style.backgroundSize = "cover";
  383. mainButton.style.position = "fixed"; // Use "fixed" for a position relative to the viewport
  384. mainButton.style.top = "10px"; // Adjust the top position as needed
  385. mainButton.style.right = "10px"; // Adjust the right position as needed
  386. mainButton.style.width = "22px"; // Adjust the width and height as needed
  387. mainButton.style.height = "22px"; // Adjust the width and height as needed
  388.  
  389. const targetSelector = '.w-96';
  390. const secondaryTargetSelector = '.w-dvw > div:nth-child(1)';
  391.  
  392. // Function to insert the mainButton into the target panel
  393. function insertMainButton(targetPanel) {
  394. targetPanel.appendChild(mainButton);
  395. }
  396.  
  397. // MutationObserver for the web version
  398. const webObserver = new MutationObserver(() => {
  399. const updatedTargetPanel = document.querySelector(targetSelector);
  400. if (updatedTargetPanel) {
  401. insertMainButton(updatedTargetPanel);
  402. webObserver.disconnect();
  403. }
  404. });
  405.  
  406. // MutationObserver for phones
  407. const phoneObserver = new MutationObserver(() => {
  408. const updatedTargetPanel2 = document.querySelector(secondaryTargetSelector);
  409. if (updatedTargetPanel2) {
  410. insertMainButton(updatedTargetPanel2);
  411. phoneObserver.disconnect();
  412. }
  413. });
  414.  
  415. // Determine whether to use webObserver or phoneObserver based on the screen width
  416. if (window.innerWidth >= 1000) {
  417. webObserver.observe(document.body, { subtree: true, childList: true });
  418. } else {
  419. phoneObserver.observe(document.body, { subtree: true, childList: true });
  420. }
  421.  
  422. console.error('Target panel not found. Waiting for changes...');