Guest User

Untitled

a guest
Jul 4th, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.setTimeout(function () {
  2.   let userMessageCount = 0;
  3.  
  4.   // Create turn display DOM
  5.   const counterDisplay = document.createElement("div");
  6.   counterDisplay.id = "message-counter";
  7.   counterDisplay.style.position = "absolute";
  8.  
  9.   counterDisplay.style.left = "50%";
  10.   counterDisplay.style.transform = "translateX(-50%)";
  11.   counterDisplay.style.zIndex = "9999";
  12.   counterDisplay.style.padding = "6px 12px";
  13.   counterDisplay.style.backgroundColor = "rgb(87 167 70 / 70%)";
  14.   counterDisplay.style.width = "13em";
  15.   counterDisplay.style.color = "white";
  16.   counterDisplay.style.textAlign = "center";
  17.   counterDisplay.style.fontSize = "20px";
  18.   counterDisplay.style.borderRadius = "6px";
  19.   counterDisplay.style.pointerEvents = "none";
  20.   counterDisplay.textContent = "Turn";
  21.  
  22.   function deleteWrongStrong() {
  23.     document
  24.       .querySelectorAll('[data-message-author-role="assistant"] .markdown')
  25.       .forEach((p) => {
  26.         if (!p.querySelector("strong[data-is-last-node]")) return;
  27.         const walker = document.createTreeWalker(
  28.           p,
  29.           NodeFilter.SHOW_TEXT,
  30.           null,
  31.           false
  32.         );
  33.         const textNodes = [];
  34.         let current;
  35.  
  36.         while ((current = walker.nextNode())) {
  37.           if (!current.parentElement.closest("code")) {
  38.             textNodes.push(current);
  39.           }
  40.         }
  41.  
  42.         const combinedText = textNodes.map((node) => node.nodeValue).join("");
  43.         const strongRegex = /\*\*(.+?)\*\*/g;
  44.         let match,
  45.           indices = [];
  46.  
  47.         while ((match = strongRegex.exec(combinedText))) {
  48.           indices.push({
  49.             start: match.index,
  50.             end: match.index + match[0].length,
  51.             text: match[1],
  52.           });
  53.         }
  54.  
  55.         if (indices.length === 0) return;
  56.  
  57.         let pointer = 0;
  58.         textNodes.forEach((node) => {
  59.           let nodeText = node.nodeValue;
  60.           let nodeEnd = pointer + nodeText.length;
  61.           let newHTML = "";
  62.           let lastIndex = 0;
  63.  
  64.           indices.forEach((item, idx) => {
  65.             if (item.start < nodeEnd && item.end > pointer) {
  66.               let startInNode = Math.max(0, item.start - pointer);
  67.               let endInNode = Math.min(nodeText.length, item.end - pointer);
  68.               newHTML += nodeText.substring(lastIndex, startInNode);
  69.               if (startInNode < endInNode) {
  70.                 newHTML += `<strong>${nodeText
  71.                   .substring(startInNode, endInNode)
  72.                   .replace(/\*\*/g, "")}</strong>`;
  73.                 lastIndex = endInNode;
  74.               }
  75.             }
  76.           });
  77.  
  78.           newHTML += nodeText.substring(lastIndex);
  79.           if (newHTML !== nodeText) {
  80.             const span = document.createElement("span");
  81.             span.innerHTML = newHTML;
  82.             node.replaceWith(span);
  83.           }
  84.  
  85.           pointer += nodeText.length;
  86.         });
  87.       });
  88.   }
  89.  
  90.   // Turn color alert
  91.   function updateDisplay() {
  92.     if (userMessageCount > 30) {
  93.       counterDisplay.style.backgroundColor = "rgb(233 83 83 / 70%)";
  94.     } else if (userMessageCount > 20) {
  95.       counterDisplay.style.backgroundColor = "rgb(197 183 46 / 74%)";
  96.     } else {
  97.       counterDisplay.style.backgroundColor = "rgb(87 167 70 / 70%)";
  98.     }
  99.     counterDisplay.textContent = `Turn: ${userMessageCount}`;
  100.   }
  101.  
  102.   function countMessages(container) {
  103.     if (!(container instanceof Element)) return;
  104.  
  105.     const userMessages = container.querySelectorAll(
  106.       '[data-message-author-role="user"]'
  107.     );
  108.     userMessageCount = userMessages.length;
  109.     console.log("Chat user message count:", userMessageCount);
  110.   }
  111.  
  112.   const observer = new MutationObserver((mutations) => {
  113.     mutations.forEach((mutation) => {
  114.       mutation.addedNodes.forEach((node) => {
  115.         if (node) {
  116.           if (
  117.             node.nodeType === 1 &&
  118.             node.getAttribute("data-message-author-role") === "user"
  119.           ) {
  120.             userMessageCount++;
  121.             updateDisplay();
  122.             console.log("User messages so far:", userMessageCount);
  123.           } else if (
  124.             node.getAttribute("data-message-author-role") === "assistant"
  125.           ) {
  126.             deleteWrongStrong(); // delete visible **
  127.           }
  128.         }
  129.       });
  130.     });
  131.   });
  132.  
  133.   setInterval(() => {
  134.     if (!document.querySelector("#composer-submit-button")) return;
  135.     document
  136.       .querySelector("#composer-submit-button")
  137.       .addEventListener("click", () => {
  138.         setInterval(() => {
  139.           deleteWrongStrong(); // delete visible **
  140.         }, 5000);
  141.       });
  142.   }, 500);
  143.  
  144.   let lastTarget = null;
  145.  
  146.   setInterval(() => {
  147.     const newTarget = document.querySelector(
  148.       '[data-testid="conversation-turn-1"]'
  149.     )?.parentNode;
  150.  
  151.     const displayParent = document.querySelector("#page-header");
  152.     countMessages(document.body);
  153.     updateDisplay();
  154.  
  155.     if (displayParent && !displayParent.querySelector("#message-counter")) {
  156.       displayParent.appendChild(counterDisplay);
  157.     }
  158.  
  159.     if (newTarget && newTarget !== lastTarget) {
  160.       if (lastTarget) observer.disconnect();
  161.  
  162.       observer.observe(newTarget, {
  163.         childList: true,
  164.         subtree: true,
  165.       });
  166.       lastTarget = newTarget;
  167.       console.log("Observer re-attached to new chat view");
  168.     }
  169.   }, 2000);
  170. }, 500);
  171.  
Advertisement
Add Comment
Please, Sign In to add comment