Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.setTimeout(function () {
- let userMessageCount = 0;
- // Create turn display DOM
- const counterDisplay = document.createElement("div");
- counterDisplay.id = "message-counter";
- counterDisplay.style.position = "absolute";
- counterDisplay.style.left = "50%";
- counterDisplay.style.transform = "translateX(-50%)";
- counterDisplay.style.zIndex = "9999";
- counterDisplay.style.padding = "6px 12px";
- counterDisplay.style.backgroundColor = "rgb(87 167 70 / 70%)";
- counterDisplay.style.width = "13em";
- counterDisplay.style.color = "white";
- counterDisplay.style.textAlign = "center";
- counterDisplay.style.fontSize = "20px";
- counterDisplay.style.borderRadius = "6px";
- counterDisplay.style.pointerEvents = "none";
- counterDisplay.textContent = "Turn";
- function deleteWrongStrong() {
- document
- .querySelectorAll('[data-message-author-role="assistant"] .markdown')
- .forEach((p) => {
- if (!p.querySelector("strong[data-is-last-node]")) return;
- const walker = document.createTreeWalker(
- p,
- NodeFilter.SHOW_TEXT,
- null,
- false
- );
- const textNodes = [];
- let current;
- while ((current = walker.nextNode())) {
- if (!current.parentElement.closest("code")) {
- textNodes.push(current);
- }
- }
- const combinedText = textNodes.map((node) => node.nodeValue).join("");
- const strongRegex = /\*\*(.+?)\*\*/g;
- let match,
- indices = [];
- while ((match = strongRegex.exec(combinedText))) {
- indices.push({
- start: match.index,
- end: match.index + match[0].length,
- text: match[1],
- });
- }
- if (indices.length === 0) return;
- let pointer = 0;
- textNodes.forEach((node) => {
- let nodeText = node.nodeValue;
- let nodeEnd = pointer + nodeText.length;
- let newHTML = "";
- let lastIndex = 0;
- indices.forEach((item, idx) => {
- if (item.start < nodeEnd && item.end > pointer) {
- let startInNode = Math.max(0, item.start - pointer);
- let endInNode = Math.min(nodeText.length, item.end - pointer);
- newHTML += nodeText.substring(lastIndex, startInNode);
- if (startInNode < endInNode) {
- newHTML += `<strong>${nodeText
- .substring(startInNode, endInNode)
- .replace(/\*\*/g, "")}</strong>`;
- lastIndex = endInNode;
- }
- }
- });
- newHTML += nodeText.substring(lastIndex);
- if (newHTML !== nodeText) {
- const span = document.createElement("span");
- span.innerHTML = newHTML;
- node.replaceWith(span);
- }
- pointer += nodeText.length;
- });
- });
- }
- // Turn color alert
- function updateDisplay() {
- if (userMessageCount > 30) {
- counterDisplay.style.backgroundColor = "rgb(233 83 83 / 70%)";
- } else if (userMessageCount > 20) {
- counterDisplay.style.backgroundColor = "rgb(197 183 46 / 74%)";
- } else {
- counterDisplay.style.backgroundColor = "rgb(87 167 70 / 70%)";
- }
- counterDisplay.textContent = `Turn: ${userMessageCount}`;
- }
- function countMessages(container) {
- if (!(container instanceof Element)) return;
- const userMessages = container.querySelectorAll(
- '[data-message-author-role="user"]'
- );
- userMessageCount = userMessages.length;
- console.log("Chat user message count:", userMessageCount);
- }
- const observer = new MutationObserver((mutations) => {
- mutations.forEach((mutation) => {
- mutation.addedNodes.forEach((node) => {
- if (node) {
- if (
- node.nodeType === 1 &&
- node.getAttribute("data-message-author-role") === "user"
- ) {
- userMessageCount++;
- updateDisplay();
- console.log("User messages so far:", userMessageCount);
- } else if (
- node.getAttribute("data-message-author-role") === "assistant"
- ) {
- deleteWrongStrong(); // delete visible **
- }
- }
- });
- });
- });
- setInterval(() => {
- if (!document.querySelector("#composer-submit-button")) return;
- document
- .querySelector("#composer-submit-button")
- .addEventListener("click", () => {
- setInterval(() => {
- deleteWrongStrong(); // delete visible **
- }, 5000);
- });
- }, 500);
- let lastTarget = null;
- setInterval(() => {
- const newTarget = document.querySelector(
- '[data-testid="conversation-turn-1"]'
- )?.parentNode;
- const displayParent = document.querySelector("#page-header");
- countMessages(document.body);
- updateDisplay();
- if (displayParent && !displayParent.querySelector("#message-counter")) {
- displayParent.appendChild(counterDisplay);
- }
- if (newTarget && newTarget !== lastTarget) {
- if (lastTarget) observer.disconnect();
- observer.observe(newTarget, {
- childList: true,
- subtree: true,
- });
- lastTarget = newTarget;
- console.log("Observer re-attached to new chat view");
- }
- }, 2000);
- }, 500);
Advertisement
Add Comment
Please, Sign In to add comment